Compare commits

...

41 Commits

Author SHA1 Message Date
benchmark
7eb7d2bfee Benchmark results 2024-08-02 10:04:10 +00:00
benchmark
7849f3b000 Benchmark results 2024-07-31 21:00:51 +00:00
benchmark
7b4338902b Benchmark results 2024-07-31 13:20:37 +00:00
benchmark
87230a215c Benchmark results 2024-07-31 12:43:06 +00:00
benchmark
0e404a85b8 Benchmark results 2024-07-31 12:22:03 +00:00
benchmark
7ca400ab1d Benchmark results 2024-07-31 12:19:39 +00:00
benchmark
fa7131d97a Benchmark results 2024-07-31 12:06:22 +00:00
benchmark
0d86fe7c12 Benchmark results 2024-07-24 17:23:08 +00:00
benchmark
974a6c6be1 Benchmark results 2024-07-24 17:13:19 +00:00
benchmark
fd8a58058e Benchmark results 2024-07-24 09:32:39 +00:00
0b51f373d5 Merge branch 'master' into benchmark 2024-07-24 11:24:09 +02:00
deb5175f94 Remove old benchmarks 2024-07-24 11:24:06 +02:00
benchmark
6ed67e27a0 Benchmark results 2024-07-24 09:17:27 +00:00
7faa683237 Print first date then commit hash for benchmark results
All checks were successful
Run Tests / Test (push) Successful in 24s
Benchmark BufferPool / RunBenchmarks (push) Successful in 26s
2024-07-24 11:16:57 +02:00
benchmark
031a0a38a4 Benchmark results 2024-07-24 09:16:11 +00:00
3d7702f1c6 Add pdu benchmarks
All checks were successful
Run Tests / Test (push) Successful in 29s
Benchmark BufferPool / RunBenchmarks (push) Successful in 35s
2024-07-24 11:15:35 +02:00
benchmark
3fe30201c9 Benchmark results 2024-07-24 06:11:33 +00:00
benchmark
1c35d35b3e Benchmark results 2024-07-23 16:47:45 +00:00
6e58c5cd45 Track benchmark before checkout
All checks were successful
Run Tests / Test (push) Successful in 15s
Benchmark BufferPool / RunBenchmarks (push) Successful in 27s
2024-07-23 18:47:05 +02:00
46ae251b32 Make workflows actually trigger, oops
Some checks failed
Run Tests / Test (push) Successful in 20s
Benchmark BufferPool / RunBenchmarks (push) Failing after 23s
2024-07-23 18:42:22 +02:00
921c31b9fc Maybe delete local benchmark before pull.. 2024-07-23 18:42:22 +02:00
e0abb0ca70 Fetch benchmark before checkout 2024-07-23 18:31:47 +02:00
123d90b4f5 Make workflows not trigger on benchmark 2024-07-23 18:30:42 +02:00
0c8725a69e Fix up the benchmark workflow 2024-07-23 18:30:03 +02:00
PhatPhuckDave
75514c7d90 Add benchmark program 2024-07-23 18:24:11 +02:00
PhatPhuckDave
6f164e5933 Add benchmark cicd
All checks were successful
Benchmark BufferPool / RunBenchmarks (push) Successful in 8s
Run Tests / Test (push) Successful in 14s
2024-07-23 17:10:36 +02:00
PhatPhuckDave
36dcbbc154 Add benchmarks for bufpool 2024-07-23 15:08:07 +02:00
PhatPhuckDave
2759185dbb Refactor bufpool methods 2024-07-23 14:55:49 +02:00
PhatPhuckDave
a9827f10ce Rework bufpool to be a little more efficient, I hope 2024-07-23 14:55:33 +02:00
PhatPhuckDave
aff4ffe070 update 2024-07-23 13:41:27 +02:00
PhatPhuckDave
c73a6066c4 Fix pool to uint from int 2024-07-23 09:31:26 +02:00
PhatPhuckDave
f00dcafac4 Update submitsm encode 2024-07-22 23:14:05 +02:00
PhatPhuckDave
4ea320c784 Make bufpool use uint
I mean we can't have a pool of negative size?
2024-07-22 23:13:34 +02:00
PhatPhuckDave
3d0d8ce676 Rework encode to return pointer 2024-07-22 23:09:32 +02:00
PhatPhuckDave
73cc49788f Implement byte buffer pool 2024-07-22 23:05:05 +02:00
PhatPhuckDave
c558e54750 Add a byte buffer pool 2024-07-22 23:05:05 +02:00
PhatPhuckDave
4fcc1d88ff Implement a part of submit encode 2024-07-22 22:58:01 +02:00
PhatPhuckDave
abfba3b5a7 Make every struct have a header header and not inherit the header 2024-07-22 22:52:04 +02:00
PhatPhuckDave
98fb872fc1 Rework encode, idiot 2024-07-22 22:49:01 +02:00
PhatPhuckDave
a68600281b Add tests for submit size 2024-07-22 22:47:06 +02:00
PhatPhuckDave
1b904b69fd Implement size in submit_pdu 2024-07-22 22:43:48 +02:00
31 changed files with 888 additions and 42 deletions

View File

