-
Notifications
You must be signed in to change notification settings - Fork 38
/
rbmutex_test.go
357 lines (328 loc) · 8.28 KB
/
rbmutex_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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright notice. Initial version of the following tests was based on
// the following file from the Go Programming Language core repo:
// https://github.com/golang/go/blob/831f9376d8d730b16fb33dfd775618dffe13ce7a/src/sync/rwmutex_test.go
package xsync_test
import (
"fmt"
"runtime"
"sync"
"sync/atomic"
"testing"
. "github.com/puzpuzpuz/xsync/v3"
)
func TestRBMutexSerialReader(t *testing.T) {
const numCalls = 10
mu := NewRBMutex()
for i := 0; i < 3; i++ {
var rtokens [numCalls]*RToken
for j := 0; j < numCalls; j++ {
rtokens[j] = mu.RLock()
}
for j := 0; j < numCalls; j++ {
mu.RUnlock(rtokens[j])
}
}
}
func TestRBMutexSerialOptimisticReader(t *testing.T) {
const numCalls = 10
mu := NewRBMutex()
for i := 0; i < 3; i++ {
var rtokens [numCalls]*RToken
for j := 0; j < numCalls; j++ {
ok, rt := mu.TryRLock()
if !ok {
t.Fatalf("TryRLock failed for %d", j)
}
if rt == nil {
t.Fatalf("nil reader token for %d", j)
}
rtokens[j] = rt
}
for j := 0; j < numCalls; j++ {
mu.RUnlock(rtokens[j])
}
}
}
func TestRBMutexSerialOptimisticWriter(t *testing.T) {
mu := NewRBMutex()
for i := 0; i < 3; i++ {
if !mu.TryLock() {
t.Fatal("TryLock failed")
}
mu.Unlock()
}
}
func parallelReader(mu *RBMutex, clocked, cunlock, cdone chan bool) {
t := mu.RLock()
clocked <- true
<-cunlock
mu.RUnlock(t)
cdone <- true
}
func doTestParallelReaders(numReaders, gomaxprocs int) {
runtime.GOMAXPROCS(gomaxprocs)
mu := NewRBMutex()
clocked := make(chan bool)
cunlock := make(chan bool)
cdone := make(chan bool)
for i := 0; i < numReaders; i++ {
go parallelReader(mu, clocked, cunlock, cdone)
}
// Wait for all parallel RLock()s to succeed.
for i := 0; i < numReaders; i++ {
<-clocked
}
for i := 0; i < numReaders; i++ {
cunlock <- true
}
// Wait for the goroutines to finish.
for i := 0; i < numReaders; i++ {
<-cdone
}
}
func TestRBMutexParallelReaders(t *testing.T) {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(0))
doTestParallelReaders(1, 4)
doTestParallelReaders(3, 4)
doTestParallelReaders(4, 2)
}
func reader(mu *RBMutex, numIterations int, activity *int32, cdone chan bool) {
for i := 0; i < numIterations; i++ {
t := mu.RLock()
n := atomic.AddInt32(activity, 1)
if n < 1 || n >= 10000 {
mu.RUnlock(t)
panic(fmt.Sprintf("rlock(%d)\n", n))
}
for i := 0; i < 100; i++ {
}
atomic.AddInt32(activity, -1)
mu.RUnlock(t)
}
cdone <- true
}
func writer(mu *RBMutex, numIterations int, activity *int32, cdone chan bool) {
for i := 0; i < numIterations; i++ {
mu.Lock()
n := atomic.AddInt32(activity, 10000)
if n != 10000 {
mu.Unlock()
panic(fmt.Sprintf("wlock(%d)\n", n))
}
for i := 0; i < 100; i++ {
}
atomic.AddInt32(activity, -10000)
mu.Unlock()
}
cdone <- true
}
func hammerRBMutex(gomaxprocs, numReaders, numIterations int) {
runtime.GOMAXPROCS(gomaxprocs)
// Number of active readers + 10000 * number of active writers.
var activity int32
mu := NewRBMutex()
cdone := make(chan bool)
go writer(mu, numIterations, &activity, cdone)
var i int
for i = 0; i < numReaders/2; i++ {
go reader(mu, numIterations, &activity, cdone)
}
go writer(mu, numIterations, &activity, cdone)
for ; i < numReaders; i++ {
go reader(mu, numIterations, &activity, cdone)
}
// Wait for the 2 writers and all readers to finish.
for i := 0; i < 2+numReaders; i++ {
<-cdone
}
}
func TestRBMutex(t *testing.T) {
const n = 1000
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(0))
hammerRBMutex(1, 1, n)
hammerRBMutex(1, 3, n)
hammerRBMutex(1, 10, n)
hammerRBMutex(4, 1, n)
hammerRBMutex(4, 3, n)
hammerRBMutex(4, 10, n)
hammerRBMutex(10, 1, n)
hammerRBMutex(10, 3, n)
hammerRBMutex(10, 10, n)
hammerRBMutex(10, 5, n)
}
func optimisticReader(mu *RBMutex, numIterations int, activity *int32, cdone chan bool) {
for i := 0; i < numIterations; i++ {
if ok, t := mu.TryRLock(); ok {
n := atomic.AddInt32(activity, 1)
if n < 1 || n >= 10000 {
mu.RUnlock(t)
panic(fmt.Sprintf("rlock(%d)\n", n))
}
for i := 0; i < 100; i++ {
}
atomic.AddInt32(activity, -1)
mu.RUnlock(t)
}
}
cdone <- true
}
func optimisticWriter(mu *RBMutex, numIterations int, activity *int32, cdone chan bool) {
for i := 0; i < numIterations; i++ {
if mu.TryLock() {
n := atomic.AddInt32(activity, 10000)
if n != 10000 {
mu.Unlock()
panic(fmt.Sprintf("wlock(%d)\n", n))
}
for i := 0; i < 100; i++ {
}
atomic.AddInt32(activity, -10000)
mu.Unlock()
}
}
cdone <- true
}
func hammerOptimisticRBMutex(gomaxprocs, numReaders, numIterations int) {
runtime.GOMAXPROCS(gomaxprocs)
// Number of active readers + 10000 * number of active writers.
var activity int32
mu := NewRBMutex()
cdone := make(chan bool)
go optimisticWriter(mu, numIterations, &activity, cdone)
var i int
for i = 0; i < numReaders/2; i++ {
go optimisticReader(mu, numIterations, &activity, cdone)
}
go optimisticWriter(mu, numIterations, &activity, cdone)
for ; i < numReaders; i++ {
go optimisticReader(mu, numIterations, &activity, cdone)
}
// Wait for the 2 writers and all readers to finish.
for i := 0; i < 2+numReaders; i++ {
<-cdone
}
}
func TestRBMutex_Optimistic(t *testing.T) {
const n = 1000
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(0))
hammerOptimisticRBMutex(1, 1, n)
hammerOptimisticRBMutex(1, 3, n)
hammerOptimisticRBMutex(1, 10, n)
hammerOptimisticRBMutex(4, 1, n)
hammerOptimisticRBMutex(4, 3, n)
hammerOptimisticRBMutex(4, 10, n)
hammerOptimisticRBMutex(10, 1, n)
hammerOptimisticRBMutex(10, 3, n)
hammerOptimisticRBMutex(10, 10, n)
hammerOptimisticRBMutex(10, 5, n)
}
func hammerMixedRBMutex(gomaxprocs, numReaders, numIterations int) {
runtime.GOMAXPROCS(gomaxprocs)
// Number of active readers + 10000 * number of active writers.
var activity int32
mu := NewRBMutex()
cdone := make(chan bool)
go writer(mu, numIterations, &activity, cdone)
var i int
for i = 0; i < numReaders/2; i++ {
go reader(mu, numIterations, &activity, cdone)
}
go optimisticWriter(mu, numIterations, &activity, cdone)
for ; i < numReaders; i++ {
go optimisticReader(mu, numIterations, &activity, cdone)
}
// Wait for the 2 writers and all readers to finish.
for i := 0; i < 2+numReaders; i++ {
<-cdone
}
}
func TestRBMutex_Mixed(t *testing.T) {
const n = 1000
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(0))
hammerMixedRBMutex(1, 1, n)
hammerMixedRBMutex(1, 3, n)
hammerMixedRBMutex(1, 10, n)
hammerMixedRBMutex(4, 1, n)
hammerMixedRBMutex(4, 3, n)
hammerMixedRBMutex(4, 10, n)
hammerMixedRBMutex(10, 1, n)
hammerMixedRBMutex(10, 3, n)
hammerMixedRBMutex(10, 10, n)
hammerMixedRBMutex(10, 5, n)
}
func benchmarkRBMutex(b *testing.B, parallelism, localWork, writeRatio int) {
mu := NewRBMutex()
b.SetParallelism(parallelism)
runParallel(b, func(pb *testing.PB) {
foo := 0
for pb.Next() {
foo++
if writeRatio > 0 && foo%writeRatio == 0 {
mu.Lock()
for i := 0; i != localWork; i += 1 {
foo *= 2
foo /= 2
}
mu.Unlock()
} else {
tk := mu.RLock()
for i := 0; i != localWork; i += 1 {
foo *= 2
foo /= 2
}
mu.RUnlock(tk)
}
}
_ = foo
})
}
func BenchmarkRBMutexWorkReadOnly_HighParallelism(b *testing.B) {
benchmarkRBMutex(b, 1024, 100, -1)
}
func BenchmarkRBMutexWorkReadOnly(b *testing.B) {
benchmarkRBMutex(b, -1, 100, -1)
}
func BenchmarkRBMutexWorkWrite100000(b *testing.B) {
benchmarkRBMutex(b, -1, 100, 100000)
}
func BenchmarkRBMutexWorkWrite1000(b *testing.B) {
benchmarkRBMutex(b, -1, 100, 1000)
}
func benchmarkRWMutex(b *testing.B, parallelism, localWork, writeRatio int) {
var mu sync.RWMutex
b.SetParallelism(parallelism)
runParallel(b, func(pb *testing.PB) {
foo := 0
for pb.Next() {
foo++
if writeRatio > 0 && foo%writeRatio == 0 {
mu.Lock()
for i := 0; i != localWork; i += 1 {
foo *= 2
foo /= 2
}
mu.Unlock()
} else {
mu.RLock()
for i := 0; i != localWork; i += 1 {
foo *= 2
foo /= 2
}
mu.RUnlock()
}
}
_ = foo
})
}
func BenchmarkRWMutexWorkReadOnly_HighParallelism(b *testing.B) {
benchmarkRWMutex(b, 1024, 100, -1)
}
func BenchmarkRWMutexWorkReadOnly(b *testing.B) {
benchmarkRWMutex(b, -1, 100, -1)
}
func BenchmarkRWMutexWorkWrite100000(b *testing.B) {
benchmarkRWMutex(b, -1, 100, 100000)
}
func BenchmarkRWMutexWorkWrite1000(b *testing.B) {
benchmarkRWMutex(b, -1, 100, 1000)
}