-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathslot_sequencer_test.go
230 lines (189 loc) · 4.32 KB
/
slot_sequencer_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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package sonic
import (
"log"
"math/rand"
"testing"
"time"
)
func TestSlotSequencer1(t *testing.T) {
b := NewByteBuffer()
s := NewSlotSequencer(10, 1024)
letters := make(map[int][]byte)
push := func(seq int, letter byte, n int) {
var what []byte
for i := 0; i < n; i++ {
what = append(what, letter)
}
letters[seq] = what
b.Write(what)
b.Commit(n)
ok, err := s.Push(seq, b.Save(n))
if !ok || err != nil {
t.Fatalf("not pushed ok=%v err=%v", ok, err)
}
}
pop := func(seq int) {
slot, ok := s.Pop(seq)
if !ok {
t.Fatal("not popped")
}
expected := string(letters[seq])
given := string(b.SavedSlot(slot))
if expected != given {
t.Fatalf("wrong slot expected=%s given=%s", expected, given)
}
b.Discard(slot)
}
push(2, 'b', 4)
push(1, 'a', 2)
push(4, 'd', 8)
push(3, 'c', 6)
push(5, 'e', 10)
if s.Bytes() != 30 {
t.Fatal("wrong number of bytes")
}
for i := 5; i >= 1; i-- {
pop(i)
}
if s.offsetter.tree.Sum() != 0 {
t.Fatal("offsetter should have been cleared")
}
if s.Bytes() != 0 {
t.Fatal("slot manager should have 0 bytes")
}
}
func TestSlotSequencerRandom(t *testing.T) {
b := NewByteBuffer()
s := NewSlotSequencer(4096, 1024*1024)
letters := make(map[int][]byte)
push := func(seq int, letter byte, n int) {
var what []byte
for i := 0; i < n; i++ {
what = append(what, letter)
}
letters[seq] = what
b.Write(what)
b.Commit(n)
ok, err := s.Push(seq, b.Save(n))
if !ok || err != nil {
t.Fatalf("not pushed ok=%v err=%v", ok, err)
}
}
pop := func(seq int) {
slot, ok := s.Pop(seq)
if !ok {
t.Fatal("not popped")
}
expected := string(letters[seq])
given := string(b.SavedSlot(slot))
if expected != given {
t.Fatalf("wrong slot expected=%s given=%s", expected, given)
}
b.Discard(slot)
}
alphabet := []byte("abcdefghijklmnopqrstuvwxyz")
rand := rand.New(rand.NewSource(time.Now().UnixNano()))
iterations := 0
start := time.Now()
for time.Since(start).Seconds() < 30 {
var sequences []int
for seq := 1; seq <= 1024; seq++ {
sequences = append(sequences, seq)
}
for len(sequences) > 0 {
var toPop []int
nPop := rand.Int()%len(sequences) + 1
for i := 0; i < nPop; i++ {
ix := rand.Int() % len(sequences)
seq := sequences[ix]
sequences = append(sequences[:ix], sequences[ix+1:]...)
toPop = append(toPop, seq)
push(seq, alphabet[rand.Int()%len(alphabet)], rand.Int()%100+1)
}
for len(toPop) > 0 {
ix := rand.Int() % len(toPop)
pop(toPop[ix])
toPop = append(toPop[:ix], toPop[ix+1:]...)
}
}
if s.offsetter.tree.Sum() != 0 {
t.Fatal("offsetter should have been cleared")
}
if s.Bytes() != 0 {
t.Fatal("slot manager should have 0 bytes")
}
iterations++
}
log.Printf("slot manager random test iterations=%d", iterations)
}
func BenchmarkSlotSequencerPop(b *testing.B) {
const N = 1024 * 1024 /* == -benchtime */ * 8
buf := NewByteBuffer()
buf.Reserve(N)
s := NewSlotSequencer(4096, N)
for i := 0; i < N; i++ {
buf.Write([]byte("12345678"))
buf.Commit(8)
s.Push(i, buf.Save(8))
}
// $ go test -bench BenchmarkSlotSeq -v -run=^$ -benchtime=1048576x .
// -benchtime is important here otherwise the results don't make sense.
for i := 0; i < b.N; i++ {
slot, ok := s.Pop(i % N)
if ok {
buf.Discard(slot)
}
}
}
func BenchmarkSlotSequencerPushPop(b *testing.B) {
b.Run("first_in_first_out", func(b *testing.B) {
buf := NewByteBuffer()
buf.Reserve(4096)
s := NewSlotSequencer(1024, 4096)
for i := 0; i < b.N; i++ {
for j := 0; j < 1024; j++ {
buf.Claim(func(b []byte) int {
for k := 0; k < 4; k++ {
b[k] = byte(k)
}
return 4
})
buf.Commit(4)
}
for j := 0; j < 1024; j++ {
s.Push(j, buf.Save(4))
}
for j := 0; j < 1024; j++ {
slot, ok := s.Pop(j)
if ok {
buf.Discard(slot)
}
}
}
})
b.Run("first_in_last_out", func(b *testing.B) {
buf := NewByteBuffer()
buf.Reserve(4096)
s := NewSlotSequencer(1024, 4096)
for i := 0; i < b.N; i++ {
for j := 0; j < 1024; j++ {
buf.Claim(func(b []byte) int {
for k := 0; k < 4; k++ {
b[k] = byte(k)
}
return 4
})
buf.Commit(4)
}
for j := 0; j < 1024; j++ {
s.Push(j, buf.Save(4))
}
for j := 1023; j >= 0; j-- {
slot, ok := s.Pop(j)
if ok {
buf.Discard(slot)
}
}
}
})
}