@@ -0,0 +1,24 @@
name: Benchmark BufferPool
on:
push:
branches:
- '*'
- '!benchmark'
jobs:
RunBenchmarks:
runs-on: ubuntu-latest
env:
RUNNER_TOOL_CACHE: /opt/hostedtoolcache
GOMODCACHE: /opt/hostedtoolcache/go/pkg/mod
steps:
- name: Check out repository code
uses: actions/checkout@v4
- name: Get Go
uses: actions/setup-go@v3
with:
go-version-file: 'go.mod'
check-latest: true
cache: true
- name: Run benchmark
run: cd benchmark && go run . -setid

View File

@@ -0,0 +1,24 @@
name: Run Tests
on:
push:
branches:
- '*'
- '!benchmark'
jobs:
Test:
runs-on: ubuntu-latest
env:
RUNNER_TOOL_CACHE: /opt/hostedtoolcache
GOMODCACHE: /opt/hostedtoolcache/go/pkg/mod
steps:
- name: Check out repository code
uses: actions/checkout@v4
- name: Get Go
uses: actions/setup-go@v3
with:
go-version-file: 'go.mod'
check-latest: true
cache: true
- name: Run tests
run: go test ./...

162
benchmark/benchmark.go Normal file
View File

@@ -0,0 +1,162 @@
package main
import (
"flag"
"log"
"os"
"os/exec"
"path"
"strings"
"time"
)
func init() {
}
func main() {
setid := flag.Bool("setid", false, "Set the git user name and email to benchmark")
flag.Parse()
log.Printf("Starting benchmarking...")
if *setid {
setId()
}
head := getHeadHash()
log.Printf("HEAD at %s", head)
err := os.MkdirAll("results", 0755)
if err != nil {
log.Fatalf("Error creating results directory with %v", err)
}
now := time.Now().Format(time.DateOnly)
resultFile := path.Join(".", "results", now+"_"+head+".txt")
log.Printf("Writing results to %s", resultFile)
outFile, err := os.Create(resultFile)
if err != nil {
log.Fatalf("Error creating result file %s with %v", resultFile, err)
}
cmd := exec.Command("go", "test", "-bench", ".", "./pdu")
cmd.Dir = "../"
cmd.Stdout = outFile
log.Printf("Running benchmark...")
err = cmd.Run()
if err != nil {
log.Fatal(err)
}
log.Printf("Benchmarking done; writing results to git...")
log.Printf("Removing local benchmark branch")
removeLocalBenchmarkBranch()
time.Sleep(200 * time.Millisecond)
log.Printf("Fetching benchmark branch")
fetchBenchmark()
time.Sleep(200 * time.Millisecond)
log.Printf("Tracking benchmark branch")
trackBenchmark()
time.Sleep(200 * time.Millisecond)
log.Printf("Checking out benchmark branch")
checkoutBenchmark()
time.Sleep(200 * time.Millisecond)
log.Printf("Adding benchmark results")
addResults()
time.Sleep(200 * time.Millisecond)
log.Printf("Committing benchmark results")
commitBenchmark()
time.Sleep(200 * time.Millisecond)
log.Printf("Pushing benchmark results")
pushBenchmark()
log.Printf("Done")
}
func removeLocalBenchmarkBranch() {
gitBranch := exec.Command("git", "branch", "-D", "benchmark")
gitBranch.Dir = "../"
out, err := gitBranch.CombinedOutput()
if err != nil {
log.Printf("Could not remove local benchmark branch with code %v and error %v", err, string(out))
}
}
func getHeadHash() string {
cmd := exec.Command("git", "rev-parse", "HEAD")
cmd.Dir = "../"
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("Could not get HEAD with code %v and error %v", err, string(out))
}
return strings.TrimSpace(string(out))
}
func fetchBenchmark() {
gitFetch := exec.Command("git", "fetch", "origin", "benchmark")
gitFetch.Dir = "../"
out, err := gitFetch.CombinedOutput()
if err != nil {
log.Fatalf("Could not fetch benchmark branch with code %v and error %v", err, string(out))
}
}
func trackBenchmark() {
gitTrack := exec.Command("git", "branch", "--track", "benchmark", "origin/benchmark")
gitTrack.Dir = "../"
out, err := gitTrack.CombinedOutput()
if err != nil {
log.Fatalf("Could not track benchmark branch with code %v and error %v", err, string(out))
}
}
func checkoutBenchmark() {
gitCheckout := exec.Command("git", "checkout", "benchmark")
gitCheckout.Dir = "../"
out, err := gitCheckout.CombinedOutput()
if err != nil {
log.Fatalf("Could not checkout benchmark branch with code %v and error %v", err, string(out))
}
}
func setId() {
gitConfig := exec.Command("git", "config", "user.name", "benchmark")
gitConfig.Dir = "../"
out, err := gitConfig.CombinedOutput()
if err != nil {
log.Fatalf("Could not set user name with code %v and error %v", err, string(out))
}
gitConfig = exec.Command("git", "config", "user.email", "benchmark@smpptester")
gitConfig.Dir = "../"
out, err = gitConfig.CombinedOutput()
if err != nil {
log.Fatalf("Could not set user email with code %v and error %v", err, string(out))
}
}
func addResults() {
gitAdd := exec.Command("git", "add", "benchmark/results")
gitAdd.Dir = "../"
out, err := gitAdd.CombinedOutput()
if err != nil {
log.Fatalf("Could not add results with code %v and error %v", err, string(out))
}
}
func commitBenchmark() {
gitCommit := exec.Command("git", "commit", "-am", "Benchmark results")
gitCommit.Dir = "../"
out, err := gitCommit.CombinedOutput()
if err != nil {
log.Fatalf("Could not commit benchmark results with code %v and error %v", err, string(out))
}
}
func pushBenchmark() {
gitPush := exec.Command("git", "push", "origin", "benchmark")
gitPush.Dir = "../"
out, err := gitPush.CombinedOutput()
if err != nil {
log.Fatalf("Could not push benchmark results with code %v and error %v", err, string(out))
}
}

