Add benchmarks for bufpool

This commit is contained in:
PhatPhuckDave
2024-07-23 15:08:07 +02:00
parent 2759185dbb
commit 36dcbbc154

View File

@@ -3,6 +3,7 @@ package pdu
import (
"sync"
"testing"
"time"
)
func TestRetrieveBufferOfRequestedSize(t *testing.T) {
@@ -145,3 +146,62 @@ func TestGetBufferIsAlwaysZero(t *testing.T) {
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)
}
}