-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmapstore_test.go
109 lines (102 loc) · 2.46 KB
/
mapstore_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
105
106
107
108
109
package mapstore
import (
"fmt"
"math/rand"
"sync"
"testing"
)
func BenchmarkShard1Read4Write4(b *testing.B) {
bench(1, 4, 4, 1000000, b)
}
func BenchmarkShard10Read4Write4(b *testing.B) {
bench(10, 4, 4, 1000000, b)
}
func BenchmarkShard100Read4Write4(b *testing.B) {
bench(100, 4, 4, 1000000, b)
}
func BenchmarkShard1000Read4Write4(b *testing.B) {
bench(1000, 4, 4, 1000000, b)
}
func BenchmarkShard1Read8Write2(b *testing.B) {
bench(1, 8, 2, 1000000, b)
}
func BenchmarkShard10Read8Write2(b *testing.B) {
bench(10, 8, 2, 1000000, b)
}
func BenchmarkShard100Read8Write2(b *testing.B) {
bench(100, 8, 2, 1000000, b)
}
func BenchmarkShard1000Read8Write2(b *testing.B) {
bench(1000, 8, 2, 1000000, b)
}
func BenchmarkShard1Read2Write8(b *testing.B) {
bench(1, 2, 8, 1000000, b)
}
func BenchmarkShard10Read2Write8(b *testing.B) {
bench(10, 2, 8, 1000000, b)
}
func BenchmarkShard100Read2Write8(b *testing.B) {
bench(100, 2, 8, 1000000, b)
}
func BenchmarkShard1000Read2Write8(b *testing.B) {
bench(1000, 2, 8, 1000000, b)
}
func BenchmarkShard1Read8Write0(b *testing.B) {
bench(1, 8, 0, 1000000, b)
}
func BenchmarkShard10Read8Write0(b *testing.B) {
bench(10, 8, 0, 1000000, b)
}
func BenchmarkShard100Read8Write0(b *testing.B) {
bench(100, 8, 0, 1000000, b)
}
func BenchmarkShard1000Read8Write0(b *testing.B) {
bench(1000, 8, 0, 1000000, b)
}
func TestShardsStat(t *testing.T) {
keys := genKeys(1e6)
shards := 4
store := NewWithSize(shards)
for i := 0; i < 1e7; i++ {
key := keys[i%len(keys)]
store.Set(key, key)
}
t.Logf("stats: %v", store.ShardStats())
}
func genKeys(count int) []string {
keys := make([]string, count)
for i := 0; i < len(keys); i++ {
keys[i] = fmt.Sprintf("%d", i)
}
return keys
}
func bench(shards, readThreads, writeThreads int, keysCount int, b *testing.B) {
keys := genKeys(keysCount)
store := NewWithSize(shards)
wg := sync.WaitGroup{}
b.ResetTimer()
wg.Add(readThreads + writeThreads)
for i := 0; i < writeThreads; i++ {
go testWrites(store, keys, b.N, &wg)
}
for i := 0; i < readThreads; i++ {
go testReads(store, keys, b.N, &wg)
}
wg.Wait()
}
func testWrites(s Store, keys []string, num int, wg *sync.WaitGroup) {
defer wg.Done()
lenKeys := len(keys)
for i := 0; i < num; i++ {
key := keys[rand.Int()%lenKeys]
s.Set(key, key)
}
}
func testReads(s Store, keys []string, num int, wg *sync.WaitGroup) {
defer wg.Done()
lenKeys := len(keys)
for i := 0; i < num; i++ {
key := keys[rand.Int()%lenKeys]
s.Get(key, key)
}
}