View File

@@ -0,0 +1,12 @@
goos: linux
goarch: amd64
pkg: smpptester/pdu
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
BenchmarkBufferPoolManager-6 13663852 90.76 ns/op
BenchmarkBufferPoolManager_Concurrent-6 9525358 130.7 ns/op
BenchmarkBufferPoolManager_Memory-6 16205492 74.61 ns/op
BenchmarkEncodeWithBufferPool-6 8466584 143.2 ns/op
BenchmarkEncodeWithoutBufferPool-6 6701893 221.0 ns/op
BenchmarkDecodeBufferPool-6 7069999 162.3 ns/op
PASS
ok smpptester/pdu 8.413s

View File

@@ -0,0 +1,12 @@
goos: linux
goarch: amd64
pkg: smpptester/pdu
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
BenchmarkBufferPoolManager-6 16792826 71.92 ns/op
BenchmarkBufferPoolManager_Concurrent-6 9800792 127.2 ns/op
BenchmarkBufferPoolManager_Memory-6 13066466 79.52 ns/op
BenchmarkEncodeWithBufferPool-6 8162733 141.6 ns/op
BenchmarkEncodeWithoutBufferPool-6 6148492 272.3 ns/op
BenchmarkDecodeBufferPool-6 7300572 170.5 ns/op
PASS
ok smpptester/pdu 10.059s

View File

@@ -0,0 +1,12 @@
goos: linux
goarch: amd64
pkg: smpptester/pdu
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
BenchmarkBufferPoolManager-6 433611 2688 ns/op
BenchmarkBufferPoolManager_Concurrent-6 1467840 683.2 ns/op
BenchmarkBufferPoolManager_Memory-6 497097 2389 ns/op
BenchmarkEncode-6 8964351 148.5 ns/op
BenchmarkEncodeInto-6 371218869 2.987 ns/op
BenchmarkDecode-6 1000000000 0.2681 ns/op
PASS
ok smpptester/pdu 7.851s

View File

@@ -0,0 +1,13 @@
goos: linux
goarch: amd64
pkg: smpptester/pdu
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
BenchmarkBufferPoolManager-6 457300 2609 ns/op
BenchmarkBufferPoolManager_Concurrent-6 2050396 561.0 ns/op
BenchmarkBufferPoolManager_Memory-6 504205 2242 ns/op
BenchmarkEncode-6 27733428 44.06 ns/op
BenchmarkEncodeWithBufferPool-6 27795820 42.97 ns/op
BenchmarkEncodeInto-6 397897166 3.004 ns/op
BenchmarkDecode-6 1000000000 0.2653 ns/op
PASS
ok smpptester/pdu 10.935s

View File

@@ -0,0 +1,27 @@
? smpptester [no test files]
? smpptester/benchmark [no test files]
? smpptester/client [no test files]
goos: linux
goarch: amd64
pkg: smpptester/encoding
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
BenchmarkGSM7EncodeSimpleASCIIString-6 19589385 67.23 ns/op
BenchmarkGSM7EncodeComplexASCIIString-6 3757233 288.3 ns/op
BenchmarkGSM7EncodeComplex8nASCIIString-6 2576941 532.0 ns/op
BenchmarkGSM7EncodeComplex8nASCIIStringPrealloc-6 2611428 460.3 ns/op
PASS
ok smpptester/encoding 6.483s
? smpptester/lua [no test files]
goos: linux
goarch: amd64
pkg: smpptester/pdu
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
BenchmarkBufferPoolManager-6 16908814 71.62 ns/op
BenchmarkBufferPoolManager_Concurrent-6 8895826 125.1 ns/op
BenchmarkBufferPoolManager_Memory-6 13243089 80.48 ns/op
BenchmarkEncodeWithBufferPool-6 8433070 139.2 ns/op
BenchmarkEncodeWithoutBufferPool-6 7134577 248.2 ns/op
BenchmarkDecodeBufferPool-6 6587127 166.0 ns/op
PASS
ok smpptester/pdu 8.296s
? smpptester/server [no test files]

View File

