veyron/lib/stats/counter: Add concurrency test.
Adds a concurrency test and parallel benchmark. On a 8-cpu GCE instance,
counter.Incr() calls take consistently ~300ns.
Change-Id: Ia146cdee2fcabab2172d564e8400ac96f73f9f18
diff --git a/lib/stats/counter/counter_test.go b/lib/stats/counter/counter_test.go
index 2c1cf8d..727dd56 100644
--- a/lib/stats/counter/counter_test.go
+++ b/lib/stats/counter/counter_test.go
@@ -1,6 +1,7 @@
package counter_test
import (
+ "sync"
"testing"
"time"
@@ -146,11 +147,32 @@
c.Reset()
}
+func TestConcurrent(t *testing.T) {
+ const numGoRoutines = 100
+ const numIncrPerGoRoutine = 100000
+ c := counter.New()
+ var wg sync.WaitGroup
+ wg.Add(numGoRoutines)
+ for i := 0; i < numGoRoutines; i++ {
+ go func() {
+ for x := 0; x < numIncrPerGoRoutine; x++ {
+ c.Incr(1)
+ }
+ wg.Done()
+ }()
+ }
+ wg.Wait()
+ if expected, got := int64(numGoRoutines*numIncrPerGoRoutine), c.Value(); got != expected {
+ t.Errorf("unexpected result. Got %v, want %v", got, expected)
+ }
+}
+
func BenchmarkCounterIncr(b *testing.B) {
c := counter.New()
-
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- c.Incr(1)
- }
+ b.SetParallelism(100)
+ b.RunParallel(func(pb *testing.PB) {
+ for pb.Next() {
+ c.Incr(1)
+ }
+ })
}