summaryrefslogtreecommitdiffstats
path: root/test/stress/maps.go
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--test/stress/maps.go113
1 files changed, 113 insertions, 0 deletions
diff --git a/test/stress/maps.go b/test/stress/maps.go
new file mode 100644
index 0000000..8ada23a
--- /dev/null
+++ b/test/stress/maps.go
@@ -0,0 +1,113 @@
+// Copyright 2013 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 main
+
+import (
+ "math/rand"
+ "runtime"
+ "sync"
+)
+
+func mapTypes() []MapType {
+ // TODO(bradfitz): bunch more map types of all different key and value types.
+ // Use reflect.MapOf and a program to generate lots of types & struct types.
+ // For now, just one:
+ return []MapType{intMapType{}}
+}
+
+type MapType interface {
+ NewMap() Map
+}
+
+type Map interface {
+ AddItem()
+ DelItem()
+ Len() int
+ GetItem()
+ RangeAll()
+}
+
+func stressMapType(mt MapType, done func()) {
+ defer done()
+ m := mt.NewMap()
+ for m.Len() < 10000 {
+ Println("map at ", m.Len())
+ if m.Len()%100 == 0 {
+ runtime.Gosched()
+ }
+ m.AddItem()
+ m.AddItem()
+ m.DelItem()
+ var wg sync.WaitGroup
+ const numGets = 10
+ wg.Add(numGets)
+ for i := 0; i < numGets; i++ {
+ go func(i int) {
+ if i&1 == 0 {
+ m.GetItem()
+ } else {
+ m.RangeAll()
+ }
+ wg.Done()
+ }(i)
+ }
+ wg.Wait()
+ }
+ for m.Len() > 0 {
+ m.DelItem()
+ }
+}
+
+type intMapType struct{}
+
+func (intMapType) NewMap() Map {
+ return make(intMap)
+}
+
+var deadcafe = []byte("\xDE\xAD\xCA\xFE")
+
+type intMap map[int][]byte
+
+func (m intMap) AddItem() {
+ s0 := len(m)
+ for len(m) == s0 {
+ key := rand.Intn(s0 + 1)
+ m[key] = make([]byte, rand.Intn(64<<10))
+ }
+}
+
+func (m intMap) DelItem() {
+ for k := range m {
+ delete(m, k)
+ return
+ }
+}
+
+func (m intMap) GetItem() {
+ key := rand.Intn(len(m))
+ if s, ok := m[key]; ok {
+ copy(s, deadcafe)
+ }
+}
+
+func (m intMap) Len() int { return len(m) }
+
+func (m intMap) RangeAll() {
+ for _ = range m {
+ }
+ for range m {
+ }
+}
+
+func stressMaps() {
+ for {
+ var wg sync.WaitGroup
+ for _, mt := range mapTypes() {
+ wg.Add(1)
+ go stressMapType(mt, wg.Done)
+ }
+ wg.Wait()
+ }
+}