@@ -0,0 +1,27 @@
? smpptester [no test files]
? smpptester/benchmark [no test files]
? smpptester/client [no test files]
goos: linux
goarch: amd64
pkg: smpptester/encoding
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
BenchmarkGSM7EncodeSimpleASCIIString-6 14935776 76.91 ns/op
BenchmarkGSM7EncodeComplexASCIIString-6 3364554 353.1 ns/op
BenchmarkGSM7EncodeComplex8nASCIIString-6 1840044 591.6 ns/op
BenchmarkGSM7EncodeComplex8nASCIIStringPrealloc-6 2081725 520.4 ns/op
PASS
ok smpptester/encoding 6.270s
? smpptester/lua [no test files]
goos: linux
goarch: amd64
pkg: smpptester/pdu
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
BenchmarkBufferPoolManager-6 16804492 75.91 ns/op
BenchmarkBufferPoolManager_Concurrent-6 11214482 134.8 ns/op
BenchmarkBufferPoolManager_Memory-6 15180892 71.99 ns/op
BenchmarkEncodeWithBufferPool-6 8587657 139.1 ns/op
BenchmarkEncodeWithoutBufferPool-6 5282655 223.4 ns/op
BenchmarkDecodeBufferPool-6 6659464 170.4 ns/op
PASS
ok smpptester/pdu 10.264s
? smpptester/server [no test files]

View File

@@ -0,0 +1,27 @@
? smpptester [no test files]
? smpptester/benchmark [no test files]
? smpptester/client [no test files]
goos: linux
goarch: amd64
pkg: smpptester/encoding
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
BenchmarkGSM7EncodeSimpleASCIIString-6 17371128 63.88 ns/op
BenchmarkGSM7EncodeComplexASCIIString-6 4210346 332.9 ns/op
BenchmarkGSM7EncodeComplex8nASCIIString-6 2748054 517.4 ns/op
BenchmarkGSM7EncodeComplex8nASCIIStringPrealloc-6 2576667 469.9 ns/op
PASS
ok smpptester/encoding 6.462s
? smpptester/lua [no test files]
goos: linux
goarch: amd64
pkg: smpptester/pdu
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
BenchmarkBufferPoolManager-6 16765892 70.02 ns/op
BenchmarkBufferPoolManager_Concurrent-6 9238833 119.7 ns/op
BenchmarkBufferPoolManager_Memory-6 15343066 75.22 ns/op
BenchmarkEncodeWithBufferPool-6 8445075 139.0 ns/op
BenchmarkEncodeWithoutBufferPool-6 5464392 274.3 ns/op
BenchmarkDecodeBufferPool-6 6709528 165.3 ns/op
PASS
ok smpptester/pdu 8.739s
? smpptester/server [no test files]

View File

@@ -0,0 +1,27 @@
? smpptester [no test files]
? smpptester/benchmark [no test files]
? smpptester/client [no test files]
goos: linux
goarch: amd64
pkg: smpptester/encoding
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
BenchmarkGSM7EncodeSimpleASCIIString-6 18785672 65.00 ns/op
BenchmarkGSM7EncodeComplexASCIIString-6 4313766 332.7 ns/op
BenchmarkGSM7EncodeComplex8nASCIIString-6 2686035 520.9 ns/op
BenchmarkGSM7EncodeComplex8nASCIIStringPrealloc-6 2613550 461.0 ns/op
PASS
ok smpptester/encoding 6.548s
? smpptester/lua [no test files]
goos: linux
goarch: amd64
pkg: smpptester/pdu
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
BenchmarkBufferPoolManager-6 16675815 69.97 ns/op
BenchmarkBufferPoolManager_Concurrent-6 9622903 120.8 ns/op
BenchmarkBufferPoolManager_Memory-6 15080410 84.43 ns/op
BenchmarkEncodeWithBufferPool-6 8589709 138.9 ns/op
BenchmarkEncodeWithoutBufferPool-6 6965332 228.4 ns/op
BenchmarkDecodeBufferPool-6 7003172 165.4 ns/op
PASS
ok smpptester/pdu 8.384s
? smpptester/server [no test files]

View File

@@ -0,0 +1,12 @@
goos: linux
goarch: amd64
pkg: smpptester/pdu
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
BenchmarkBufferPoolManager-6 15687109 91.05 ns/op
BenchmarkBufferPoolManager_Concurrent-6 11083287 128.9 ns/op
BenchmarkBufferPoolManager_Memory-6 16576201 74.12 ns/op
BenchmarkEncodeWithBufferPool-6 7868800 138.1 ns/op
BenchmarkEncodeWithoutBufferPool-6 6833810 221.2 ns/op
BenchmarkDecodeBufferPool-6 7195328 166.0 ns/op
PASS
ok smpptester/pdu 8.763s

View File

@@ -0,0 +1,12 @@
goos: linux
goarch: amd64
pkg: smpptester/pdu
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
BenchmarkBufferPoolManager-6 14983336 90.65 ns/op
BenchmarkBufferPoolManager_Concurrent-6 10671728 124.4 ns/op
BenchmarkBufferPoolManager_Memory-6 16471220 75.06 ns/op
BenchmarkEncodeWithBufferPool-6 8545360 139.1 ns/op
BenchmarkEncodeWithoutBufferPool-6 6794032 224.7 ns/op
BenchmarkDecodeBufferPool-6 6737445 165.3 ns/op
PASS
ok smpptester/pdu 8.681s

