]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: more reliable mapdelete benchmark
authorHugues Bruant <hugues.bruant@gmail.com>
Mon, 21 Aug 2017 18:16:08 +0000 (11:16 -0700)
committerDaniel Martí <mvdan@mvdan.cc>
Sat, 21 Oct 2017 22:48:07 +0000 (22:48 +0000)
Increasing the map size with the benchmark iteration count
introduced non-linearities and made benchmark runs slow when
increasing benchtime.

Rework the benchmark to use a map size independent of the
iteration count and instead re-fill it when it becomes empty.

Fixes #21546

Change-Id: Iafb6eb225e81830263f30b3aba0d449c361aec32
Reviewed-on: https://go-review.googlesource.com/57650
Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
src/runtime/map_test.go

index a3a21e2f80012e92fde5baffdcba51e05664f623..d6054c05273e3a232affd189a4c5ec4835dce97e 100644 (file)
@@ -688,12 +688,16 @@ func benchmarkMapAssignInt32(b *testing.B, n int) {
 }
 
 func benchmarkMapDeleteInt32(b *testing.B, n int) {
-       a := make(map[int32]int, n*b.N)
-       for i := 0; i < n*b.N; i++ {
-               a[int32(i)] = i
-       }
+       a := make(map[int32]int, n)
        b.ResetTimer()
-       for i := 0; i < n*b.N; i = i + n {
+       for i := 0; i < b.N; i++ {
+               if len(a) == 0 {
+                       b.StopTimer()
+                       for j := i; j < i+n; j++ {
+                               a[int32(j)] = j
+                       }
+                       b.StartTimer()
+               }
                delete(a, int32(i))
        }
 }
@@ -706,12 +710,16 @@ func benchmarkMapAssignInt64(b *testing.B, n int) {
 }
 
 func benchmarkMapDeleteInt64(b *testing.B, n int) {
-       a := make(map[int64]int, n*b.N)
-       for i := 0; i < n*b.N; i++ {
-               a[int64(i)] = i
-       }
+       a := make(map[int64]int, n)
        b.ResetTimer()
-       for i := 0; i < n*b.N; i = i + n {
+       for i := 0; i < b.N; i++ {
+               if len(a) == 0 {
+                       b.StopTimer()
+                       for j := i; j < i+n; j++ {
+                               a[int64(j)] = j
+                       }
+                       b.StartTimer()
+               }
                delete(a, int64(i))
        }
 }
@@ -729,17 +737,23 @@ func benchmarkMapAssignStr(b *testing.B, n int) {
 }
 
 func benchmarkMapDeleteStr(b *testing.B, n int) {
-       k := make([]string, n*b.N)
-       for i := 0; i < n*b.N; i++ {
-               k[i] = strconv.Itoa(i)
-       }
-       a := make(map[string]int, n*b.N)
-       for i := 0; i < n*b.N; i++ {
-               a[k[i]] = i
+       i2s := make([]string, n)
+       for i := 0; i < n; i++ {
+               i2s[i] = strconv.Itoa(i)
        }
+       a := make(map[string]int, n)
        b.ResetTimer()
-       for i := 0; i < n*b.N; i = i + n {
-               delete(a, k[i])
+       k := 0
+       for i := 0; i < b.N; i++ {
+               if len(a) == 0 {
+                       b.StopTimer()
+                       for j := 0; j < n; j++ {
+                               a[i2s[j]] = j
+                       }
+                       k = i
+                       b.StartTimer()
+               }
+               delete(a, i2s[i-k])
        }
 }
 
@@ -758,7 +772,7 @@ func BenchmarkMapAssign(b *testing.B) {
 }
 
 func BenchmarkMapDelete(b *testing.B) {
-       b.Run("Int32", runWith(benchmarkMapDeleteInt32, 1, 2, 4))
-       b.Run("Int64", runWith(benchmarkMapDeleteInt64, 1, 2, 4))
-       b.Run("Str", runWith(benchmarkMapDeleteStr, 1, 2, 4))
+       b.Run("Int32", runWith(benchmarkMapDeleteInt32, 100, 1000, 10000))
+       b.Run("Int64", runWith(benchmarkMapDeleteInt64, 100, 1000, 10000))
+       b.Run("Str", runWith(benchmarkMapDeleteStr, 100, 1000, 10000))
 }