Commit 39651bb5 authored by Bryan C. Mills's avatar Bryan C. Mills Committed by Bryan Mills

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/36717Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
parent 066ac428
...@@ -7,12 +7,14 @@ package expvar ...@@ -7,12 +7,14 @@ package expvar
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
"net" "net"
"net/http/httptest" "net/http/httptest"
"reflect" "reflect"
"runtime" "runtime"
"strconv" "strconv"
"sync" "sync"
"sync/atomic"
"testing" "testing"
) )
...@@ -219,23 +221,42 @@ func BenchmarkMapSet(b *testing.B) { ...@@ -219,23 +221,42 @@ func BenchmarkMapSet(b *testing.B) {
} }
func BenchmarkMapAddSame(b *testing.B) { func BenchmarkMapAddSame(b *testing.B) {
for i := 0; i < b.N; i++ { m := new(Map).Init()
m := new(Map).Init() b.ResetTimer()
m.Add("red", 1)
m.Add("red", 1) b.RunParallel(func(pb *testing.PB) {
m.Add("red", 1) for pb.Next() {
m.Add("red", 1) m.Add("red", 1)
} }
})
} }
func BenchmarkMapAddDifferent(b *testing.B) { func BenchmarkMapAddDifferent(b *testing.B) {
for i := 0; i < b.N; i++ { procKeys := make([][]string, runtime.GOMAXPROCS(0))
m := new(Map).Init() for i := range procKeys {
m.Add("red", 1) keys := make([]string, 4)
m.Add("blue", 1) for j := range keys {
m.Add("green", 1) keys[j] = fmt.Sprint(i, j)
m.Add("yellow", 1) }
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) { func TestFunc(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