-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathcallback_bench_test.go
More file actions
85 lines (71 loc) · 1.77 KB
/
Copy pathcallback_bench_test.go
File metadata and controls
85 lines (71 loc) · 1.77 KB
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
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
//go:build cgo
// +build cgo
package sqlite3
import (
"sync"
"sync/atomic"
"testing"
"unsafe"
)
func BenchmarkHandleLookupParallel(b *testing.B) {
d := SQLiteDriver{}
conn, err := d.Open(":memory:")
if err != nil {
b.Fatal(err)
}
defer conn.Close()
c := conn.(*SQLiteConn)
handle := newHandle(c, func() {})
benchmarkHandleLookupParallel(b, func() any {
return lookupHandle(handle)
})
}
func BenchmarkHandleLookupBeforeAfter(b *testing.B) {
value := handleVal{val: func() {}}
handle := unsafe.Pointer(&value)
before := mutexHandleTable{vals: map[unsafe.Pointer]handleVal{handle: value}}
after := atomicHandleTable{}
after.vals.Store(map[unsafe.Pointer]handleVal{handle: value})
b.Run("before_mutex", func(b *testing.B) {
benchmarkHandleLookupParallel(b, func() any {
return before.lookup(handle).val
})
})
b.Run("after_atomic", func(b *testing.B) {
benchmarkHandleLookupParallel(b, func() any {
return after.lookup(handle).val
})
})
}
func benchmarkHandleLookupParallel(b *testing.B, lookup func() any) {
b.Helper()
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if lookup() == nil {
b.Fatal("lookup returned nil")
}
}
})
}
type mutexHandleTable struct {
mu sync.Mutex
vals map[unsafe.Pointer]handleVal
}
func (t *mutexHandleTable) lookup(handle unsafe.Pointer) handleVal {
t.mu.Lock()
defer t.mu.Unlock()
return t.vals[handle]
}
type atomicHandleTable struct {
vals atomic.Value
}
func (t *atomicHandleTable) lookup(handle unsafe.Pointer) handleVal {
m, _ := t.vals.Load().(map[unsafe.Pointer]handleVal)
return m[handle]
}