Rework every uint8 to byte and rework every []byte to bytes.Buffer
Some checks failed
Benchmark BufferPool / RunBenchmarks (push) Failing after 17s
Run Tests / Test (push) Failing after 15s

I just learnt that uint8 and byte are the fucking same
And that bytes.Buffer is the thing to use
Idiot idiot idiot...
Live and learn
This commit is contained in:
2024-07-24 18:43:18 +02:00
parent bed69fbfd3
commit f30680c26f
9 changed files with 461 additions and 418 deletions

View File

@@ -10,14 +10,14 @@ func TestRetrieveBufferOfRequestedSize(t *testing.T) {
bpm := NewBufferPoolManager()
size := 1024
buffer := bpm.Get(uint(size))
buffer := bpm.Get(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))
if buffer.Len() != size {
t.Errorf("Expected buffer size %d, got %d", size, buffer.Len())
}
}
@@ -25,14 +25,14 @@ func TestRequestBufferSizeZero(t *testing.T) {
bpm := NewBufferPoolManager()
size := 0
buffer := bpm.Get(uint(size))
buffer := bpm.Get(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))
if buffer.Len() != size {
t.Errorf("Expected buffer size %d, got %d", size, buffer.Len())
}
}
@@ -47,12 +47,12 @@ func TestConcurrentAccessToBufferPool(t *testing.T) {
wg.Add(1)
go func() {
defer wg.Done()
buffer := bpm.Get(uint(size))
buffer := bpm.Get(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))
if buffer.Len() != size {
t.Errorf("Expected buffer size %d, got %d", size, buffer.Len())
}
}()
}
@@ -64,14 +64,14 @@ func TestGetBufferLockUnlock(t *testing.T) {
bpm := NewBufferPoolManager()
size := 1024
buffer := bpm.Get(uint(size))
buffer := bpm.Get(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))
if buffer.Len() != size {
t.Errorf("Expected buffer size %d, got %d", size, buffer.Len())
}
}
@@ -79,14 +79,14 @@ func TestVerifyPoolCreationForNewSizes(t *testing.T) {
bpm := NewBufferPoolManager()
size := 512
buffer := bpm.Get(uint(size))
buffer := bpm.Get(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))
if buffer.Len() != size {
t.Errorf("Expected buffer size %d, got %d", size, buffer.Len())
}
}
@@ -94,14 +94,14 @@ func TestBufferPoolManagerGetBuffer(t *testing.T) {
bpm := NewBufferPoolManager()
size := 1024
buffer := bpm.Get(uint(size))
buffer := bpm.Get(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))
if buffer.Len() != size {
t.Errorf("Expected buffer size %d, got %d", size, buffer.Len())
}
}
@@ -110,14 +110,14 @@ func TestGetBufferWithMultipleSizes(t *testing.T) {
sizes := []int{512, 1024, 2048}
for _, size := range sizes {
buffer := bpm.Get(uint(size))
buffer := bpm.Get(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))
if buffer.Len() != size {
t.Errorf("Expected buffer size %d, got %d", size, buffer.Len())
}
}
}
@@ -125,7 +125,7 @@ func TestGetBufferWithMultipleSizes(t *testing.T) {
func TestGetBufferIsAlwaysZero(t *testing.T) {
bpm := NewBufferPoolManager()
var size uint = 1024 * 64
size := 1024 * 64
for i := 0; i < 1000; i++ {
buffer := bpm.Get(size)
@@ -133,11 +133,11 @@ func TestGetBufferIsAlwaysZero(t *testing.T) {
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))
if buffer.Len() != size {
t.Errorf("Expected buffer size %d, got %d", size, buffer.Len())
}
for _, b := range *buffer {
for _, b := range buffer.Bytes() {
if b != 0 {
t.Errorf("Expected buffer to be zero, got %d", b)
}
@@ -149,7 +149,7 @@ func TestGetBufferIsAlwaysZero(t *testing.T) {
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
bufSize := 128 * 1024 // a PDU should not be larger than this... Even this is way too large
b.ResetTimer()
@@ -163,7 +163,7 @@ func BenchmarkBufferPoolManager(b *testing.B) {
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
bufSize := 128 * 1024 // a PDU should not be larger than this... Even this is way too large
b.ResetTimer()
@@ -186,17 +186,17 @@ func BenchmarkBufferPoolManager_Concurrent(b *testing.B) {
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
bufSize := 128 * 1024 // a PDU should not be larger than this... Even this is way too large
b.ResetTimer()
var i uint8
var i byte
buf := bpm.Get(bufSize)
b.StopTimer()
// Simulate some work
time.Sleep(10 * time.Millisecond)
for k := range *buf {
(*buf)[k] = i % 255
for _ = range buf.Bytes() {
buf.WriteByte(i % 255)
}
b.StartTimer()