From 36dcbbc1542469e358c910b875ce88477c6001c7 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave <> Date: Tue, 23 Jul 2024 15:08:07 +0200 Subject: [PATCH] Add benchmarks for bufpool --- pdu/bufpool_test.go | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/pdu/bufpool_test.go b/pdu/bufpool_test.go index fbe9ca0..f1d944d 100644 --- a/pdu/bufpool_test.go +++ b/pdu/bufpool_test.go @@ -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) + } +}