-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsieve_test.go
274 lines (227 loc) · 5.13 KB
/
sieve_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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// sieve_test.go - test harness for sieve cache
//
// (c) 2024 Sudhi Herle <sudhi@herle.net>
//
// Copyright 2024- Sudhi Herle <sw-at-herle-dot-net>
// License: BSD-2-Clause
//
// If you need a commercial license for this work, please contact
// the author.
//
// This software does not come with any express or implied
// warranty; it is provided "as is". No claim is made to its
// suitability for any purpose.
package sieve_test
import (
"encoding/binary"
"fmt"
"math/rand"
"runtime"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/opencoff/go-sieve"
)
func TestBasic(t *testing.T) {
assert := newAsserter(t)
s := sieve.New[int, string](4)
ok := s.Add(1, "hello")
assert(!ok, "empty cache: expected clean add of 1")
ok = s.Add(2, "foo")
assert(!ok, "empty cache: expected clean add of 2")
ok = s.Add(3, "bar")
assert(!ok, "empty cache: expected clean add of 3")
ok = s.Add(4, "gah")
assert(!ok, "empty cache: expected clean add of 4")
ok = s.Add(1, "world")
assert(ok, "key 1: expected to replace")
ok = s.Add(5, "boo")
assert(!ok, "adding 5: expected to be new add")
_, ok = s.Get(2)
assert(!ok, "evict: expected 2 to be evicted")
}
func TestEvictAll(t *testing.T) {
assert := newAsserter(t)
size := 128
s := sieve.New[int, string](size)
for i := 0; i < size*2; i++ {
val := fmt.Sprintf("val %d", i)
_, ok := s.Probe(i, val)
assert(!ok, "%d: exp new add", i)
}
// the first half should've been all evicted
for i := 0; i < size; i++ {
_, ok := s.Get(i)
assert(!ok, "%d: exp to be evicted", i)
}
// leaving the second half intact
for i := size; i < size*2; i++ {
ok := s.Delete(i)
assert(ok, "%d: exp del on existing cache elem")
}
}
func TestAllOps(t *testing.T) {
size := 8192
vals := randints(size * 3)
s := sieve.New[uint64, uint64](size)
for i := range vals {
k := vals[i]
s.Add(k, k)
}
vals = shuffle(vals)
var hit, miss int
for i := range vals {
k := vals[i]
_, ok := s.Get(k)
if ok {
hit++
} else {
miss++
}
}
t.Logf("%d items: hit %d, miss %d, ratio %4.2f\n", len(vals), hit, miss, float64(hit)/float64(hit+miss))
}
type timing struct {
typ string
d time.Duration
hit, miss uint64
}
type barrier atomic.Uint64
func (b *barrier) Wait() {
v := (*atomic.Uint64)(b)
for {
if v.Load() == 1 {
return
}
runtime.Gosched()
}
}
func (b *barrier) Signal() {
v := (*atomic.Uint64)(b)
v.Store(1)
}
func TestSpeed(t *testing.T) {
size := 32768
vals := randints(size * 3)
//valr := shuffle(vals)
// we will start 4 types of workers: add, get, del, probe
// each worker will be working on a shuffled version of
// the uint64 array.
for ncpu := 2; ncpu <= 32; ncpu *= 2 {
var wg sync.WaitGroup
wg.Add(ncpu)
s := sieve.New[uint64, uint64](size)
var bar barrier
// number of workers of each type
m := ncpu / 2
ch := make(chan timing, m)
for i := 0; i < m; i++ {
go func(ch chan timing, wg *sync.WaitGroup) {
var hit, miss uint64
bar.Wait()
st := time.Now()
// shuffled array
for _, x := range vals {
v := x % 16384
if _, ok := s.Get(v); ok {
hit++
} else {
miss++
}
}
d := time.Now().Sub(st)
ch <- timing{
typ: "get",
d: d,
hit: hit,
miss: miss,
}
wg.Done()
}(ch, &wg)
go func(ch chan timing, wg *sync.WaitGroup) {
var hit, miss uint64
bar.Wait()
st := time.Now()
for _, x := range vals {
v := x % 16384
if _, ok := s.Probe(v, v); ok {
hit++
} else {
miss++
}
}
d := time.Now().Sub(st)
ch <- timing{
typ: "probe",
d: d,
hit: hit,
miss: miss,
}
wg.Done()
}(ch, &wg)
}
bar.Signal()
// wait for goroutines to end and close the chan
go func() {
wg.Wait()
close(ch)
}()
// now harvest timing
times := map[string]timing{}
for tm := range ch {
if v, ok := times[tm.typ]; ok {
z := (int64(v.d) + int64(tm.d)) / 2
v.d = time.Duration(z)
v.hit = (v.hit + tm.hit) / 2
v.miss = (v.miss + tm.miss) / 2
times[tm.typ] = v
} else {
times[tm.typ] = tm
}
}
var out strings.Builder
fmt.Fprintf(&out, "Tot CPU %d, workers/type %d %d elems\n", ncpu, m, len(vals))
for _, v := range times {
var ratio string
ns := toNs(int64(v.d), len(vals), m)
ratio = hitRatio(v.hit, v.miss)
fmt.Fprintf(&out, "%6s %4.2f ns/op%s\n", v.typ, ns, ratio)
}
t.Logf(out.String())
}
}
func dup[T ~[]E, E any](v T) []E {
n := len(v)
g := make([]E, n)
copy(g, v)
return g
}
func shuffle[T ~[]E, E any](v T) []E {
i := len(v)
for i--; i >= 0; i-- {
j := rand.Intn(i + 1)
v[i], v[j] = v[j], v[i]
}
return v
}
func toNs(tot int64, nvals, ncpu int) float64 {
return (float64(tot) / float64(nvals)) / float64(ncpu)
}
func hitRatio(hit, miss uint64) string {
r := float64(hit) / float64(hit+miss)
return fmt.Sprintf(" hit-ratio %4.2f (hit %d, miss %d)", r, hit, miss)
}
func randints(sz int) []uint64 {
var b [8]byte
v := make([]uint64, sz)
for i := 0; i < sz; i++ {
n, err := rand.Read(b[:])
if n != 8 || err != nil {
panic("can't generate rand")
}
v[i] = binary.BigEndian.Uint64(b[:])
}
return v
}