diff --git a/lib.go b/lib.go index 9b6450c..c7c2ed3 100644 --- a/lib.go +++ b/lib.go @@ -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) } diff --git a/lib_test.go b/lib_test.go index bbb7b16..8b916b1 100644 --- a/lib_test.go +++ b/lib_test.go @@ -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) } }