-
Notifications
You must be signed in to change notification settings - Fork 1
/
sc_test.go
79 lines (65 loc) · 1.89 KB
/
sc_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
package sc
import (
"math/rand"
"strconv"
"strings"
"time"
)
// Filter iterates over elements of collection, returning an array of all elements predicate returns truthy for.
// Copied from github.com/samber/lo.
func Filter[V any](collection []V, predicate func(V, int) bool) []V {
var result []V
for i, item := range collection {
if predicate(item, i) {
result = append(result, item)
}
}
return result
}
// Map manipulates a slice and transforms it to a slice of another type.
// Copied from github.com/samber/lo.
func Map[T any, R any](collection []T, iteratee func(T, int) R) []R {
result := make([]R, len(collection))
for i, item := range collection {
result[i] = iteratee(item, i)
}
return result
}
type testCase struct {
name string
cacheOpts []CacheOption
}
func nonStrictCaches(cap int) []testCase {
return []testCase{
{name: "map cache", cacheOpts: []CacheOption{WithMapBackend(cap)}},
{name: "LRU cache", cacheOpts: []CacheOption{WithLRUBackend(cap)}},
{name: "2Q cache", cacheOpts: []CacheOption{With2QBackend(cap)}},
}
}
func strictCaches(cap int) []testCase {
return Map(nonStrictCaches(cap), func(t testCase, _ int) testCase {
return testCase{
name: "strict " + t.name,
cacheOpts: append(t.cacheOpts, EnableStrictCoalescing()),
}
})
}
func allCaches(cap int) []testCase {
return append(nonStrictCaches(cap), strictCaches(cap)...)
}
func evictingCaches(cap int) []testCase {
return Filter(allCaches(cap), func(c testCase, _ int) bool { return !strings.HasSuffix(c.name, "map cache") })
}
func newZipfian(s, v float64, size uint64) func() string {
zipf := rand.NewZipf(rand.New(rand.NewSource(time.Now().UnixNano())), s, v, size)
return func() string {
return strconv.Itoa(int(zipf.Uint64()))
}
}
func newKeys(next func() string, size int) []string {
keys := make([]string, size)
for i := 0; i < size; i++ {
keys[i] = next()
}
return keys
}