Commit f7832df7 authored by Dmitriy Vyukov's avatar Dmitriy Vyukov

encoding/gob: fix data races in benchmarks

All goroutines decode into the same value.

LGTM=r
R=r, abursavich
CC=golang-codereviews
https://golang.org/cl/123930043
parent 21f7d83f
...@@ -19,12 +19,13 @@ type Bench struct { ...@@ -19,12 +19,13 @@ type Bench struct {
D []byte D []byte
} }
func benchmarkEndToEnd(b *testing.B, v interface{}, pipe func() (r io.Reader, w io.Writer, err error)) { func benchmarkEndToEnd(b *testing.B, ctor func() interface{}, pipe func() (r io.Reader, w io.Writer, err error)) {
b.RunParallel(func(pb *testing.PB) { b.RunParallel(func(pb *testing.PB) {
r, w, err := pipe() r, w, err := pipe()
if err != nil { if err != nil {
b.Fatal("can't get pipe:", err) b.Fatal("can't get pipe:", err)
} }
v := ctor()
enc := NewEncoder(w) enc := NewEncoder(w)
dec := NewDecoder(r) dec := NewDecoder(r)
for pb.Next() { for pb.Next() {
...@@ -39,29 +40,33 @@ func benchmarkEndToEnd(b *testing.B, v interface{}, pipe func() (r io.Reader, w ...@@ -39,29 +40,33 @@ func benchmarkEndToEnd(b *testing.B, v interface{}, pipe func() (r io.Reader, w
} }
func BenchmarkEndToEndPipe(b *testing.B) { func BenchmarkEndToEndPipe(b *testing.B) {
v := &Bench{7, 3.2, "now is the time", bytes.Repeat([]byte("for all good men"), 100)} benchmarkEndToEnd(b, func() interface{} {
benchmarkEndToEnd(b, v, func() (r io.Reader, w io.Writer, err error) { return &Bench{7, 3.2, "now is the time", bytes.Repeat([]byte("for all good men"), 100)}
}, func() (r io.Reader, w io.Writer, err error) {
r, w, err = os.Pipe() r, w, err = os.Pipe()
return return
}) })
} }
func BenchmarkEndToEndByteBuffer(b *testing.B) { func BenchmarkEndToEndByteBuffer(b *testing.B) {
v := &Bench{7, 3.2, "now is the time", bytes.Repeat([]byte("for all good men"), 100)} benchmarkEndToEnd(b, func() interface{} {
benchmarkEndToEnd(b, v, func() (r io.Reader, w io.Writer, err error) { return &Bench{7, 3.2, "now is the time", bytes.Repeat([]byte("for all good men"), 100)}
}, func() (r io.Reader, w io.Writer, err error) {
var buf bytes.Buffer var buf bytes.Buffer
return &buf, &buf, nil return &buf, &buf, nil
}) })
} }
func BenchmarkEndToEndSliceByteBuffer(b *testing.B) { func BenchmarkEndToEndSliceByteBuffer(b *testing.B) {
v := &Bench{7, 3.2, "now is the time", nil} benchmarkEndToEnd(b, func() interface{} {
Register(v) v := &Bench{7, 3.2, "now is the time", nil}
arr := make([]interface{}, 100) Register(v)
for i := range arr { arr := make([]interface{}, 100)
arr[i] = v for i := range arr {
} arr[i] = v
benchmarkEndToEnd(b, &arr, func() (r io.Reader, w io.Writer, err error) { }
return &arr
}, func() (r io.Reader, w io.Writer, err error) {
var buf bytes.Buffer var buf bytes.Buffer
return &buf, &buf, nil return &buf, &buf, nil
}) })
......
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