]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: new map tests and benchmarks
authorBrad Fitzpatrick <bradfitz@golang.org>
Wed, 3 Apr 2013 00:55:49 +0000 (17:55 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 3 Apr 2013 00:55:49 +0000 (17:55 -0700)
Also, move an existing benchmark from map_test.go to
mapspeed_test.go.

R=golang-dev, khr
CC=golang-dev
https://golang.org/cl/8294043

src/pkg/runtime/map_test.go
src/pkg/runtime/mapspeed_test.go

index fcce9a4fe6d6effa4bbd26104545cf318a9abe56..209355e9301709d0a72d4d78d98279d34a38a946 100644 (file)
@@ -10,6 +10,7 @@ import (
        "os"
        "runtime"
        "sort"
+       "strings"
        "sync"
        "testing"
 )
@@ -317,9 +318,37 @@ func TestEmptyKeyAndValue(t *testing.T) {
        }
 }
 
-func BenchmarkNewEmptyMap(b *testing.B) {
-       b.ReportAllocs()
-       for i := 0; i < b.N; i++ {
-               _ = make(map[int]int)
+// Tests a map with a single bucket, with same-lengthed short keys
+// ("quick keys") as well as long keys.
+func TestSingleBucketMapStringKeys_DupLen(t *testing.T) {
+       testMapLookups(t, map[string]string{
+               "x":    "x1val",
+               "xx":   "x2val",
+               "foo":  "fooval",
+               "bar":  "barval", // same key length as "foo"
+               "xxxx": "x4val",
+               strings.Repeat("x", 128): "longval1",
+               strings.Repeat("y", 128): "longval2",
+       })
+}
+
+// Tests a map with a single bucket, with all keys having different lengths.
+func TestSingleBucketMapStringKeys_NoDupLen(t *testing.T) {
+       testMapLookups(t, map[string]string{
+               "x":                      "x1val",
+               "xx":                     "x2val",
+               "foo":                    "fooval",
+               "xxxx":                   "x4val",
+               "xxxxx":                  "x5val",
+               "xxxxxx":                 "x6val",
+               strings.Repeat("x", 128): "longval",
+       })
+}
+
+func testMapLookups(t *testing.T, m map[string]string) {
+       for k, v := range m {
+               if m[k] != v {
+                       t.Fatalf("m[%q] = %q; want %q", k, m[k], v)
+               }
        }
 }
index 73be43453527b2dff4541fc8618139334e74f5f8..3b7fbfd638b10194b9e4b8c88a2dd0b2b1646198 100644 (file)
@@ -150,6 +150,23 @@ func BenchmarkSmallStrMap(b *testing.B) {
        }
 }
 
+func BenchmarkMapStringKeysEight_16(b *testing.B) { benchmarkMapStringKeysEight(b, 16) }
+func BenchmarkMapStringKeysEight_32(b *testing.B) { benchmarkMapStringKeysEight(b, 32) }
+func BenchmarkMapStringKeysEight_64(b *testing.B) { benchmarkMapStringKeysEight(b, 64) }
+func BenchmarkMapStringKeysEight_1M(b *testing.B) { benchmarkMapStringKeysEight(b, 1<<20) }
+
+func benchmarkMapStringKeysEight(b *testing.B, keySize int) {
+       m := make(map[string]bool)
+       for i := 0; i < 8; i++ {
+               m[strings.Repeat("K", i+1)] = true
+       }
+       key := strings.Repeat("K", keySize)
+       b.ResetTimer()
+       for i := 0; i < b.N; i++ {
+               _ = m[key]
+       }
+}
+
 func BenchmarkIntMap(b *testing.B) {
        m := make(map[int]bool)
        for i := 0; i < 8; i++ {
@@ -182,3 +199,10 @@ func benchmarkRepeatedLookup(b *testing.B, lookupKeySize int) {
 
 func BenchmarkRepeatedLookupStrMapKey32(b *testing.B) { benchmarkRepeatedLookup(b, 32) }
 func BenchmarkRepeatedLookupStrMapKey1M(b *testing.B) { benchmarkRepeatedLookup(b, 1<<20) }
+
+func BenchmarkNewEmptyMap(b *testing.B) {
+       b.ReportAllocs()
+       for i := 0; i < b.N; i++ {
+               _ = make(map[int]int)
+       }
+}