-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtrylock_test.go
149 lines (132 loc) · 3.2 KB
/
trylock_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
package utils
import (
"runtime"
"sync"
"sync/atomic"
"testing"
"time"
)
func TestTrylock(t *testing.T) {
m := NewTryMutex()
ok := m.TryLock(20 * time.Millisecond)
if !ok {
t.Error("it should be the lock succeeded but failed")
}
ok = m.TryLock(20 * time.Millisecond)
if ok {
t.Error("It should be the lock failed but it succeeded")
}
m.Unlock()
ok = m.TryLock()
if !ok {
t.Fatal("it should be the lock succeeded but failed")
}
go func() {
time.Sleep(50 * time.Millisecond)
m.Unlock()
}()
ts := time.Now()
ok = m.TryLock(120 * time.Millisecond)
took := time.Since(ts)
if !ok {
t.Fatal("it should be the lock succeeded but failed")
}
if took < 50*time.Millisecond {
t.Fatalf("expected time to acquire lock < 100ms, got: %s", took)
}
}
func BenchmarkTrylock(b *testing.B) {
b.Run("trylock", func(b *testing.B) {
lock := NewTryMutex()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
go func() {
defer lock.Unlock()
lock.TryLock(20 * time.Millisecond)
}()
}
})
})
b.Run("lock", func(b *testing.B) {
lock := NewTryMutex()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
go func() {
defer lock.Unlock()
lock.Lock()
}()
}
})
})
b.Run("mutex", func(b *testing.B) {
var lock sync.Mutex
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
go func() {
defer lock.Unlock()
lock.Lock()
}()
}
})
})
}
/*
Ref: https://github.com/panjf2000/ants/blob/dev/pkg/sync/spinlock_test.go
Benchmark result for three types of locks:
goos: darwin
goarch: arm64
pkg: github.com/panjf2000/ants/v2/pkg/sync
BenchmarkMutex-10 10452573 111.1 ns/op 0 B/op 0 allocs/op
BenchmarkSpinLock-10 58953211 18.01 ns/op 0 B/op 0 allocs/op
BenchmarkBackOffSpinLock-10 100000000 10.81 ns/op 0 B/op 0 allocs/op
*/
type originSpinLock uint32
func (sl *originSpinLock) Lock() {
for !atomic.CompareAndSwapUint32((*uint32)(sl), 0, 1) {
runtime.Gosched()
}
}
func (sl *originSpinLock) Unlock() {
atomic.StoreUint32((*uint32)(sl), 0)
}
func NewOriginSpinLock() sync.Locker {
return new(originSpinLock)
}
func BenchmarkSpinMutex(b *testing.B) {
m := sync.Mutex{}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
m.Lock()
//nolint:staticcheck
m.Unlock()
}
})
}
func BenchmarkSpinLock(b *testing.B) {
spin := NewOriginSpinLock()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
spin.Lock()
//nolint:staticcheck
spin.Unlock()
}
})
}
func BenchmarkBackOffSpinLock(b *testing.B) {
spin := NewSpinLock()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
spin.Lock()
//nolint:staticcheck
spin.Unlock()
}
})
}
// # go test -run=^$ -benchmem -benchtime=1s -bench=Spin
// goos: linux
// goarch: amd64
// pkg: github.com/fufuok/utils
// cpu: AMD Ryzen 7 5700G with Radeon Graphics
// BenchmarkSpinMutex-16 23156875 54.36 ns/op 0 B/op 0 allocs/op
// BenchmarkSpinLock-16 215412934 5.564 ns/op 0 B/op 0 allocs/op
// BenchmarkBackOffSpinLock-16 246957524 4.877 ns/op 0 B/op 0 allocs/op