document shared pool expected
This commit is contained in:
parent
160253ae75
commit
fcbad4b62f
3
Makefile
3
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
|
||||
|
||||
4
lib.go
4
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
|
||||
}
|
||||
|
||||
|
||||
44
lib_test.go
44
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)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user