Skip to content

Commit 39651bb

Browse files
author
Bryan C. Mills
committed
expvar: parallelize BenchmarkMapAdd{Same,Different}
The other expvar tests are already parallelized, and this will help to measure the impact of potential implementations for #18177. updates #18177 Change-Id: I0f4f1a16a0285556cbcc8339855b6459af412675 Reviewed-on: https://go-review.googlesource.com/36717 Reviewed-by: Russ Cox <[email protected]>
1 parent 066ac42 commit 39651bb

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

src/expvar/expvar_test.go

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ package expvar
77
import (
88
"bytes"
99
"encoding/json"
10+
"fmt"
1011
"net"
1112
"net/http/httptest"
1213
"reflect"
1314
"runtime"
1415
"strconv"
1516
"sync"
17+
"sync/atomic"
1618
"testing"
1719
)
1820

@@ -219,23 +221,42 @@ func BenchmarkMapSet(b *testing.B) {
219221
}
220222

221223
func BenchmarkMapAddSame(b *testing.B) {
222-
for i := 0; i < b.N; i++ {
223-
m := new(Map).Init()
224-
m.Add("red", 1)
225-
m.Add("red", 1)
226-
m.Add("red", 1)
227-
m.Add("red", 1)
228-
}
224+
m := new(Map).Init()
225+
b.ResetTimer()
226+
227+
b.RunParallel(func(pb *testing.PB) {
228+
for pb.Next() {
229+
m.Add("red", 1)
230+
}
231+
})
229232
}
230233

231234
func BenchmarkMapAddDifferent(b *testing.B) {
232-
for i := 0; i < b.N; i++ {
233-
m := new(Map).Init()
234-
m.Add("red", 1)
235-
m.Add("blue", 1)
236-
m.Add("green", 1)
237-
m.Add("yellow", 1)
235+
procKeys := make([][]string, runtime.GOMAXPROCS(0))
236+
for i := range procKeys {
237+
keys := make([]string, 4)
238+
for j := range keys {
239+
keys[j] = fmt.Sprint(i, j)
240+
}
241+
procKeys[i] = keys
238242
}
243+
244+
m := new(Map).Init()
245+
b.ResetTimer()
246+
247+
var n int32
248+
b.RunParallel(func(pb *testing.PB) {
249+
i := int(atomic.AddInt32(&n, 1)-1) % len(procKeys)
250+
keys := procKeys[i]
251+
j := 0
252+
253+
for pb.Next() {
254+
m.Add(keys[j], 1)
255+
if j++; j == len(keys) {
256+
j = 0
257+
}
258+
}
259+
})
239260
}
240261

241262
func TestFunc(t *testing.T) {

0 commit comments

Comments
 (0)