Rework bufpool to be a little more efficient, I hope

This commit is contained in:
PhatPhuckDave
2024-07-23 14:55:33 +02:00
parent aff4ffe070
commit a9827f10ce
3 changed files with 77 additions and 68 deletions

View File

@@ -6,10 +6,7 @@ import (
)
func TestRetrieveBufferOfRequestedSize(t *testing.T) {
bpm := &BufferPoolManager{
pools: make(map[uint]*sync.Pool),
mu: sync.Mutex{},
}
bpm := NewBufferPoolManager()
size := 1024
buffer := bpm.GetBuffer(uint(size))
@@ -24,10 +21,7 @@ func TestRetrieveBufferOfRequestedSize(t *testing.T) {
}
func TestRequestBufferSizeZero(t *testing.T) {
bpm := &BufferPoolManager{
pools: make(map[uint]*sync.Pool),
mu: sync.Mutex{},
}
bpm := NewBufferPoolManager()
size := 0
buffer := bpm.GetBuffer(uint(size))
@@ -42,10 +36,7 @@ func TestRequestBufferSizeZero(t *testing.T) {
}
func TestConcurrentAccessToBufferPool(t *testing.T) {
bpm := &BufferPoolManager{
pools: make(map[uint]*sync.Pool),
mu: sync.Mutex{},
}
bpm := NewBufferPoolManager()
size := 1024
var wg sync.WaitGroup
@@ -69,10 +60,7 @@ func TestConcurrentAccessToBufferPool(t *testing.T) {
}
func TestGetBufferLockUnlock(t *testing.T) {
bpm := &BufferPoolManager{
pools: make(map[uint]*sync.Pool),
mu: sync.Mutex{},
}
bpm := NewBufferPoolManager()
size := 1024
buffer := bpm.GetBuffer(uint(size))
@@ -87,10 +75,7 @@ func TestGetBufferLockUnlock(t *testing.T) {
}
func TestVerifyPoolCreationForNewSizes(t *testing.T) {
bpm := &BufferPoolManager{
pools: make(map[uint]*sync.Pool),
mu: sync.Mutex{},
}
bpm := NewBufferPoolManager()
size := 512
buffer := bpm.GetBuffer(uint(size))
@@ -105,10 +90,7 @@ func TestVerifyPoolCreationForNewSizes(t *testing.T) {
}
func TestBufferPoolManagerGetBuffer(t *testing.T) {
bpm := &BufferPoolManager{
pools: make(map[uint]*sync.Pool),
mu: sync.Mutex{},
}
bpm := NewBufferPoolManager()
size := 1024
buffer := bpm.GetBuffer(uint(size))
@@ -123,10 +105,7 @@ func TestBufferPoolManagerGetBuffer(t *testing.T) {
}
func TestGetBufferWithMultipleSizes(t *testing.T) {
bpm := &BufferPoolManager{
pools: make(map[uint]*sync.Pool),
mu: sync.Mutex{},
}
bpm := NewBufferPoolManager()
sizes := []int{512, 1024, 2048}
for _, size := range sizes {
@@ -141,3 +120,28 @@ func TestGetBufferWithMultipleSizes(t *testing.T) {
}
}
}
func TestGetBufferIsAlwaysZero(t *testing.T) {
bpm := NewBufferPoolManager()
var size uint = 1024*64
for i := 0; i < 1000; i++ {
buffer := bpm.GetBuffer(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.PutBuffer(buffer)
}
}