diff options
| author | Bryan C. Mills <bcmills@google.com> | 2017-02-09 18:39:16 -0500 |
|---|---|---|
| committer | Bryan Mills <bcmills@google.com> | 2017-02-10 17:37:14 +0000 |
| commit | 39651bb50bc0e1038e77e63baa37729bc4676e8a (patch) | |
| tree | 98a4f12efd296a7e2e6939eb69f6da869b15941a | |
| parent | 066ac428cdcb0b220a8a58f31c884d054cecd118 (diff) | |
| download | go-39651bb50bc0e1038e77e63baa37729bc4676e8a.tar.xz | |
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 <rsc@golang.org>
| -rw-r--r-- | src/expvar/expvar_test.go | 47 |
1 files changed, 34 insertions, 13 deletions
diff --git a/src/expvar/expvar_test.go b/src/expvar/expvar_test.go index 0efa8643c0..f802cfd37e 100644 --- a/src/expvar/expvar_test.go +++ b/src/expvar/expvar_test.go @@ -7,12 +7,14 @@ package expvar import ( "bytes" "encoding/json" + "fmt" "net" "net/http/httptest" "reflect" "runtime" "strconv" "sync" + "sync/atomic" "testing" ) @@ -219,23 +221,42 @@ func BenchmarkMapSet(b *testing.B) { } func BenchmarkMapAddSame(b *testing.B) { - for i := 0; i < b.N; i++ { - m := new(Map).Init() - m.Add("red", 1) - m.Add("red", 1) - m.Add("red", 1) - m.Add("red", 1) - } + m := new(Map).Init() + b.ResetTimer() + + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + m.Add("red", 1) + } + }) } func BenchmarkMapAddDifferent(b *testing.B) { - for i := 0; i < b.N; i++ { - m := new(Map).Init() - m.Add("red", 1) - m.Add("blue", 1) - m.Add("green", 1) - m.Add("yellow", 1) + procKeys := make([][]string, runtime.GOMAXPROCS(0)) + for i := range procKeys { + keys := make([]string, 4) + for j := range keys { + keys[j] = fmt.Sprint(i, j) + } + procKeys[i] = keys } + + m := new(Map).Init() + b.ResetTimer() + + var n int32 + b.RunParallel(func(pb *testing.PB) { + i := int(atomic.AddInt32(&n, 1)-1) % len(procKeys) + keys := procKeys[i] + j := 0 + + for pb.Next() { + m.Add(keys[j], 1) + if j++; j == len(keys) { + j = 0 + } + } + }) } func TestFunc(t *testing.T) { |
