-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmap_bench_test.go
196 lines (152 loc) · 2.92 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package lrc
import (
"math/rand"
"sync"
"testing"
)
const test_entry_size int = 1000
type testMap interface {
Put(k, v int)
Get(k int) (int, bool)
}
type LockMap struct {
m map[int]int
lock sync.RWMutex
}
func (l *LockMap) Put(k, v int) {
l.lock.Lock()
l.m[k] = v
l.lock.Unlock()
}
func (l *LockMap) Get(k int) (int, bool) {
l.lock.RLock()
v, ok := l.m[k]
l.lock.RUnlock()
return v, ok
}
func InitLockMap(num int) *LockMap {
lockmap := &LockMap{
m: make(map[int]int),
lock: sync.RWMutex{},
}
for i := 0; i < num; i++ {
lockmap.Put(i, i)
}
return lockmap
}
func InitLRMap(num int) *LRMap {
lrmap := newIntMap()
for i := 0; i < num; i++ {
lrmap.Put(i, i)
}
return lrmap
}
func BenchmarkLRMap_Write(b *testing.B) {
lrmap := InitLRMap(0)
for i := 0; i < b.N; i++ {
k := rand.Intn(10000)
lrmap.Put(k, k)
}
}
func BenchmarkLockMap_Write(b *testing.B) {
lockmap := InitLockMap(0)
for i := 0; i < b.N; i++ {
k := rand.Intn(10000)
lockmap.Put(k, k)
}
}
func BenchmarkLRMap_Read(b *testing.B) {
lrmap := InitLRMap(1000000)
for i := 0; i < b.N; i++ {
k := rand.Intn(1000000)
lrmap.Get(k)
}
}
func BenchmarkLockMap_Read(b *testing.B) {
lockmap := InitLockMap(1000000)
for i := 0; i < b.N; i++ {
k := rand.Intn(1000000)
lockmap.Get(k)
}
}
func run(m testMap, reader int) {
wg := sync.WaitGroup{}
for k := 0; k < reader; k++ {
wg.Add(1)
go func() {
for i := 0; i < test_entry_size; i++ {
m.Get(i)
}
wg.Done()
}()
}
wg.Add(1)
go func() {
for i := 0; i < test_entry_size; i++ {
k := i
m.Put(k, k+1)
}
wg.Done()
}()
wg.Wait()
}
func BenchmarkLRMap_Read_Write_5_1(b *testing.B) {
m := InitLRMap(test_entry_size)
for i := 0; i < b.N; i++ {
run(m, 5)
}
}
func BenchmarkLockMap_Read_Write_5_1(b *testing.B) {
m := InitLockMap(test_entry_size)
for i := 0; i < b.N; i++ {
run(m, 5)
}
}
func BenchmarkLRMap_Read_Write_10_1(b *testing.B) {
m := InitLRMap(test_entry_size)
for i := 0; i < b.N; i++ {
run(m, 10)
}
}
func BenchmarkLockMap_Read_Write_10_1(b *testing.B) {
m := InitLockMap(test_entry_size)
for i := 0; i < b.N; i++ {
run(m, 10)
}
}
func BenchmarkLRMap_Read_Write_50_1(b *testing.B) {
m := InitLRMap(test_entry_size)
for i := 0; i < b.N; i++ {
run(m, 50)
}
}
func BenchmarkLockMap_Read_Write_50_1(b *testing.B) {
m := InitLockMap(test_entry_size)
for i := 0; i < b.N; i++ {
run(m, 50)
}
}
func BenchmarkLRMap_Read_Write_100_1(b *testing.B) {
m := InitLRMap(test_entry_size)
for i := 0; i < b.N; i++ {
run(m, 100)
}
}
func BenchmarkLockMap_Read_Write_100_1(b *testing.B) {
m := InitLockMap(test_entry_size)
for i := 0; i < b.N; i++ {
run(m, 100)
}
}
func BenchmarkLRMap_Read_Write_500_1(b *testing.B) {
m := InitLRMap(test_entry_size)
for i := 0; i < b.N; i++ {
run(m, 500)
}
}
func BenchmarkLockMap_Read_Write_500_1(b *testing.B) {
m := InitLockMap(test_entry_size)
for i := 0; i < b.N; i++ {
run(m, 100)
}
}