-
Notifications
You must be signed in to change notification settings - Fork 5
/
benchmark_test.go
264 lines (203 loc) · 5.43 KB
/
benchmark_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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package concurrentcount
import (
"sync"
"testing"
)
const (
// How many more writes to the counter than reads. In typical telemetry
// systems, you might have just ~1 read per minute, but possibly
// millions of reads in the same time.
writeToReadRatio = 1000
// Delta used in runAdd.
delta = 0.5
)
func runInc(b *testing.B, c Counter, concurrency int) {
b.StopTimer()
var start, end sync.WaitGroup
start.Add(1)
end.Add(concurrency)
n := b.N / concurrency
for i := 0; i < concurrency; i++ {
go func() {
start.Wait()
for i := 0; i < n; i++ {
if i%writeToReadRatio == 0 {
c.Get()
} else {
c.Inc()
}
}
end.Done()
}()
}
b.StartTimer()
start.Done()
end.Wait()
}
func runAdd(b *testing.B, c Counter, concurrency int) {
b.StopTimer()
var start, end sync.WaitGroup
start.Add(1)
end.Add(concurrency)
n := b.N / concurrency
for i := 0; i < concurrency; i++ {
go func() {
start.Wait()
for i := 0; i < n; i++ {
if i%writeToReadRatio == 0 {
c.Get()
} else {
c.Add(delta)
}
}
end.Done()
}()
}
b.StartTimer()
start.Done()
end.Wait()
}
func BenchmarkMutexCounterInc1(b *testing.B) {
runInc(b, &MutexCounter{}, 1)
}
func BenchmarkMutexCounterAdd1(b *testing.B) {
runAdd(b, &MutexCounter{}, 1)
}
func BenchmarkRWMutexCounterInc1(b *testing.B) {
runInc(b, &RWMutexCounter{}, 1)
}
func BenchmarkRWMutexCounterAdd1(b *testing.B) {
runAdd(b, &RWMutexCounter{}, 1)
}
func BenchmarkAtomicIntCounterInc1(b *testing.B) {
runInc(b, new(AtomicIntCounter), 1)
}
func BenchmarkAtomicIntCounterAdd1(b *testing.B) {
runAdd(b, new(AtomicIntCounter), 1)
}
func BenchmarkNaiveCounterInc1(b *testing.B) {
runInc(b, new(NaiveCounter), 1)
}
func BenchmarkNaiveCounterAdd1(b *testing.B) {
runAdd(b, new(NaiveCounter), 1)
}
func BenchmarkNaiveIntCounterInc1(b *testing.B) {
runInc(b, new(NaiveIntCounter), 1)
}
func BenchmarkNaiveIntCounterAdd1(b *testing.B) {
runAdd(b, new(NaiveIntCounter), 1)
}
func BenchmarkAtomicSpinningCounterInc1(b *testing.B) {
runInc(b, new(AtomicSpinningCounter), 1)
}
func BenchmarkAtomicSpinningCounterAdd1(b *testing.B) {
runAdd(b, new(AtomicSpinningCounter), 1)
}
func BenchmarkSyncChannelCounterInc1(b *testing.B) {
runInc(b, NewChannelCounter(0), 1)
}
func BenchmarkSyncChannelCounterAdd1(b *testing.B) {
runAdd(b, NewChannelCounter(0), 1)
}
func BenchmarkBufferedChannelCounterInc1(b *testing.B) {
runInc(b, NewChannelCounter(1024), 1)
}
func BenchmarkBufferedChannelCounterAdd1(b *testing.B) {
runAdd(b, NewChannelCounter(1024), 1)
}
func BenchmarkMutexCounterInc10(b *testing.B) {
runInc(b, &MutexCounter{}, 10)
}
func BenchmarkMutexCounterAdd10(b *testing.B) {
runAdd(b, &MutexCounter{}, 10)
}
func BenchmarkRWMutexCounterInc10(b *testing.B) {
runInc(b, &RWMutexCounter{}, 10)
}
func BenchmarkRWMutexCounterAdd10(b *testing.B) {
runAdd(b, &RWMutexCounter{}, 10)
}
func BenchmarkAtomicIntCounterInc10(b *testing.B) {
runInc(b, new(AtomicIntCounter), 10)
}
func BenchmarkAtomicIntCounterAdd10(b *testing.B) {
runAdd(b, new(AtomicIntCounter), 10)
}
func BenchmarkNaiveCounterInc10(b *testing.B) {
runInc(b, new(NaiveCounter), 10)
}
func BenchmarkNaiveCounterAdd10(b *testing.B) {
runAdd(b, new(NaiveCounter), 10)
}
func BenchmarkNaiveIntCounterInc10(b *testing.B) {
runInc(b, new(NaiveIntCounter), 10)
}
func BenchmarkNaiveIntCounterAdd10(b *testing.B) {
runAdd(b, new(NaiveIntCounter), 10)
}
func BenchmarkAtomicSpinningCounterInc10(b *testing.B) {
runInc(b, new(AtomicSpinningCounter), 10)
}
func BenchmarkAtomicSpinningCounterAdd10(b *testing.B) {
runAdd(b, new(AtomicSpinningCounter), 10)
}
func BenchmarkSyncChannelCounterInc10(b *testing.B) {
runInc(b, NewChannelCounter(0), 10)
}
func BenchmarkSyncChannelCounterAdd10(b *testing.B) {
runAdd(b, NewChannelCounter(0), 10)
}
func BenchmarkBufferedChannelCounterInc10(b *testing.B) {
runInc(b, NewChannelCounter(1024), 10)
}
func BenchmarkBufferedChannelCounterAdd10(b *testing.B) {
runAdd(b, NewChannelCounter(1024), 10)
}
func BenchmarkMutexCounterInc100(b *testing.B) {
runInc(b, &MutexCounter{}, 100)
}
func BenchmarkMutexCounterAdd100(b *testing.B) {
runAdd(b, &MutexCounter{}, 100)
}
func BenchmarkRWMutexCounterInc100(b *testing.B) {
runInc(b, &RWMutexCounter{}, 100)
}
func BenchmarkRWMutexCounterAdd100(b *testing.B) {
runAdd(b, &RWMutexCounter{}, 100)
}
func BenchmarkAtomicIntCounterInc100(b *testing.B) {
runInc(b, new(AtomicIntCounter), 100)
}
func BenchmarkAtomicIntCounterAdd100(b *testing.B) {
runAdd(b, new(AtomicIntCounter), 100)
}
func BenchmarkNaiveCounterInc100(b *testing.B) {
runInc(b, new(NaiveCounter), 100)
}
func BenchmarkNaiveCounterAdd100(b *testing.B) {
runAdd(b, new(NaiveCounter), 100)
}
func BenchmarkNaiveIntCounterInc100(b *testing.B) {
runInc(b, new(NaiveIntCounter), 100)
}
func BenchmarkNaiveIntCounterAdd100(b *testing.B) {
runAdd(b, new(NaiveIntCounter), 100)
}
func BenchmarkAtomicSpinningCounterInc100(b *testing.B) {
runInc(b, new(AtomicSpinningCounter), 100)
}
func BenchmarkAtomicSpinningCounterAdd100(b *testing.B) {
runAdd(b, new(AtomicSpinningCounter), 100)
}
func BenchmarkSyncChannelCounterInc100(b *testing.B) {
runInc(b, NewChannelCounter(0), 100)
}
func BenchmarkSyncChannelCounterAdd100(b *testing.B) {
runAdd(b, NewChannelCounter(0), 100)
}
func BenchmarkBufferedChannelCounterInc100(b *testing.B) {
runInc(b, NewChannelCounter(1024), 100)
}
func BenchmarkBufferedChannelCounterAdd100(b *testing.B) {
runAdd(b, NewChannelCounter(1024), 100)
}