Commit cc063592 authored by Dmitriy Vyukov's avatar Dmitriy Vyukov

encoding/gob: make benchmarks parallel

There are lots of internal synchronization in gob,
so it makes sense to have parallel benchmarks.
Also add a benchmark with slices and interfaces.

LGTM=r
R=golang-codereviews, r
CC=golang-codereviews
https://golang.org/cl/115960043
parent d078d483
...@@ -19,33 +19,52 @@ type Bench struct { ...@@ -19,33 +19,52 @@ type Bench struct {
D []byte D []byte
} }
func benchmarkEndToEnd(r io.Reader, w io.Writer, b *testing.B) { func benchmarkEndToEnd(b *testing.B, v interface{}, pipe func() (r io.Reader, w io.Writer, err error)) {
b.StopTimer() b.RunParallel(func(pb *testing.PB) {
enc := NewEncoder(w) r, w, err := pipe()
dec := NewDecoder(r) if err != nil {
bench := &Bench{7, 3.2, "now is the time", bytes.Repeat([]byte("for all good men"), 100)} b.Fatal("can't get pipe:", err)
b.StartTimer()
for i := 0; i < b.N; i++ {
if enc.Encode(bench) != nil {
panic("encode error")
} }
if dec.Decode(bench) != nil { enc := NewEncoder(w)
panic("decode error") dec := NewDecoder(r)
for pb.Next() {
if err := enc.Encode(v); err != nil {
b.Fatal("encode error:", err)
}
if err := dec.Decode(v); err != nil {
b.Fatal("decode error:", err)
}
} }
} })
} }
func BenchmarkEndToEndPipe(b *testing.B) { func BenchmarkEndToEndPipe(b *testing.B) {
r, w, err := os.Pipe() v := &Bench{7, 3.2, "now is the time", bytes.Repeat([]byte("for all good men"), 100)}
if err != nil { benchmarkEndToEnd(b, v, func() (r io.Reader, w io.Writer, err error) {
b.Fatal("can't get pipe:", err) r, w, err = os.Pipe()
} return
benchmarkEndToEnd(r, w, b) })
} }
func BenchmarkEndToEndByteBuffer(b *testing.B) { func BenchmarkEndToEndByteBuffer(b *testing.B) {
var buf bytes.Buffer v := &Bench{7, 3.2, "now is the time", bytes.Repeat([]byte("for all good men"), 100)}
benchmarkEndToEnd(&buf, &buf, b) benchmarkEndToEnd(b, v, func() (r io.Reader, w io.Writer, err error) {
var buf bytes.Buffer
return &buf, &buf, nil
})
}
func BenchmarkEndToEndSliceByteBuffer(b *testing.B) {
v := &Bench{7, 3.2, "now is the time", nil}
Register(v)
arr := make([]interface{}, 100)
for i := range arr {
arr[i] = v
}
benchmarkEndToEnd(b, &arr, func() (r io.Reader, w io.Writer, err error) {
var buf bytes.Buffer
return &buf, &buf, nil
})
} }
func TestCountEncodeMallocs(t *testing.T) { func TestCountEncodeMallocs(t *testing.T) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment