Add benchmarks for bufpool
This commit is contained in:
@@ -3,6 +3,7 @@ package pdu
|
|||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRetrieveBufferOfRequestedSize(t *testing.T) {
|
func TestRetrieveBufferOfRequestedSize(t *testing.T) {
|
||||||
@@ -145,3 +146,62 @@ func TestGetBufferIsAlwaysZero(t *testing.T) {
|
|||||||
bpm.Put(buffer)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user