-
Notifications
You must be signed in to change notification settings - Fork 3
/
sharded_map_test.go
79 lines (67 loc) · 1.14 KB
/
sharded_map_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
package shardmap
import (
"sync"
"testing"
)
func TestMap(t *testing.T) {
sm := NewShardedMap[int, int](1000, 10, HashInt)
sm.Put(1, 10)
if !sm.Has(1) {
t.Fail()
}
if v, ok := sm.Get(1); ok {
if v != 10 {
t.Fail()
}
}
}
func BenchmarkSyncMapPut(b *testing.B) {
var m sync.Map
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.Store(i, i)
}
}
func BenchmarkSyncMapGet(b *testing.B) {
var m sync.Map
for i := 0; i < b.N; i++ {
m.Store(i, i)
}
b.ResetTimer()
var v int
for i := 0; i < b.N; i++ {
if val, ok := m.Load(i); ok {
v = val.(int)
}
}
_ = v
}
func BenchmarkShardMapPut(b *testing.B) {
sm := NewShardedMap[int, int](b.N, 1, HashInt)
b.ResetTimer()
for i := 0; i < b.N; i++ {
sm.Put(i, i)
}
}
func BenchmarkShardMapGet(b *testing.B) {
sm := NewShardedMap[int, int](b.N, 1, HashInt)
for i := 0; i < b.N; i++ {
sm.Put(i, i)
}
b.ResetTimer()
var v int
for i := 0; i < b.N; i++ {
v, _ = sm.Get(i)
}
_ = v
}
func BenchmarkShardMapIter(b *testing.B) {
sm := NewShardedMap[int, int](b.N, 1, HashInt)
for i := 0; i < b.N; i++ {
sm.Put(i, i)
}
b.ResetTimer()
for kv := range sm.Iter() {
_ = kv
}
}