View File

@@ -0,0 +1,28 @@
? smpptester [no test files]
? smpptester/benchmark [no test files]
? smpptester/client [no test files]
goos: linux
goarch: amd64
pkg: smpptester/encoding
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
BenchmarkGSM7EncodeSimpleASCIIString-6 17190692 63.88 ns/op
BenchmarkGSM7EncodeComplexASCIIString-6 3987712 302.8 ns/op
BenchmarkGSM7EncodeComplex8nASCIIString-6 2563090 523.2 ns/op
BenchmarkGSM7EncodeComplex8nASCIIStringPrealloc-6 2615463 462.5 ns/op
PASS
ok smpptester/encoding 6.235s
? smpptester/lua [no test files]
goos: linux
goarch: amd64
pkg: smpptester/pdu
cpu: Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
BenchmarkBufferPoolManager-6 16913668 70.22 ns/op
BenchmarkBufferPoolManager_Concurrent-6 9289608 129.8 ns/op
BenchmarkBufferPoolManager_Memory-6 13000988 83.78 ns/op
BenchmarkEncodeWithBufferPool-6 7950153 137.7 ns/op
BenchmarkEncodeWithoutBufferPool-6 6290456 172.9 ns/op
BenchmarkDecodeBufferPool-6 7194349 162.7 ns/op
PASS
ok smpptester/pdu 7.717s
? smpptester/server [no test files]
? smpptester/utils [no test files]

View File

@@ -3,7 +3,7 @@ const smpp = require("smpp");
let message_id = 0;
const session = smpp.connect(
{
url: "smpp://0.0.0.0:6001",
url: "smpp://0.0.0.0:2775",
auto_enquire_link_period: 10000,
debug: true,
},

View File

@@ -3,4 +3,4 @@ package pdu
type (
ALERT_NOTIFICATION struct{}
ALERT_NOTIFICATION_RESP struct{}
)
)

View File

@@ -2,7 +2,7 @@ package pdu
type (
BIND struct {
PDU_HEADER
header PDU_HEADER
system_id string
password string
system_type string
@@ -12,7 +12,7 @@ type (
address_range string
}
BIND_RESP struct {
PDU_HEADER
header PDU_HEADER
system_id string
sc_interface_version uint8
}
@@ -34,11 +34,11 @@ type (
BIND_TRANSCIEVER_RESP struct {
BIND_RESP
}
UNBIND struct {
PDU_HEADER
}
UNBIND_RESP struct {
PDU_HEADER
}
)
)

58
pdu/bufpool.go Normal file
View File

@@ -0,0 +1,58 @@
package pdu
import (
"sync"
)
type BufferPoolManager struct {
pools map[uint]*sync.Pool
mu sync.RWMutex
}
func NewBufferPoolManager() *BufferPoolManager {
return &BufferPoolManager{
pools: make(map[uint]*sync.Pool),
}
}
func (bpm *BufferPoolManager) Get(size uint) *[]uint8 {
bpm.mu.RLock()
pool, exists := bpm.pools[size]
bpm.mu.RUnlock()
if !exists {
bpm.mu.Lock()
// Double-check if another goroutine added the pool while we were waiting
pool, exists = bpm.pools[size]
if !exists {
pool = &sync.Pool{
New: func() interface{} {
buf := make([]uint8, size)
return &buf
},
}
bpm.pools[size] = pool
}
bpm.mu.Unlock()
}
return pool.Get().(*[]uint8)
}
func (bpm *BufferPoolManager) Put(buf *[]uint8) {
size := uint(len(*buf))
bpm.mu.RLock()
pool, exists := bpm.pools[size]
bpm.mu.RUnlock()
if !exists {
return
}
// Clear buffer
for i := range *buf {
(*buf)[i] = 0
}
pool.Put(buf)
}

207
pdu/bufpool_test.go Normal file
View File

@@ -0,0 +1,207 @@
package pdu
import (
"sync"
"testing"
"time"
)
func TestRetrieveBufferOfRequestedSize(t *testing.T) {
bpm := NewBufferPoolManager()
size := 1024
buffer := bpm.Get(uint(size))
if buffer == nil {
t.Fatalf("Expected buffer, got nil")
}
if len(*buffer) != size {
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
}
}
func TestRequestBufferSizeZero(t *testing.T) {
bpm := NewBufferPoolManager()
size := 0
buffer := bpm.Get(uint(size))
if buffer == nil {
t.Fatalf("Expected buffer, got nil")
}
if len(*buffer) != size {
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
}
}
func TestConcurrentAccessToBufferPool(t *testing.T) {
bpm := NewBufferPoolManager()
size := 1024
var wg sync.WaitGroup
numRoutines := 100
for i := 0; i < numRoutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
buffer := bpm.Get(uint(size))
if buffer == nil {
t.Errorf("Expected buffer, got nil")
}
if len(*buffer) != size {
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
}
}()
}
wg.Wait()
}
func TestGetBufferLockUnlock(t *testing.T) {
bpm := NewBufferPoolManager()
size := 1024
buffer := bpm.Get(uint(size))
if buffer == nil {
t.Fatalf("Expected buffer, got nil")
}
if len(*buffer) != size {
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
}
}
func TestVerifyPoolCreationForNewSizes(t *testing.T) {
bpm := NewBufferPoolManager()
size := 512
buffer := bpm.Get(uint(size))
if buffer == nil {
t.Fatalf("Expected buffer, got nil")
}
if len(*buffer) != size {
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
}
}
func TestBufferPoolManagerGetBuffer(t *testing.T) {
bpm := NewBufferPoolManager()
size := 1024
buffer := bpm.Get(uint(size))
if buffer == nil {
t.Fatalf("Expected buffer, got nil")
}
if len(*buffer) != size {
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
}
}
func TestGetBufferWithMultipleSizes(t *testing.T) {
bpm := NewBufferPoolManager()
sizes := []int{512, 1024, 2048}
for _, size := range sizes {
buffer := bpm.Get(uint(size))
if buffer == nil {
t.Fatalf("Expected buffer for size %d, got nil", size)
}
if len(*buffer) != size {
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
}
}
}
func TestGetBufferIsAlwaysZero(t *testing.T) {
bpm := NewBufferPoolManager()
var size uint = 1024 * 64
for i := 0; i < 1000; i++ {
buffer := bpm.Get(size)
if buffer == nil {
t.Fatalf("Expected buffer for size %d, got nil", size)
}
if uint(len(*buffer)) != size {
t.Errorf("Expected buffer size %d, got %d", size, len(*buffer))
}
for _, b := range *buffer {
if b != 0 {
t.Errorf("Expected buffer to be zero, got %d", b)
}
}
bpm.Put(buffer)
}
}
func BenchmarkBufferPoolManager(b *testing.B) {
bpm := NewBufferPoolManager()
bufSize := uint(128 * 1024) // a PDU should not be larger than this... Even this is way too large
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Benchmark Get
buf := bpm.Get(bufSize)
// Benchmark Put
bpm.Put(buf)
}
}
func BenchmarkBufferPoolManager_Concurrent(b *testing.B) {
bpm := NewBufferPoolManager()
bufSize := uint(128 * 1024) // a PDU should not be larger than this... Even this is vway too large
b.ResetTimer()
var wg sync.WaitGroup
concurrency := 1000
for i := 0; i < concurrency; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < b.N/concurrency; j++ {
buf := bpm.Get(bufSize)
bpm.Put(buf)
}
}()
}
wg.Wait()
}
func BenchmarkBufferPoolManager_Memory(b *testing.B) {
bpm := NewBufferPoolManager()
bufSize := uint(128 * 1024) // a PDU should not be larger than this... Even this is vway too large
b.ResetTimer()
var i uint8
buf := bpm.Get(bufSize)
b.StopTimer()
// Simulate some work
time.Sleep(10 * time.Millisecond)
for k := range *buf {
(*buf)[k] = i % 255
}
b.StartTimer()
for i := 0; i < b.N; i++ {
bpm.Put(buf)
buf = bpm.Get(bufSize)
}
}

