-
Notifications
You must be signed in to change notification settings - Fork 160
/
once_test.go
50 lines (42 loc) · 818 Bytes
/
once_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
// MIT License
// Copyright (c) 2020 Qi Yin <qiyin@thinkeridea.com>
package benchmark
import (
"sync"
"testing"
"unsafe"
"github.com/thinkeridea/go-extend/exsync"
)
type one int
func (o *one) Increment() {
*o++
}
func BenchmarkSyncOnce(b *testing.B) {
var once sync.Once
f := func() {
_ = new(one)
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
once.Do(f)
}
})
}
func BenchmarkOnce(b *testing.B) {
var once exsync.Once
f := func() interface{} { return new(one) }
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = once.Do(f).(*one)
}
})
}
func BenchmarkOncePointer(b *testing.B) {
var once exsync.OncePointer
f := func() unsafe.Pointer { return unsafe.Pointer(new(one)) }
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = (*one)(once.Do(f))
}
})
}