reflect: add a copy of BenchmarkMap
This is a copy of reflect_test.BenchmakMap from the Go standard library,
added in http://golang.org/cl/345486.
This is added to x/benchmarks only temporarily as a sample 'go test'
benchmark to use with benchmarking tools. This benchmark was selected in
particular because it had a significant performance improvement with a
recent upstream change (http://golang.org/cl/345486).
Change-Id: I2bff47ffbaab1d471a37ec3cba4bea88cc78d8ed
Reviewed-on: https://go-review.googlesource.com/c/benchmarks/+/353649
Trust: Michael Pratt <mpratt@google.com>
Run-TryBot: Michael Pratt <mpratt@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
diff --git a/reflect/reflect_test.go b/reflect/reflect_test.go
new file mode 100644
index 0000000..f02be3e
--- /dev/null
+++ b/reflect/reflect_test.go
@@ -0,0 +1,60 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package reflect_test
+
+import (
+ "fmt"
+ "reflect"
+ "testing"
+)
+
+// BenchmarkMap is a copy of reflect_test.BenchmarkMap from the Go standard
+// library, placed here for basic benchmarking testing.
+func BenchmarkMap(b *testing.B) {
+ type V *int
+ value := reflect.ValueOf((V)(nil))
+ stringKeys := []string{}
+ mapOfStrings := map[string]V{}
+ uint64Keys := []uint64{}
+ mapOfUint64s := map[uint64]V{}
+ for i := 0; i < 100; i++ {
+ stringKey := fmt.Sprintf("key%d", i)
+ stringKeys = append(stringKeys, stringKey)
+ mapOfStrings[stringKey] = nil
+
+ uint64Key := uint64(i)
+ uint64Keys = append(uint64Keys, uint64Key)
+ mapOfUint64s[uint64Key] = nil
+ }
+
+ tests := []struct {
+ label string
+ m, keys, value reflect.Value
+ }{
+ {"StringKeys", reflect.ValueOf(mapOfStrings), reflect.ValueOf(stringKeys), value},
+ {"Uint64Keys", reflect.ValueOf(mapOfUint64s), reflect.ValueOf(uint64Keys), value},
+ }
+
+ for _, tt := range tests {
+ b.Run(tt.label, func(b *testing.B) {
+ b.Run("MapIndex", func(b *testing.B) {
+ b.ReportAllocs()
+ for i := 0; i < b.N; i++ {
+ for j := tt.keys.Len() - 1; j >= 0; j-- {
+ tt.m.MapIndex(tt.keys.Index(j))
+ }
+ }
+ })
+ b.Run("SetMapIndex", func(b *testing.B) {
+ b.ReportAllocs()
+ for i := 0; i < b.N; i++ {
+ for j := tt.keys.Len() - 1; j >= 0; j-- {
+ tt.m.SetMapIndex(tt.keys.Index(j), tt.value)
+ }
+ }
+ })
+ })
+ }
+}