View File

@@ -2,7 +2,7 @@ package pdu
type (
CANCEL_SM struct {
PDU_HEADER
header PDU_HEADER
service_type string
message_id string
source_addr_ton uint8
@@ -13,6 +13,6 @@ type (
destination_addr string
}
CANCEL_SM_RESP struct {
PDU_HEADER
header PDU_HEADER
}
)
)

View File

@@ -156,4 +156,4 @@ var COMMAND_STATUS = struct {
ESME_RINVOPTPARAMVAL: 0x000000C4, // Invalid Optional Parameter Value
ESME_RDELIVERYFAILURE: 0x000000FE, // Delivery Failure (used for
ESME_RUNKNOWNERR: 0x000000FF, // Unknown Error
}
}

View File

@@ -3,4 +3,4 @@ package pdu
type (
DATA_SM struct{}
DATA_SM_RESP struct{}
)
)

View File

@@ -2,11 +2,11 @@ package pdu
type (
DELIVER_SM struct {
PDU_HEADER
header PDU_HEADER
SUBMIT_SM
}
DELIVER_SM_RESP struct {
PDU_HEADER
header PDU_HEADER
SUBMIT_SM_RESP
}
)
)

View File

@@ -2,9 +2,9 @@ package pdu
type (
ENQUIRE_LINK struct {
PDU_HEADER
header PDU_HEADER
}
ENQUIRE_LINK_RESP struct {
PDU_HEADER
header PDU_HEADER
}
)
)

3
pdu/global.go Normal file
View File

@@ -0,0 +1,3 @@
package pdu
var ByteBufferPool = NewBufferPoolManager()

View File

