diff --git a/Makefile b/Makefile index e98dea1..66616ab 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,9 @@ cover: .cover showcover: .cover go tool cover -html .cover +bench: $(sources) + go test -bench Benchmark -run ^$ + clean: go clean rm .cover diff --git a/lib.go b/lib.go index d20a30a..9b6450c 100644 --- a/lib.go +++ b/lib.go @@ -26,7 +26,9 @@ type Pool interface { // Options provides options for the Reader. type Options struct { - // Pool defines the buffer pool to be used. It defaults to the pool created by DefaultPool(). + // Pool defines the buffer pool to be used. It defaults to the pool created by DefaultPool(). It is + // expected to explicitly set the Pool instance, otherwise, not defining any globals, each Reader + // instance will create its own pool. Pool Pool } diff --git a/lib_test.go b/lib_test.go index 660c5a4..bbb7b16 100644 --- a/lib_test.go +++ b/lib_test.go @@ -1,6 +1,7 @@ package buffer_test import ( + "bufio" "bytes" "code.squareroundforest.org/arpio/buffer" "errors" @@ -150,3 +151,46 @@ func TestLib(t *testing.T) { }) }) } + +// -- bench + +type readerOnly struct { + in io.Reader +} + +type writerOnly struct { + in io.Writer +} + +func (r readerOnly) Read(p []byte) (int, error) { + return r.in.Read(p) +} + +func (w writerOnly) Write(p []byte) (int, error) { + return w.in.Write(p) +} + +func BenchmarkThroughput(b *testing.B) { + p := buffer.DefaultPool(0) + dst := bytes.NewBuffer(nil) + wo := writerOnly{dst} + for i := 0; i < b.N; i++ { + src := bytes.NewBuffer(make([]byte, 1<<18)) + r := buffer.BufferedReader(src, buffer.Options{Pool: p}) + ro := readerOnly{r} + dst.Reset() + io.Copy(wo, ro) + } +} + +func BenchmarkCompare(b *testing.B) { + dstBuf := bytes.NewBuffer(nil) + dst := writerOnly{dstBuf} + 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) + } +}