This repository has been archived by the owner on May 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmap_test.go
182 lines (155 loc) · 3.17 KB
/
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
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
package synapse
import (
"sync"
"testing"
)
func TestWaiterMap(t *testing.T) {
mp := &wMap{}
q := mp.remove(4309821)
if q != nil {
t.Error("remove() should return 'nil' from an empty map")
}
vals := make([]waiter, 1000)
for i := range vals {
vals[i].seq = uint64(i) * 17
}
for i := range vals {
mp.insert(&vals[i])
}
if mp.length() != 1000 {
t.Errorf("expected map to have 1000 elements; found %d", mp.length())
}
for i := range vals {
q := mp.remove(vals[i].seq)
if q != &vals[i] {
t.Errorf("expected %v; got %v", &vals[i], q)
}
}
l := mp.length()
if l != 0 {
t.Errorf("expected map to have 0 elements; found %d", l)
}
mp.insert(&vals[200])
mp.flush(nil)
if mp.length() != 0 {
t.Errorf("expected map length to be 0 after flush(); found %d", mp.length())
}
for i := range vals {
mp.insert(&vals[i])
}
l = mp.length()
if l != 1000 {
t.Errorf("expected 1000 elements after re-insertion; found %d", l)
}
mp.reap()
l = mp.length()
if l != 1000 {
t.Errorf("expected 1000 elements after first reap(); found %d", l)
}
mp.reap()
l = mp.length()
if l != 0 {
t.Errorf("expected 0 elements after second reap(); found %d", l)
}
for i := range vals {
vals[i].reap = false
mp.insert(&vals[i])
}
// non-sequential removal
for i := range vals {
x := &vals[len(vals)-i-1]
v := mp.remove(x.seq)
if v == nil {
t.Errorf("index %d: no value returned", len(vals)-i-1)
}
if v != x {
t.Errorf("expected %v out; got %v", x, v)
}
}
}
func seqInsert(mp *wMap, b *testing.B, num int) {
vals := make([]waiter, num)
for i := range vals {
vals[i].seq = uint64(i)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := range vals {
mp.insert(&vals[j])
}
for j := range vals {
mp.remove(uint64(j))
}
}
}
func stdInsert(mp map[uint64]*waiter, b *testing.B, num int) {
vals := make([]waiter, num)
for i := range vals {
vals[i].seq = uint64(i)
}
// the mutex is just for symmetry
// with the other map implementation,
// as it does one lock per insert/delete
var m sync.Mutex
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := range vals {
m.Lock()
mp[vals[j].seq] = &vals[j]
m.Unlock()
}
for j := range vals {
m.Lock()
if mp[vals[j].seq] != nil {
delete(mp, vals[j].seq)
}
m.Unlock()
}
}
}
func BenchmarkMapInsertDelete(b *testing.B) {
var mp wMap
w := &waiter{}
w.seq = 39082134
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
mp.insert(w)
mp.remove(w.seq)
}
}
func BenchmarkStdMapInsertDelete(b *testing.B) {
mp := make(map[uint64]*waiter)
w := &waiter{}
w.seq = 39082134
b.ReportAllocs()
b.ResetTimer()
var m sync.Mutex
for i := 0; i < b.N; i++ {
m.Lock()
mp[w.seq] = w
m.Unlock()
m.Lock()
w = mp[w.seq]
delete(mp, w.seq)
m.Unlock()
}
}
func Benchmark1000Sequential(b *testing.B) {
var mp wMap
seqInsert(&mp, b, 1000)
}
func Benchmark500Sequential(b *testing.B) {
var mp wMap
seqInsert(&mp, b, 500)
}
func Benchmark1000StdSequential(b *testing.B) {
mp := make(map[uint64]*waiter)
stdInsert(mp, b, 1000)
}
func Benchmark500StdSequential(b *testing.B) {
mp := make(map[uint64]*waiter)
stdInsert(mp, b, 500)
}