From 1708898c2dc48fe0aeb780dff640b00f9dd9c204 Mon Sep 17 00:00:00 2001 From: Arpad Ryszka Date: Sat, 28 Feb 2026 14:56:45 +0100 Subject: [PATCH] bench --- lib_test.go | 296 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 264 insertions(+), 32 deletions(-) diff --git a/lib_test.go b/lib_test.go index 1f2d9c9..b39c44a 100644 --- a/lib_test.go +++ b/lib_test.go @@ -172,8 +172,7 @@ func (w writerOnly) Write(p []byte) (int, error) { func TestBenchmarkThroughput(t *testing.T) { p := buffer.NoPool(0) - dst := bytes.NewBuffer(nil) - wo := writerOnly{dst} + wo := writerOnly{io.Discard} src := &gen{max: 1 << 18} r := buffer.BufferedReader(src, buffer.Options{Pool: p}) ro := readerOnly{r} @@ -184,8 +183,7 @@ func TestBenchmarkThroughput(t *testing.T) { } func TestBenchmarkThroughputCompare(t *testing.T) { - dst := bytes.NewBuffer(nil) - wo := writerOnly{dst} + wo := writerOnly{io.Discard} src := &gen{max: 1 << 18} r := bufio.NewReader(src) ro := readerOnly{r} @@ -197,33 +195,28 @@ func TestBenchmarkThroughputCompare(t *testing.T) { func BenchmarkThroughput(b *testing.B) { p := buffer.NoPool(0) - dst := bytes.NewBuffer(nil) - wo := writerOnly{dst} + wo := writerOnly{io.Discard} for i := 0; i < b.N; i++ { src := &gen{max: 1 << 18} r := buffer.BufferedReader(src, buffer.Options{Pool: p}) ro := readerOnly{r} - dst.Reset() io.Copy(wo, ro) } } func BenchmarkThroughputCompare(b *testing.B) { - dst := bytes.NewBuffer(nil) - wo := writerOnly{dst} + wo := writerOnly{io.Discard} for i := 0; i < b.N; i++ { src := &gen{max: 1 << 18} r := bufio.NewReader(src) ro := readerOnly{r} - dst.Reset() io.Copy(wo, ro) } } -func TestThroughputPooled(t *testing.T) { +func TestBenchmarkThroughputPooled(t *testing.T) { p := &foreverPool{allocSize: 1 << 12} - dst := bytes.NewBuffer(nil) - wo := writerOnly{dst} + wo := writerOnly{io.Discard} src := &gen{max: 1 << 18} r := buffer.BufferedReader(src, buffer.Options{Pool: p}) ro := readerOnly{r} @@ -233,9 +226,8 @@ func TestThroughputPooled(t *testing.T) { } } -func TestThroughputPooledCompare(t *testing.T) { - dst := bytes.NewBuffer(nil) - wo := writerOnly{dst} +func TestBenchmarkThroughputPooledCompare(t *testing.T) { + wo := writerOnly{io.Discard} r := bufio.NewReader(nil) src := &gen{max: 1 << 18} r.Reset(src) @@ -248,37 +240,32 @@ func TestThroughputPooledCompare(t *testing.T) { func BenchmarkThroughputPooled(b *testing.B) { p := &foreverPool{allocSize: 1 << 12} - dst := bytes.NewBuffer(nil) - wo := writerOnly{dst} + wo := writerOnly{io.Discard} for i := 0; i < b.N; i++ { src := &gen{max: 1 << 18} r := buffer.BufferedReader(src, buffer.Options{Pool: p}) ro := readerOnly{r} - dst.Reset() io.Copy(wo, ro) } } func BenchmarkThroughputPooledCompare(b *testing.B) { - dst := bytes.NewBuffer(nil) - wo := writerOnly{dst} + wo := writerOnly{io.Discard} r := bufio.NewReader(nil) for i := 0; i < b.N; i++ { src := &gen{max: 1 << 18} r.Reset(src) ro := readerOnly{r} - dst.Reset() io.Copy(wo, ro) } } -func TestThroughputPooledParallel(t *testing.T) { +func TestBenchmarkThroughputPooledParallel(t *testing.T) { p := newSyncedForeverPool(func() []byte { return make([]byte, 1<<12) }) - dst := bytes.NewBuffer(nil) - wo := writerOnly{dst} + wo := writerOnly{io.Discard} src := &gen{max: 1 << 18} r := buffer.BufferedReader(src, buffer.Options{Pool: p}) ro := readerOnly{r} @@ -288,13 +275,12 @@ func TestThroughputPooledParallel(t *testing.T) { } } -func TestThroughputPooledParallelCompare(t *testing.T) { +func TestBenchmarkThroughputPooledParallelCompare(t *testing.T) { p := newSyncedForeverPool(func() *bufio.Reader { return bufio.NewReader(nil) }) - dst := bytes.NewBuffer(nil) - wo := writerOnly{dst} + wo := writerOnly{io.Discard} r, err := p.Get() if err != nil { t.Fatal(err) @@ -315,13 +301,13 @@ func BenchmarkThroughputPooledParallel(b *testing.B) { }) b.ResetTimer() + b.SetParallelism(128) b.RunParallel(func(pb *testing.PB) { for pb.Next() { src := &gen{max: 1 << 18} r := buffer.BufferedReader(src, buffer.Options{Pool: p}) ro := readerOnly{r} - dst := bytes.NewBuffer(nil) - wo := writerOnly{dst} + wo := writerOnly{io.Discard} io.Copy(wo, ro) } }) @@ -333,15 +319,261 @@ func BenchmarkThroughputPooledParallelCompare(b *testing.B) { }) b.ResetTimer() + b.SetParallelism(128) b.RunParallel(func(pb *testing.PB) { for pb.Next() { r, _ := p.Get() src := &gen{max: 1 << 18} r.Reset(src) ro := readerOnly{r} - dst := bytes.NewBuffer(nil) - wo := writerOnly{dst} + wo := writerOnly{io.Discard} io.Copy(wo, ro) } }) } + +func TestBenchmarkScan(t *testing.T) { + const delimiterPosition = 1<<17 + 1<<16 - 3 + p := buffer.NoPool(0) + src := &gen{ + max: 1 << 18, + customContentAfter: []int{delimiterPosition}, + customContent: map[int][]byte{delimiterPosition: []byte("123")}, + } + + r := buffer.BufferedReader(src, buffer.Options{Pool: p}) + b, ok, err := r.ReadBytes([]byte{'1'}, 1<<18) + if err != nil { + t.Fatal(err) + } + + if !ok { + t.Fatal("delimiter") + } + + if !bytes.Equal(b, append(generate(delimiterPosition), '1')) { + t.Fatal("content") + } +} + +func TestBenchmarkScanCompare(t *testing.T) { + const delimiterPosition = 1<<17 + 1<<16 - 3 + src := &gen{ + max: 1 << 18, + customContentAfter: []int{delimiterPosition}, + customContent: map[int][]byte{delimiterPosition: []byte("123")}, + } + + r := bufio.NewReader(src) + b, err := r.ReadBytes('1') + if err != nil { + t.Fatal(err) + } + + if !bytes.Equal(b, append(generate(delimiterPosition), '1')) { + t.Fatal("content") + } +} + +func BenchmarkScan(b *testing.B) { + const delimiterPosition = 1<<17 + 1<<16 - 3 + p := buffer.NoPool(0) + for i := 0; i < b.N; i++ { + src := &gen{ + max: 1 << 18, + customContentAfter: []int{delimiterPosition}, + customContent: map[int][]byte{delimiterPosition: []byte("123")}, + } + + r := buffer.BufferedReader(src, buffer.Options{Pool: p}) + r.ReadBytes([]byte{'1'}, 1<<18) + } +} + +func BenchmarkScanCompare(b *testing.B) { + const delimiterPosition = 1<<17 + 1<<16 - 3 + for i := 0; i < b.N; i++ { + src := &gen{ + max: 1 << 18, + customContentAfter: []int{delimiterPosition}, + customContent: map[int][]byte{delimiterPosition: []byte("123")}, + } + + r := bufio.NewReader(src) + r.ReadBytes('1') + } +} + +func TestBenchmarkScanPooled(t *testing.T) { + const delimiterPosition = 1<<17 + 1<<16 - 3 + p := &foreverPool{allocSize: 1 << 12} + src := &gen{ + max: 1 << 18, + customContentAfter: []int{delimiterPosition}, + customContent: map[int][]byte{delimiterPosition: []byte("123")}, + } + + r := buffer.BufferedReader(src, buffer.Options{Pool: p}) + b, ok, err := r.ReadBytes([]byte{'1'}, 1<<18) + if err != nil { + t.Fatal(err) + } + + if !ok { + t.Fatal("delimiter") + } + + if !bytes.Equal(b, append(generate(delimiterPosition), '1')) { + t.Fatal("content") + } +} + +func TestBenchmarkScanPooledCompare(t *testing.T) { + const delimiterPosition = 1<<17 + 1<<16 - 3 + src := &gen{ + max: 1 << 18, + customContentAfter: []int{delimiterPosition}, + customContent: map[int][]byte{delimiterPosition: []byte("123")}, + } + + r := bufio.NewReader(nil) + r.Reset(src) + b, err := r.ReadBytes('1') + if err != nil { + t.Fatal(err) + } + + if !bytes.Equal(b, append(generate(delimiterPosition), '1')) { + t.Fatal("content") + } +} + +func BenchmarkScanPooled(b *testing.B) { + const delimiterPosition = 1<<17 + 1<<16 - 3 + p := &foreverPool{allocSize: 1 << 12} + for i := 0; i < b.N; i++ { + src := &gen{ + max: 1 << 18, + customContentAfter: []int{delimiterPosition}, + customContent: map[int][]byte{delimiterPosition: []byte("123")}, + } + + r := buffer.BufferedReader(src, buffer.Options{Pool: p}) + r.ReadBytes([]byte{'1'}, 1<<18) + } +} + +func BenchmarkScanPooledCompare(b *testing.B) { + const delimiterPosition = 1<<17 + 1<<16 - 3 + r := bufio.NewReader(nil) + for i := 0; i < b.N; i++ { + src := &gen{ + max: 1 << 18, + customContentAfter: []int{delimiterPosition}, + customContent: map[int][]byte{delimiterPosition: []byte("123")}, + } + + r.Reset(src) + r.ReadBytes('1') + } +} + +func TestBenchmarkScanPooledParallel(t *testing.T) { + const delimiterPosition = 1<<17 + 1<<16 - 3 + p := newSyncedForeverPool(func() []byte { + return make([]byte, 1<<12) + }) + + src := &gen{ + max: 1 << 18, + customContentAfter: []int{delimiterPosition}, + customContent: map[int][]byte{delimiterPosition: []byte("123")}, + } + + r := buffer.BufferedReader(src, buffer.Options{Pool: p}) + b, ok, err := r.ReadBytes([]byte{'1'}, 1<<18) + if err != nil { + t.Fatal(err) + } + + if !ok { + t.Fatal("delimiter") + } + + if !bytes.Equal(b, append(generate(delimiterPosition), '1')) { + t.Fatal("content") + } +} + +func TestBenchmarkScanPooledParallelCompare(t *testing.T) { + const delimiterPosition = 1<<17 + 1<<16 - 3 + p := newSyncedForeverPool(func() *bufio.Reader { + return bufio.NewReader(nil) + }) + + src := &gen{ + max: 1 << 18, + customContentAfter: []int{delimiterPosition}, + customContent: map[int][]byte{delimiterPosition: []byte("123")}, + } + + r, err := p.Get() + if err != nil { + t.Fatal(err) + } + + r.Reset(src) + b, err := r.ReadBytes('1') + if err != nil { + t.Fatal(err) + } + + if !bytes.Equal(b, append(generate(delimiterPosition), '1')) { + t.Fatal("content") + } +} + +func BenchmarkScanPooledParallel(b *testing.B) { + const delimiterPosition = 1<<17 + 1<<16 - 3 + p := newSyncedForeverPool(func() []byte { + return make([]byte, 1<<12) + }) + + b.ResetTimer() + b.SetParallelism(128) + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + src := &gen{ + max: 1 << 18, + customContentAfter: []int{delimiterPosition}, + customContent: map[int][]byte{delimiterPosition: []byte("123")}, + } + + r := buffer.BufferedReader(src, buffer.Options{Pool: p}) + r.ReadBytes([]byte{'1'}, 1<<18) + } + }) +} + +func BenchmarkScanPooledParallelCompare(b *testing.B) { + const delimiterPosition = 1<<17 + 1<<16 - 3 + p := newSyncedForeverPool(func() *bufio.Reader { + return bufio.NewReader(nil) + }) + + b.ResetTimer() + b.SetParallelism(128) + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + src := &gen{ + max: 1 << 18, + customContentAfter: []int{delimiterPosition}, + customContent: map[int][]byte{delimiterPosition: []byte("123")}, + } + + r, _ := p.Get() + r.Reset(src) + r.ReadBytes('1') + } + }) +}