-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbench-utils_test.go
67 lines (58 loc) · 1.2 KB
/
bench-utils_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
package squirrel_test
import (
"math/rand"
"testing"
"time"
qt "github.com/frankban/quicktest"
"github.com/anacrolix/squirrel"
)
func benchCache(
b *testing.B,
cacheOpts squirrel.NewCacheOpts,
setup func(cache *squirrel.Cache) error,
loop func(cache *squirrel.Cache) error,
) {
c := qt.New(b)
cache := squirrel.TestingNewCache(c, cacheOpts)
err := setup(cache)
c.Assert(err, qt.IsNil)
started := time.Now()
b.ResetTimer()
for i := 0; i < b.N; i++ {
err = loop(cache)
if err != nil {
c.Fatalf("error in iteration %v after %v: %v", i, time.Since(started), err)
}
}
}
func benchCacheWrapLoop(
b *testing.B,
cacheOpts squirrel.NewCacheOpts,
setup func(cache *squirrel.Cache) error,
loop func(cache *squirrel.Cache) error,
) {
c := qt.New(b)
cache := squirrel.TestingNewCache(c, cacheOpts)
err := setup(cache)
c.Assert(err, qt.IsNil)
b.ResetTimer()
err = loop(cache)
b.StopTimer()
c.Assert(err, qt.IsNil)
}
const defaultKey = "hello"
var defaultValue = []byte("world")
func readRandSlow(b []byte) {
n, err := rand.Read(b)
if err != nil {
panic(err)
}
if n != len(b) {
panic(n)
}
}
func readRandSparse(b []byte) {
for i := 0; i < 10; i++ {
b[rand.Intn(len(b))] = byte(i)
}
}