Commit 3b09ac57 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

runtime: new map tests and benchmarks

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
parent 26e0ddcf
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
"os" "os"
"runtime" "runtime"
"sort" "sort"
"strings"
"sync" "sync"
"testing" "testing"
) )
...@@ -317,9 +318,37 @@ func TestEmptyKeyAndValue(t *testing.T) { ...@@ -317,9 +318,37 @@ func TestEmptyKeyAndValue(t *testing.T) {
} }
} }
func BenchmarkNewEmptyMap(b *testing.B) { // Tests a map with a single bucket, with same-lengthed short keys
b.ReportAllocs() // ("quick keys") as well as long keys.
for i := 0; i < b.N; i++ { func TestSingleBucketMapStringKeys_DupLen(t *testing.T) {
_ = make(map[int]int) 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)
}
} }
} }
...@@ -150,6 +150,23 @@ func BenchmarkSmallStrMap(b *testing.B) { ...@@ -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) { func BenchmarkIntMap(b *testing.B) {
m := make(map[int]bool) m := make(map[int]bool)
for i := 0; i < 8; i++ { for i := 0; i < 8; i++ {
...@@ -182,3 +199,10 @@ func benchmarkRepeatedLookup(b *testing.B, lookupKeySize int) { ...@@ -182,3 +199,10 @@ func benchmarkRepeatedLookup(b *testing.B, lookupKeySize int) {
func BenchmarkRepeatedLookupStrMapKey32(b *testing.B) { benchmarkRepeatedLookup(b, 32) } func BenchmarkRepeatedLookupStrMapKey32(b *testing.B) { benchmarkRepeatedLookup(b, 32) }
func BenchmarkRepeatedLookupStrMapKey1M(b *testing.B) { benchmarkRepeatedLookup(b, 1<<20) } 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)
}
}
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