@@ -10,6 +10,7 @@ type (
EncodeInto(*[]uint8)
Encode() []uint8
Decode([]uint8)
// Size in bytes
Size() uint32
}
@@ -21,17 +22,45 @@ type (
}
GENERIC_NACK struct {
PDU_HEADER
header PDU_HEADER
}
)
func (p *PDU_HEADER) Encode() ([]uint8, error) {
buf := make([]uint8, 16)
binary.BigEndian.PutUint32(buf[0:4], p.command_length)
binary.BigEndian.PutUint32(buf[4:8], p.command_id)
binary.BigEndian.PutUint32(buf[8:12], p.command_status)
binary.BigEndian.PutUint32(buf[12:16], p.sequence_number)
return buf, nil
// Hmm the header can be partially encoded
// As in command_status can just be null
// So not 0s, null, non existent, not encoded
// So we'll have to maybe set them to nil or something...
// Wireshark dump of a submit_sm
// See header: 00 00 00 3e (size, 62 bytes), 00 00 00 04 (command_id), status and sequence number are 0
// So they are not encoded
// Here the header is 8 bytes, not 16
// 0000 02 00 00 00 45 00 00 66 6f ff 40 00 80 06 00 00 ....E..fo.@.....
// 0010 7f 00 00 01 7f 00 00 01 31 4f 0a d7 e8 b0 b1 b5 ........1O......
// 0020 3e 91 d9 dc 50 18 20 fa 2a 1f 00 00 00 00 00 3e >...P. .*......>
// 0030 00 00 00 04 00 00 00 00 00 00 00 02 00 00 00 73 ...............s
// 0040 6d 70 70 5f 74 65 73 74 5f 31 00 00 00 31 32 33 mpp_test_1...123
// 0050 31 32 33 31 32 33 31 32 33 00 00 00 00 00 00 00 123123123.......
// 0060 00 c0 00 06 48 65 6c 6c 6f 21 ....Hello!
// I AM NOT RIGHT
// Beyond the size and command_id, command_status and sequence_number here are:
// 00 00 00 00 (0) 00 00 00 02 (2)
//
// Another example - bind_transciever:
// 0000 02 00 00 00 45 00 00 47 6f fb 40 00 80 06 00 00 ....E..Go.@.....
// 0010 7f 00 00 01 7f 00 00 01 31 4f 0a d7 e8 b0 b1 96 ........1O......
// 0020 3e 91 d9 cb 50 18 20 fa df ab 00 00 00 00 00 1f >...P. .........
// 0030 00 00 00 09 00 00 00 00 00 00 00 01 74 65 73 74 ............test
// 0040 00 74 65 73 74 00 00 50 00 00 00 .test..P...
// 00 00 00 1f - 00 00 00 09 - 00 00 00 00 - 00 00 00 01 (header)
// 74 65 73 74 00 - 74 65 73 74 00 - 00 50 00 00 00
// ^system_id ^password ^interface_version
// What are the other 0s?
// Don't know
func (p *PDU_HEADER) Encode() (*[]uint8, error) {
buf := ByteBufferPool.Get(uint(p.Size()))
err := p.EncodeInto(buf)
return buf, err
}
func (p *PDU_HEADER) EncodeInto(buf *[]uint8) error {
if buf == nil {
@@ -65,4 +94,4 @@ func (p *PDU_HEADER) Decode(data []uint8) error {
}
func (p *PDU_HEADER) Size() uint32 {
return 16
}
}

View File

@@ -20,8 +20,8 @@ func TestEncodeReturnsByteSliceOfLength16(t *testing.T) {
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if len(result) != 16 {
t.Errorf("Expected byte slice of length 16, got %d", len(result))
if len(*result) != 16 {
t.Errorf("Expected byte slice of length 16, got %d", len(*result))
}
}
@@ -38,9 +38,9 @@ func TestEncodeHandlesZeroValues(t *testing.T) {
t.Errorf("Expected no error, got %v", err)
}
expected := make([]uint8, 16)
for i, v := range result {
for i, v := range *result {
if v != expected[i] {
t.Errorf("Expected byte slice with zero values, got %v", result)
t.Errorf("Expected byte slice with zero values, got %v", *result)
break
}
}
@@ -60,9 +60,9 @@ func TestEncodeEncodesProperly(t *testing.T) {
}
expected := []uint8{0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4}
for i, v := range result {
for i, v := range *result {
if v != expected[i] {
t.Errorf("Expected byte slice with values %v, got %v", expected, result)
t.Errorf("Expected byte slice with values %v, got %v", expected, *result)
break
}
}
@@ -82,9 +82,9 @@ func TestEncodeEncodesProperlyComplex(t *testing.T) {
}
expected := []uint8{0, 204, 224, 36, 0, 3, 225, 144, 2, 31, 188, 88, 28, 103, 122, 35}
for i, v := range result {
for i, v := range *result {
if v != expected[i] {
t.Errorf("Expected byte slice with values %v, got %v", expected, result)
t.Errorf("Expected byte slice with values %v, got %v", expected, *result)
break
}
}
@@ -292,6 +292,7 @@ func TestEncodeIntoWithBoundaryValues(t *testing.T) {
}
}
// region decode
func TestDecodeHandlesShortByteSlice(t *testing.T) {
var p PDU_HEADER
@@ -448,4 +449,44 @@ func TestSizeReturns16(t *testing.T) {
if p.Size() != 16 {
t.Errorf("Expected size to be 16, got %d", p.Size())
}
}
// region benchmarks
// With buffer pool
func BenchmarkEncode(b *testing.B) {
p := &PDU_HEADER{
command_length: 16,
command_id: 1,
command_status: 0,
sequence_number: 12345,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
p.Encode()
}
}
// Without buffer pool
func BenchmarkEncodeInto(b *testing.B) {
p := &PDU_HEADER{
command_length: 16,
command_id: 1,
command_status: 0,
sequence_number: 12345,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf := make([]uint8, 16)
p.EncodeInto(&buf)
}
}
func BenchmarkDecode(b *testing.B) {
p := &PDU_HEADER{}
data := []uint8{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}
b.ResetTimer()
for i := 0; i < b.N; i++ {
p.Decode(data)
}
}

View File

@@ -3,4 +3,4 @@ package pdu
type (
QUERY_SM struct{}
QUERY_SM_RESP struct{}
)
)

View File

@@ -3,4 +3,4 @@ package pdu
type (
REPLACE_SM struct{}
REPLACE_SM_RESP struct{}
)
)

View File

@@ -1,8 +1,10 @@
package pdu
import "fmt"
type (
SUBMIT_SM struct {
PDU_HEADER
header PDU_HEADER
service_type string
source_addr_ton uint8
source_addr_npi uint8
@@ -51,20 +53,52 @@ type (
// ussd_service_op uint8
}
SUBMIT_SM_RESP struct {
PDU_HEADER
header PDU_HEADER
message_id string
}
SUBMIT_MULTI struct{}
SUBMIT_MULTI_RESP struct{}
)
func (p *SUBMIT_SM) Encode() []byte {
return []byte{}
func (p *SUBMIT_SM) Encode() (*[]byte, error) {
buf := ByteBufferPool.Get(uint(p.Size()))
err := p.EncodeInto(buf)
return buf, err
}
func (p *SUBMIT_SM) EncodeInto(buf *[]byte) {
func (p *SUBMIT_SM) EncodeInto(buf *[]byte) error {
if buf == nil {
return fmt.Errorf("cannot encode SUBMIT_SM, buffer is nil")
}
if len(*buf) < int(p.Size()) {
return fmt.Errorf("cannot encode SUBMIT_SM, buffer too small (%d, required %d)", len(*buf), p.Size())
}
p.header.EncodeInto(buf)
return nil
}
func (p *SUBMIT_SM) Decode(data []byte) {
}
func (p *SUBMIT_SM) Size() uint32 {
return 0
var size uint32
size += p.header.Size()
size += uint32(len(p.service_type) * 1)
size += 1 // source_addr_ton
size += 1 // source_addr_npi
size += uint32(len(p.source_addr) * 1)
size += 1 // dest_addr_ton
size += 1 // dest_addr_npi
size += uint32(len(p.destination_addr) * 1)
size += 1 // esm_class
size += 1 // protocol_id
size += 1 // priority_flag
size += uint32(len(p.schedule_delivery_time) * 1)
size += uint32(len(p.validity_period) * 1)
size += 1 // registered_delivery
size += 1 // replace_if_present
size += 1 // data_coding
size += 1 // sm_default_msg_id
size += 1 // sm_length
size += uint32(len(p.short_message) * 1)
return size
}

55
pdu/submit_test.go Normal file
View File

@@ -0,0 +1,55 @@
package pdu
import (
"testing"
)
// region size
func TestCalculateSizeTypicalInstance(t *testing.T) {
p := &SUBMIT_SM{
service_type: "test_service",
source_addr: "12345",
destination_addr: "67890",
schedule_delivery_time: "",
validity_period: "",
short_message: "Hello, World!",
}
expectedSize := uint32(16 + len(p.service_type) + 1 + 1 + len(p.source_addr) + 1 + 1 + len(p.destination_addr) + 1 + 1 + 1 + len(p.schedule_delivery_time) + len(p.validity_period) + 1 + 1 + 1 + 1 + 1 + len(p.short_message))
actualSize := p.Size()
if actualSize != expectedSize {
t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
}
}
func TestCalculateSizeMaxLengths(t *testing.T) {
maxLen := 255
p := &SUBMIT_SM{
service_type: string(make([]byte, maxLen)),
source_addr: string(make([]byte, maxLen)),
destination_addr: string(make([]byte, maxLen)),
schedule_delivery_time: string(make([]byte, maxLen)),
validity_period: string(make([]byte, maxLen)),
short_message: string(make([]byte, maxLen)),
}
expectedSize := uint32(16 + maxLen + 1 + 1 + maxLen + 1 + 1 + maxLen + 1 + 1 + 1 + maxLen + maxLen + 1 + 1 + 1 + 1 + 1 + maxLen)
actualSize := p.Size()
if actualSize != expectedSize {
t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
}
}
func TestHandlesEmptyStringsForAllStringFields(t *testing.T) {
p := &SUBMIT_SM{
service_type: "",
source_addr: "",
destination_addr: "",
schedule_delivery_time: "",
validity_period: "",
short_message: "",
}
expectedSize := uint32(16 + len(p.service_type) + 1 + 1 + len(p.source_addr) + 1 + 1 + len(p.destination_addr) + 1 + 1 + 1 + len(p.schedule_delivery_time) + len(p.validity_period) + 1 + 1 + 1 + 1 + 1 + len(p.short_message))
actualSize := p.Size()
if actualSize != expectedSize {
t.Errorf("Expected size %d, but got %d", expectedSize, actualSize)
}
}