-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbench-get_test.go
180 lines (170 loc) · 4.64 KB
/
bench-get_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
package squirrel_test
import (
"errors"
"fmt"
squirrelTesting "github.com/anacrolix/squirrel/internal/testing"
"io"
"testing"
g "github.com/anacrolix/generics"
qt "github.com/frankban/quicktest"
"github.com/anacrolix/squirrel"
)
func benchCacheGets(cache *squirrel.Cache, b *testing.B) {
c := qt.New(b)
key := "hello"
value := []byte("world")
err := cache.Put(key, []byte("world"))
c.Assert(err, qt.IsNil)
b.Run("Hit", func(b *testing.B) {
for i := 0; i < b.N; i++ {
pb, err := cache.OpenPinnedReadOnly(key)
if err != nil {
b.Fatalf("error opening cache: %v", err)
}
var buf [6]byte
n, err := pb.ReadAt(buf[:], 0)
pb.Close()
if !squirrelTesting.EofOrNil(err) {
b.Fatalf("got error reading value from blob on iteration %v: %v", i, err)
}
if n != len(value) {
err = fmt.Errorf("read unexpected length %v after %v iterations", n, i)
b.Fatal(err)
}
}
})
b.Run("HitFull", func(b *testing.B) {
for i := 0; i < b.N; i++ {
var buf [6]byte
n, err := cache.ReadFull(key, buf[:])
if n != len(value) {
err = fmt.Errorf("read unexpected length %v after %v iterations: %v", n, i, err)
b.Fatal(err)
}
}
})
b.Run("HitFullTransaction", func(b *testing.B) {
err := cache.Tx(func(tx *squirrel.Tx) error {
for i := 0; i < b.N; i++ {
var buf [6]byte
n, err := tx.ReadFull(key, buf[:])
if n != len(value) {
err = fmt.Errorf("read unexpected length %v after %v iterations: %#v", n, i, err)
b.Fatal(err)
}
}
return nil
})
if err != nil {
b.Fatalf("error in transaction: %v", err)
}
})
}
func BenchmarkCacheDefaults(b *testing.B) {
c := qt.New(b)
cacheOpts := squirrel.TestingDefaultCacheOpts(b)
cache := squirrel.TestingNewCache(c, cacheOpts)
benchCacheGets(cache, b)
}
func benchmarkReadAtEndOfBlob(b *testing.B, blobSize int, readSize int, cacheOpts squirrel.NewCacheOpts) {
value := make([]byte, blobSize)
readRandSparse(value)
buf := make([]byte, readSize)
b.SetBytes(int64(readSize))
benchCache(b, cacheOpts,
func(cache *squirrel.Cache) error {
return cache.Put(defaultKey, value)
},
func(cache *squirrel.Cache) (err error) {
pb, err := cache.OpenPinnedReadOnly(defaultKey)
if err != nil {
return err
}
defer pb.Close()
n, err := pb.ReadAt(buf, int64(len(value)-len(buf)))
if err != nil && !errors.Is(err, io.EOF) {
return
}
if n != len(buf) {
panic(n)
}
return nil
})
}
// Demonstrate that reads from the end of blobs is slow without overflow caching, or pointer maps
// enabled.
func BenchmarkReadAtEndOfBlob(b *testing.B) {
autoVacuums := []nestedBench{
{"AutoVacuumNone", func(opts *squirrel.NewCacheOpts) {
opts.SetAutoVacuum = g.Some("none")
opts.RequireAutoVacuum = g.Some[any](0)
}},
{"AutoVacuumIncremental", func(opts *squirrel.NewCacheOpts) {
// Show that incremental still generates a "pointer map" in sqlite3.
opts.SetAutoVacuum = g.Some("incremental")
opts.RequireAutoVacuum = g.Some[any](2)
}},
{"AutoVacuumFull", func(opts *squirrel.NewCacheOpts) {
opts.SetAutoVacuum = g.Some("full")
opts.RequireAutoVacuum = g.Some[any](1)
}},
}
startNestedBenchmark(
b,
func(b testing.TB) squirrel.NewCacheOpts {
cacheOpts := squirrel.TestingDefaultCacheOpts(b)
// This needs to be significantly less than the blob size or the linked list of pages
// will be cached.
cacheOpts.CacheSize = g.Some[int64](-1 << 10) // 1 MiB
cacheOpts.PageSize = 4096
cacheOpts.MaxBlobSize.Set(1e9)
return cacheOpts
},
func(b *testing.B, opts func() squirrel.NewCacheOpts) {
benchmarkReadAtEndOfBlob(b, 32<<20, 4<<10, opts())
},
autoVacuums,
)
}
type nestedBench struct {
name string
withOpts func(opts *squirrel.NewCacheOpts)
}
func startNestedBenchmark(
b *testing.B,
newCacheOpts func(b testing.TB) squirrel.NewCacheOpts,
finally func(b *testing.B, opts func() squirrel.NewCacheOpts),
nested ...[]nestedBench,
) {
runNested(b, nil, newCacheOpts, nested, finally)
}
func runNested(
b *testing.B,
withOpts []func(*squirrel.NewCacheOpts),
newCacheOpts func(testing.TB) squirrel.NewCacheOpts,
nested [][]nestedBench,
finally func(b *testing.B, opts func() squirrel.NewCacheOpts),
) {
if len(nested) == 0 {
finally(b, func() squirrel.NewCacheOpts {
cacheOpts := newCacheOpts(b)
for _, withOpt := range withOpts {
withOpt(&cacheOpts)
}
return cacheOpts
})
return
}
runRest := func(b *testing.B, nb nestedBench) {
runNested(b, append(withOpts, nb.withOpts), newCacheOpts, nested[1:], finally)
}
if len(nested[0]) == 1 {
runRest(b, nested[0][0])
} else {
for _, n := range nested[0] {
b.Run(n.name, func(b *testing.B) {
runRest(b, n)
})
}
}
}