1
0

fix/improve benchmarks

This commit is contained in:
Arpad Ryszka 2026-02-23 00:22:19 +01:00
parent 8048927df8
commit 21e75fcb0b
2 changed files with 119 additions and 8 deletions

4
lib.go
View File

@ -59,6 +59,10 @@ var (
// DefultPool initializes a synchronized pool that stores and returns byte slices of allocSize length. It can be
// used with multiple readers concurrently.
func DefaultPool(allocSize int) Pool {
if allocSize == 0 {
allocSize = 1 << 12
}
return newPool(allocSize)
}

View File

@ -6,6 +6,7 @@ import (
"code.squareroundforest.org/arpio/buffer"
"errors"
"io"
"runtime"
"testing"
)
@ -170,7 +171,113 @@ func (w writerOnly) Write(p []byte) (int, error) {
return w.in.Write(p)
}
func TestBenchmarkThroughput(t *testing.T) {
p := buffer.DefaultPool(0)
dst := bytes.NewBuffer(nil)
wo := writerOnly{dst}
src := &gen{max: 1 << 18}
r := buffer.BufferedReader(src, buffer.Options{Pool: p})
ro := readerOnly{r}
dst.Reset()
if n, err := io.Copy(wo, ro); n != 1<<18 || err != nil {
t.Fatal(n, err)
}
}
func TestBenchmarkCompare(t *testing.T) {
dst := bytes.NewBuffer(nil)
wo := writerOnly{dst}
src := &gen{max: 1 << 18}
r := bufio.NewReader(src)
ro := readerOnly{r}
dst.Reset()
if n, err := io.Copy(wo, ro); n != 1<<18 || err != nil {
t.Fatal(n, err)
}
}
func TestBenchmarkThroughputSTDLib(t *testing.T) {
p := buffer.DefaultPool(0)
dst := bytes.NewBuffer(nil)
wo := writerOnly{dst}
src := bytes.NewBuffer(make([]byte, 1<<18))
r := buffer.BufferedReader(src, buffer.Options{Pool: p})
ro := readerOnly{r}
dst.Reset()
if n, err := io.Copy(wo, ro); n != 1<<18 || err != nil {
t.Fatal(n, err)
}
}
func TestBenchmarkCompareSTDLib(t *testing.T) {
dst := bytes.NewBuffer(nil)
wo := writerOnly{dst}
src := bytes.NewBuffer(make([]byte, 1<<18))
r := bufio.NewReader(src)
ro := readerOnly{r}
dst.Reset()
if n, err := io.Copy(wo, ro); n != 1<<18 || err != nil {
t.Fatal(n, err)
}
}
func BenchmarkThroughput(b *testing.B) {
p := buffer.DefaultPool(0)
dst := bytes.NewBuffer(nil)
wo := writerOnly{dst}
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 BenchmarkCompare(b *testing.B) {
dst := bytes.NewBuffer(nil)
wo := writerOnly{dst}
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 BenchmarkThroughputP(b *testing.B) {
p := buffer.DefaultPool(0)
b.SetParallelism(runtime.GOMAXPROCS(-1))
b.RunParallel(func(pb *testing.PB) {
dst := bytes.NewBuffer(nil)
wo := writerOnly{dst}
for pb.Next() {
src := &gen{max: 1 << 18}
r := buffer.BufferedReader(src, buffer.Options{Pool: p})
ro := readerOnly{r}
dst.Reset()
io.Copy(wo, ro)
}
})
}
func BenchmarkCompareP(b *testing.B) {
b.SetParallelism(runtime.GOMAXPROCS(-1))
b.RunParallel(func(pb *testing.PB) {
dst := bytes.NewBuffer(nil)
wo := writerOnly{dst}
for pb.Next() {
src := &gen{max: 1 << 18}
r := bufio.NewReader(src)
ro := readerOnly{r}
dst.Reset()
io.Copy(wo, ro)
}
})
}
func BenchmarkThroughputSTDLib(b *testing.B) {
p := buffer.DefaultPool(0)
dst := bytes.NewBuffer(nil)
wo := writerOnly{dst}
@ -183,14 +290,14 @@ func BenchmarkThroughput(b *testing.B) {
}
}
func BenchmarkCompare(b *testing.B) {
dstBuf := bytes.NewBuffer(nil)
dst := writerOnly{dstBuf}
func BenchmarkCompareSTDLib(b *testing.B) {
dst := bytes.NewBuffer(nil)
wo := writerOnly{dst}
for i := 0; i < b.N; i++ {
srcBuf := bytes.NewBuffer(make([]byte, 1<<18))
srcReader := bufio.NewReader(srcBuf)
src := readerOnly{srcReader}
dstBuf.Reset()
io.Copy(dst, src)
src := bytes.NewBuffer(make([]byte, 1<<18))
r := bufio.NewReader(src)
ro := readerOnly{r}
dst.Reset()
io.Copy(wo, ro)
}
}