fix/improve benchmarks
This commit is contained in:
parent
8048927df8
commit
21e75fcb0b
4
lib.go
4
lib.go
@ -59,6 +59,10 @@ var (
|
|||||||
// DefultPool initializes a synchronized pool that stores and returns byte slices of allocSize length. It can be
|
// DefultPool initializes a synchronized pool that stores and returns byte slices of allocSize length. It can be
|
||||||
// used with multiple readers concurrently.
|
// used with multiple readers concurrently.
|
||||||
func DefaultPool(allocSize int) Pool {
|
func DefaultPool(allocSize int) Pool {
|
||||||
|
if allocSize == 0 {
|
||||||
|
allocSize = 1 << 12
|
||||||
|
}
|
||||||
|
|
||||||
return newPool(allocSize)
|
return newPool(allocSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
123
lib_test.go
123
lib_test.go
@ -6,6 +6,7 @@ import (
|
|||||||
"code.squareroundforest.org/arpio/buffer"
|
"code.squareroundforest.org/arpio/buffer"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -170,7 +171,113 @@ func (w writerOnly) Write(p []byte) (int, error) {
|
|||||||
return w.in.Write(p)
|
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) {
|
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)
|
p := buffer.DefaultPool(0)
|
||||||
dst := bytes.NewBuffer(nil)
|
dst := bytes.NewBuffer(nil)
|
||||||
wo := writerOnly{dst}
|
wo := writerOnly{dst}
|
||||||
@ -183,14 +290,14 @@ func BenchmarkThroughput(b *testing.B) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkCompare(b *testing.B) {
|
func BenchmarkCompareSTDLib(b *testing.B) {
|
||||||
dstBuf := bytes.NewBuffer(nil)
|
dst := bytes.NewBuffer(nil)
|
||||||
dst := writerOnly{dstBuf}
|
wo := writerOnly{dst}
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
srcBuf := bytes.NewBuffer(make([]byte, 1<<18))
|
src := bytes.NewBuffer(make([]byte, 1<<18))
|
||||||
srcReader := bufio.NewReader(srcBuf)
|
r := bufio.NewReader(src)
|
||||||
src := readerOnly{srcReader}
|
ro := readerOnly{r}
|
||||||
dstBuf.Reset()
|
dst.Reset()
|
||||||
io.Copy(dst, src)
|
io.Copy(wo, ro)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user