-
Notifications
You must be signed in to change notification settings - Fork 5
/
cmap_go18_test.go
92 lines (80 loc) · 1.6 KB
/
cmap_go18_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
package cmap_test
import (
"context"
"fmt"
"strconv"
"testing"
"github.com/OneOfOne/cmap"
)
var keys [1e5]interface{}
func init() {
for i := range keys {
keys[i] = fmt.Sprintf("%010d", i)
}
}
func TestDistruption(t *testing.T) {
cm := cmap.NewSize(32)
for i := 0; i < 1e6; i++ {
cm.Set(i, i)
}
t.Logf("%+v", cm.ShardDistribution())
}
func TestIter(t *testing.T) {
cm := cmap.New()
for i := 0; i < 100; i++ {
cm.Set(i, i)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
i := 0
for kv := range cm.IterLocked(ctx, 1) {
t.Logf("%d: %+v", i, kv)
if i++; i > 10 {
cancel()
}
}
}
func benchCmapSetGet(b *testing.B, sz int) {
cm := cmap.NewSize(sz)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
var i int
for pb.Next() {
x := keys[i%len(keys)]
cm.Set(x, x)
if v, ok := cm.Get(x).(string); !ok || v != x {
b.Fatalf("sz: %d, wanted %v, got %v", sz, x, v)
}
i++
}
})
}
func BenchmarkCMap(b *testing.B) {
shardCounts := []int{32, 64, 128, 256, 512, 1024, 2048, 4096, 8192}
if testing.Short() {
shardCounts = shardCounts[len(shardCounts)-3:]
}
for _, sz := range shardCounts {
b.Run(strconv.Itoa(sz), func(b *testing.B) {
benchCmapSetGet(b, sz)
})
}
}
func BenchmarkLMap(b *testing.B) {
cm := cmap.NewLMapSize(cmap.DefaultShardCount)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
var i int
for pb.Next() {
x := keys[i%len(keys)]
cm.Set(x, x)
if v, ok := cm.Get(x).(string); !ok || v != x {
b.Fatalf("wanted %v, got %v", x, v)
}
i++
}
})
if testing.Verbose() {
b.Logf("size: %v", cm.Len())
}
}