-
Notifications
You must be signed in to change notification settings - Fork 0
/
orderedmap_bench_test.go
168 lines (148 loc) Β· 2.84 KB
/
orderedmap_bench_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package orderedmap
import (
"math/rand"
"sync"
"testing"
"time"
)
func BenchmarkOrderedMap(b *testing.B) {
benchmarkOrderedMap(b, false, false)
}
func BenchmarkOrderedMapSlack(b *testing.B) {
benchmarkOrderedMap(b, true, false)
}
func BenchmarkOrderedMapWork(b *testing.B) {
benchmarkOrderedMap(b, false, true)
}
func BenchmarkOrderedMapWorkSlack(b *testing.B) {
benchmarkOrderedMap(b, true, true)
}
func benchmarkOrderedMap(b *testing.B, slack, work bool) {
m := New[int, int]()
if slack {
b.SetParallelism(10)
}
rand.Seed(time.Now().UnixNano())
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
foo := 0
for pb.Next() {
k := rand.Int()
v := rand.Int()
m.Store(k, v)
if nv, _ := m.Load(k); nv != v {
panic("NOT EQ!!")
}
m.Delete(k)
if m.Has(k) {
panic("KEY EXISTS")
}
if work {
for i := 0; i < 100; i++ {
foo *= 2
foo /= 2
}
}
}
_ = foo
})
}
func BenchmarkNativeSyncMap_Store(b *testing.B) {
rand.Seed(time.Now().UnixNano())
b.ReportAllocs()
m := new(sync.Map)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
k := rand.Int()
v := rand.Int()
m.Store(k, v)
if _, ok := m.Load(k); !ok {
panic("KEY NOT SET")
}
}
})
}
func BenchmarkOrderedMap_Store(b *testing.B) {
rand.Seed(time.Now().UnixNano())
b.ReportAllocs()
m := New[int, int]()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
k := rand.Int()
v := rand.Int()
m.Store(k, v)
if !m.Has(k) {
panic("KEY NOT SET")
}
}
})
}
func BenchmarkNativeSyncMap_LoadOrStore(b *testing.B) {
rand.Seed(time.Now().UnixNano())
b.ReportAllocs()
m := new(sync.Map)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
k := rand.Int()
v := rand.Int()
m.LoadOrStore(k, v)
if _, ok := m.Load(k); !ok {
panic("KEY NOT SET")
}
}
})
}
func BenchmarkOrderedMap_LoadOrStore(b *testing.B) {
rand.Seed(time.Now().UnixNano())
b.ReportAllocs()
m := New[int, int]()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
k := rand.Int()
v := rand.Int()
m.LoadOrStore(k, v)
if !m.Has(k) {
panic("KEY NOT SET")
}
}
})
}
func BenchmarkNativeSyncMap_Delete(b *testing.B) {
rand.Seed(time.Now().UnixNano())
b.ReportAllocs()
m := new(sync.Map)
for i := 0; i < 1000000; i++ {
k := rand.Int()
v := rand.Int()
m.Store(k, v)
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
var k any
m.Range(func(key, value any) bool {
k = key
return false
})
m.Delete(k)
}
})
}
func BenchmarkOrderedMap_Delete(b *testing.B) {
rand.Seed(time.Now().UnixNano())
b.ReportAllocs()
m := New[int, int]()
for i := 0; i < 1000000; i++ {
k := rand.Int()
v := rand.Int()
m.Store(k, v)
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if m.Front() != nil {
m.Delete(m.Front().Key())
}
}
})
}