1
0

document shared pool expected

This commit is contained in:
Arpad Ryszka 2026-02-22 20:55:38 +01:00
parent 160253ae75
commit fcbad4b62f
3 changed files with 50 additions and 1 deletions

View File

@ -20,6 +20,9 @@ cover: .cover
showcover: .cover showcover: .cover
go tool cover -html .cover go tool cover -html .cover
bench: $(sources)
go test -bench Benchmark -run ^$
clean: clean:
go clean go clean
rm .cover rm .cover

4
lib.go
View File

@ -26,7 +26,9 @@ type Pool interface {
// Options provides options for the Reader. // Options provides options for the Reader.
type Options struct { 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 Pool Pool
} }

View File

@ -1,6 +1,7 @@
package buffer_test package buffer_test
import ( import (
"bufio"
"bytes" "bytes"
"code.squareroundforest.org/arpio/buffer" "code.squareroundforest.org/arpio/buffer"
"errors" "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)
}
}