forked from orcaman/concurrent-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcurrent_map_bench_test.go
196 lines (176 loc) · 4.31 KB
/
concurrent_map_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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package cmap
import "testing"
import "strconv"
func BenchmarkItems(b *testing.B) {
m := New()
// Insert 100 elements.
for i := 0; i < 10000; i++ {
m.Set(strconv.Itoa(i), Animal{strconv.Itoa(i)})
}
for i := 0; i < b.N; i++ {
m.Items()
}
}
func BenchmarkMarshalJson(b *testing.B) {
m := New()
// Insert 100 elements.
for i := 0; i < 10000; i++ {
m.Set(strconv.Itoa(i), Animal{strconv.Itoa(i)})
}
for i := 0; i < b.N; i++ {
m.MarshalJSON()
}
}
func BenchmarkStrconv(b *testing.B) {
for i := 0; i < b.N; i++ {
strconv.Itoa(i)
}
}
func BenchmarkSingleInsertAbsent(b *testing.B) {
m := New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Set(strconv.Itoa(i), "value")
}
}
func BenchmarkSingleInsertPresent(b *testing.B) {
m := New()
m.Set("key", "value")
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Set("key", "value")
}
}
func benchmarkMultiInsertDifferent(b *testing.B) {
m := New()
finished := make(chan struct{}, b.N)
_, set := GetSet(m, finished)
b.ResetTimer()
for i := 0; i < b.N; i++ {
set(strconv.Itoa(i), "value")
}
for i := 0; i < b.N; i++ {
<-finished
}
}
func BenchmarkMultiInsertDifferent_1_Shard(b *testing.B) {
runWithShards(benchmarkMultiInsertDifferent, b, 1)
}
func BenchmarkMultiInsertDifferent_16_Shard(b *testing.B) {
runWithShards(benchmarkMultiInsertDifferent, b, 16)
}
func BenchmarkMultiInsertDifferent_32_Shard(b *testing.B) {
runWithShards(benchmarkMultiInsertDifferent, b, 32)
}
func BenchmarkMultiInsertDifferent_256_Shard(b *testing.B) {
runWithShards(benchmarkMultiGetSetDifferent, b, 256)
}
func BenchmarkMultiInsertSame(b *testing.B) {
m := New()
finished := make(chan struct{}, b.N)
_, set := GetSet(m, finished)
m.Set("key", "value")
b.ResetTimer()
for i := 0; i < b.N; i++ {
set("key", "value")
}
for i := 0; i < b.N; i++ {
<-finished
}
}
func BenchmarkMultiGetSame(b *testing.B) {
m := New()
finished := make(chan struct{}, b.N)
get, _ := GetSet(m, finished)
m.Set("key", "value")
b.ResetTimer()
for i := 0; i < b.N; i++ {
get("key", "value")
}
for i := 0; i < b.N; i++ {
<-finished
}
}
func benchmarkMultiGetSetDifferent(b *testing.B) {
m := New()
finished := make(chan struct{}, 2*b.N)
get, set := GetSet(m, finished)
m.Set("-1", "value")
b.ResetTimer()
for i := 0; i < b.N; i++ {
set(strconv.Itoa(i-1), "value")
get(strconv.Itoa(i), "value")
}
for i := 0; i < 2*b.N; i++ {
<-finished
}
}
func BenchmarkMultiGetSetDifferent_1_Shard(b *testing.B) {
runWithShards(benchmarkMultiGetSetDifferent, b, 1)
}
func BenchmarkMultiGetSetDifferent_16_Shard(b *testing.B) {
runWithShards(benchmarkMultiGetSetDifferent, b, 16)
}
func BenchmarkMultiGetSetDifferent_32_Shard(b *testing.B) {
runWithShards(benchmarkMultiGetSetDifferent, b, 32)
}
func BenchmarkMultiGetSetDifferent_256_Shard(b *testing.B) {
runWithShards(benchmarkMultiGetSetDifferent, b, 256)
}
func benchmarkMultiGetSetBlock(b *testing.B) {
m := New()
finished := make(chan struct{}, 2*b.N)
get, set := GetSet(m, finished)
for i := 0; i < b.N; i++ {
m.Set(strconv.Itoa(i%100), "value")
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
set(strconv.Itoa(i%100), "value")
get(strconv.Itoa(i%100), "value")
}
for i := 0; i < 2*b.N; i++ {
<-finished
}
}
func BenchmarkMultiGetSetBlock_1_Shard(b *testing.B) {
runWithShards(benchmarkMultiGetSetBlock, b, 1)
}
func BenchmarkMultiGetSetBlock_16_Shard(b *testing.B) {
runWithShards(benchmarkMultiGetSetBlock, b, 16)
}
func BenchmarkMultiGetSetBlock_32_Shard(b *testing.B) {
runWithShards(benchmarkMultiGetSetBlock, b, 32)
}
func BenchmarkMultiGetSetBlock_256_Shard(b *testing.B) {
runWithShards(benchmarkMultiGetSetBlock, b, 256)
}
func GetSet(m ConcurrentMap, finished chan struct{}) (set func(key, value string), get func(key, value string)) {
return func(key, value string) {
for i := 0; i < 10; i++ {
m.Get(key)
}
finished <- struct{}{}
}, func(key, value string) {
for i := 0; i < 10; i++ {
m.Set(key, value)
}
finished <- struct{}{}
}
}
func runWithShards(bench func(b *testing.B), b *testing.B, shardsCount int) {
oldShardsCount := SHARD_COUNT
SHARD_COUNT = shardsCount
bench(b)
SHARD_COUNT = oldShardsCount
}
func BenchmarkKeys(b *testing.B) {
m := New()
// Insert 100 elements.
for i := 0; i < 10000; i++ {
m.Set(strconv.Itoa(i), Animal{strconv.Itoa(i)})
}
for i := 0; i < b.N; i++ {
m.Keys()
}
}