Skip to content

Commit 8ea7d61

Browse files
committed
Buffer: add benchmarks for a showdown against the Go standard library
Currently we see ```shell name old time/op new time/op delta WriteByte-8 16.8µs ± 4% 16.7µs ± 9% ~ (p=0.400 n=9+10) BufferFullSmallReads-8 61.1µs ± 6% 46.4µs ± 3% -24.06% (p=0.000 n=10+10) BufferNotEmptyWriteRead-8 417µs ± 3% 182µs ± 5% -56.28% (p=0.000 n=10+10) name old speed new speed delta WriteByte-8 244MB/s ± 3% 245MB/s ± 8% ~ (p=0.400 n=9+10) name old alloc/op new alloc/op delta WriteByte-8 0.00B 0.00B ~ (all equal) BufferFullSmallReads-8 0.00B 0.00B ~ (all equal) BufferNotEmptyWriteRead-8 0.90B ±122% 0.00B -100.00% (p=0.011 n=10+10) name old allocs/op new allocs/op delta WriteByte-8 0.00 0.00 ~ (all equal) BufferFullSmallReads-8 0.00 0.00 ~ (all equal) BufferNotEmptyWriteRead-8 0.00 0.00 ~ (all equal) ```
1 parent 4ac51de commit 8ea7d61

File tree

1 file changed

+51
-12
lines changed

1 file changed

+51
-12
lines changed

buffer_test.go

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package pool
88

99
import (
1010
"bytes"
11+
"io"
1112
"math/rand"
1213
"runtime"
1314
"testing"
@@ -360,8 +361,19 @@ func TestBufferGrowth(t *testing.T) {
360361

361362
func BenchmarkWriteByte(b *testing.B) {
362363
const n = 4 << 10
363-
b.SetBytes(n)
364364
buf := NewBuffer(make([]byte, n))
365+
benchmarkWriteByte(b, buf, n)
366+
}
367+
368+
func BenchmarkWriteByteStdlib(b *testing.B) {
369+
const n = 4 << 10
370+
buf := NewBuffer(make([]byte, n))
371+
benchmarkWriteByte(b, buf, n)
372+
}
373+
374+
func benchmarkWriteByte(b *testing.B, buf bufLike, n int) {
375+
b.SetBytes(int64(n))
376+
b.ResetTimer()
365377
for i := 0; i < b.N; i++ {
366378
buf.Reset()
367379
for i := 0; i < n; i++ {
@@ -372,29 +384,56 @@ func BenchmarkWriteByte(b *testing.B) {
372384

373385
// From Issue 5154.
374386
func BenchmarkBufferNotEmptyWriteRead(b *testing.B) {
387+
benchmarkBufferNotEmptyWriteRead(b, func() bufLike { return &Buffer{} })
388+
}
389+
390+
func BenchmarkBufferNotEmptyWriteReadStdlib(b *testing.B) {
391+
benchmarkBufferNotEmptyWriteRead(b, func() bufLike { return new(bytes.Buffer) })
392+
}
393+
394+
func benchmarkBufferNotEmptyWriteRead(b *testing.B, fn func() bufLike) {
375395
buf := make([]byte, 1024)
396+
bf := fn()
397+
b.ResetTimer()
376398
for i := 0; i < b.N; i++ {
377-
var b Buffer
378-
b.Write(buf[0:1])
399+
bf.Write(buf[0:1])
379400
for i := 0; i < 5<<10; i++ {
380-
b.Write(buf)
381-
b.Read(buf)
401+
bf.Write(buf)
402+
bf.Read(buf)
382403
}
404+
bf.Reset()
383405
}
384406
}
385407

408+
type bufLike interface {
409+
io.ByteWriter
410+
io.ReadWriter
411+
Len() int
412+
Cap() int
413+
Reset()
414+
}
415+
386416
// Check that we don't compact too often. From Issue 5154.
387-
func BenchmarkBufferFullSmallReads(b *testing.B) {
417+
func benchmarkBufferFullSmallReads(b *testing.B, createReadWriter func() bufLike) {
388418
buf := make([]byte, 1024)
419+
bf := createReadWriter()
389420
for i := 0; i < b.N; i++ {
390-
var b Buffer
391-
b.Write(buf)
392-
for b.Len()+20 < b.Cap() {
393-
b.Write(buf[:10])
421+
bf.Write(buf)
422+
for len(buf)+20 < cap(buf) {
423+
bf.Write(buf[:10])
394424
}
395425
for i := 0; i < 5<<10; i++ {
396-
b.Read(buf[:1])
397-
b.Write(buf[:1])
426+
bf.Read(buf[:1])
427+
bf.Write(buf[:1])
398428
}
429+
bf.Reset()
399430
}
400431
}
432+
433+
func BenchmarkBufferFullSmallReads(b *testing.B) {
434+
benchmarkBufferFullSmallReads(b, func() bufLike { return &Buffer{} })
435+
}
436+
437+
func BenchmarkBufferFullSmallReadsStdlib(b *testing.B) {
438+
benchmarkBufferFullSmallReads(b, func() bufLike { return &bytes.Buffer{} })
439+
}

0 commit comments

Comments
 (0)