-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_bench_test.go
104 lines (94 loc) · 2.01 KB
/
map_bench_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
package ash
import (
"math/rand"
"sync"
"sync/atomic"
"testing"
)
func BenchmarkSyncMap_70Load20Store10Delete(b *testing.B) {
var cache sync.Map
keys := generateIntKeys(1000000)
total := len(keys) - 1
var (
storeCnt int64
loadCnt int64
deleteCnt int64
totalCnt int64
)
// for i := 0; i < len(keys); i++ {
// cache.Store(keys[i], i)
// }
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
var cnt int
for pb.Next() {
atomic.AddInt64(&totalCnt, 1)
cnt++
r := rand.Intn(10)
if r < 1 {
cache.Delete(keys[cnt%total])
atomic.AddInt64(&deleteCnt, 1)
continue
}
if r < 3 {
cache.Store(keys[cnt%total], cnt)
atomic.AddInt64(&storeCnt, 1)
continue
}
cache.Load(keys[cnt%total])
atomic.AddInt64(&loadCnt, 1)
}
})
b.Cleanup(func() {
b.Log("sync.Map total calls to Store/Delete/Load: ",
atomic.LoadInt64(&storeCnt), "/",
atomic.LoadInt64(&deleteCnt), "/",
atomic.LoadInt64(&loadCnt), "/",
)
b.Log("Execution time: ", b.Elapsed())
})
}
func BenchmarkAshMap_70Load20Store10Delete(b *testing.B) {
cache := new(Map).From(NewSkipList(32))
keys := generateIntKeys(1000000)
total := len(keys) - 1
var (
storeCnt int64
loadCnt int64
deleteCnt int64
totalCnt int64
)
// for i := 0; i < len(keys); i++ {
// cache.Store(keys[i], i)
// }
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
var cnt int
for pb.Next() {
atomic.AddInt64(&totalCnt, 1)
cnt++
r := rand.Intn(10)
if r < 1 {
cache.Delete(keys[cnt%total])
atomic.AddInt64(&deleteCnt, 1)
continue
}
if r < 3 {
cache.Store(keys[cnt%total], cnt)
atomic.AddInt64(&storeCnt, 1)
continue
}
cache.Load(keys[cnt%total])
atomic.AddInt64(&loadCnt, 1)
}
})
b.Cleanup(func() {
b.Log("ash.Map total calls to Store/Delete/Load: ",
atomic.LoadInt64(&storeCnt), "/",
atomic.LoadInt64(&deleteCnt), "/",
atomic.LoadInt64(&loadCnt), "/",
)
//" total: ", atomic.LoadInt64(&totalCnt))
b.Log("Execution time: ", b.Elapsed())
})
}