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 {
D []byte
}
func benchmarkEndToEnd(r io.Reader, w io.Writer, b *testing.B) {
b.StopTimer()
func benchmarkEndToEnd(b *testing.B, v interface{}, pipe func() (r io.Reader, w io.Writer, err error)) {
b.RunParallel(func(pb *testing.PB) {
r, w, err := pipe()
if err != nil {
b.Fatal("can't get pipe:", err)
}
enc := NewEncoder(w)
dec := NewDecoder(r)
bench := &Bench{7, 3.2, "now is the time", bytes.Repeat([]byte("for all good men"), 100)}
b.StartTimer()
for i := 0; i < b.N; i++ {
if enc.Encode(bench) != nil {
panic("encode error")
for pb.Next() {
if err := enc.Encode(v); err != nil {
b.Fatal("encode error:", err)
}
if dec.Decode(bench) != nil {
panic("decode error")
if err := dec.Decode(v); err != nil {
b.Fatal("decode error:", err)
}
}
})
}
func BenchmarkEndToEndPipe(b *testing.B) {
r, w, err := os.Pipe()
if err != nil {
b.Fatal("can't get pipe:", err)
}
benchmarkEndToEnd(r, w, b)
v := &Bench{7, 3.2, "now is the time", bytes.Repeat([]byte("for all good men"), 100)}
benchmarkEndToEnd(b, v, func() (r io.Reader, w io.Writer, err error) {
r, w, err = os.Pipe()
return
})
}
func BenchmarkEndToEndByteBuffer(b *testing.B) {
v := &Bench{7, 3.2, "now is the time", bytes.Repeat([]byte("for all good men"), 100)}
benchmarkEndToEnd(b, v, func() (r io.Reader, w io.Writer, err error) {
var buf bytes.Buffer
benchmarkEndToEnd(&buf, &buf, b)
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) {
......
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