-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark_test.go
351 lines (284 loc) · 7.79 KB
/
benchmark_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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package sqlitezstd_test
import (
"database/sql"
"fmt"
"log/slog"
"math/rand"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
"time"
"github.com/brianvoe/gofakeit/v7"
sqlitezstd "github.com/jtarchie/sqlitezstd"
_ "github.com/mattn/go-sqlite3" // ensure you import the SQLite3 driver
"github.com/onsi/gomega/gexec"
)
// nolint: gosec
func randomBoundingBox() (float64, float64, float64, float64) {
minX := rand.Float64() * 100
maxX := minX + rand.Float64()*10
minY := rand.Float64() * 100
maxY := minY + rand.Float64()*10
return minX, maxX, minY, maxY
}
// nolint: gochecknoglobals
var dbPath, zstPath string
// setupDB prepares a database for benchmarking.
// It returns the path of the created database and a cleanup function.
// nolint: cyclop
func setupDB(b *testing.B) (string, string) {
b.Helper()
if dbPath != "" {
return dbPath, zstPath
}
_ = sqlitezstd.Init()
buildPath, err := os.MkdirTemp("", "")
if err != nil {
b.Fatalf("Failed to create temp directory: %v", err)
}
dbPath = filepath.Join(buildPath, "test.sqlite")
client, err := sql.Open("sqlite3", dbPath)
if err != nil {
b.Fatalf("Failed to open database: %v", err)
}
_, err = client.Exec(`
CREATE TABLE entries (
value INTEGER,
sentence TEXT,
minX REAL,
maxX REAL,
minY REAL,
maxY REAL
);
CREATE VIRTUAL TABLE demo_rtree USING rtree(
id INTEGER PRIMARY KEY,
minX REAL,
maxX REAL,
minY REAL,
maxY REAL
);
`)
if err != nil {
b.Fatalf("Failed to create table: %v", err)
}
transaction, err := client.Begin()
if err != nil {
b.Fatalf("Failed to start transaction: %v", err)
}
defer func() { _ = transaction.Rollback() }()
insertEntry, err := transaction.Prepare("INSERT INTO entries (value, sentence, minX, maxX, minY, maxY) VALUES (?, ?, ?, ?, ?, ?)")
if err != nil {
b.Fatalf("Failed to entry prepare: %v", err)
}
defer insertEntry.Close()
slog.Info("insert.start")
for range 1_000_000 {
//nolint: gosec
minX, maxX, minY, maxY := randomBoundingBox()
_, err = insertEntry.Exec(rand.Int63(), gofakeit.Sentence(100), minX, maxX, minY, maxY)
if err != nil {
b.Fatalf("Failed to insert data: %v", err)
}
}
slog.Info("insert.done")
_ = transaction.Commit()
slog.Info("tx.done")
// index reduces number of page loads
_, err = client.Exec(`
CREATE INDEX aindex ON entries(value);
CREATE VIRTUAL TABLE entries_porter USING fts5(sentence, tokenize="porter unicode61");
INSERT INTO entries_porter(rowid, sentence)
SELECT rowid, sentence FROM entries;
INSERT INTO demo_rtree (id, minX, maxX, minY, maxY)
SELECT rowid, minX, maxX, minY, maxY from entries;
INSERT INTO entries_porter(entries_porter) VALUES ('optimize');
PRAGMA page_size = 4096;
VACUUM;
PRAGMA optimize;
`)
if err != nil {
b.Fatalf("Failed to create index: %v", err)
}
slog.Info("commit.end")
// Assuming the compression step is the same as in the test,
// and that it's already correctly set up and works.
zstPath = dbPath + ".zst"
command := exec.Command(
"go", "run", "github.com/SaveTheRbtz/zstd-seekable-format-go/cmd/zstdseek",
"-f", dbPath,
"-o", zstPath,
"-c", "16:32:64",
)
session, err := gexec.Start(command, os.Stderr, os.Stderr)
if err != nil {
b.Fatalf("Failed to compress data: %v", err)
}
timeout := time.NewTimer(30 * time.Second)
defer timeout.Stop()
select {
case <-session.Exited:
if session.ExitCode() != 0 {
panic("something went wrong compressing")
}
case <-timeout.C:
panic("something timeout wrong compressing")
}
slog.Info("compression.end")
return dbPath, zstPath
}
// Benchmark reading from the uncompressed SQLite file.
func BenchmarkReadUncompressedSQLite(b *testing.B) {
dbPath, _ := setupDB(b)
client, err := sql.Open("sqlite3", dbPath)
if err != nil {
b.Fatalf("Failed to open database: %v", err)
}
defer client.Close()
client.SetMaxOpenConns(max(4, runtime.NumCPU()))
b.ResetTimer() // Start timing now.
b.RunParallel(func(pb *testing.PB) {
var count int
for pb.Next() {
err = client.QueryRow("SELECT MAX(value) FROM entries").Scan(&count)
if err != nil {
b.Fatalf("Query failed: %v", err)
}
}
})
}
func BenchmarkReadUncompressedRtreeSQLite(b *testing.B) {
dbPath, _ := setupDB(b)
client, err := sql.Open("sqlite3", dbPath)
if err != nil {
b.Fatalf("Failed to open database: %v", err)
}
defer client.Close()
client.SetMaxOpenConns(max(4, runtime.NumCPU()))
b.ResetTimer() // Start timing now.
b.RunParallel(func(pb *testing.PB) {
var count int
for pb.Next() {
err = client.QueryRow(`
SELECT
COUNT(*)
FROM demo_rtree
WHERE
minX <= -1 AND 1 <= maxX AND
minY <= -1 AND 1 <= maxY
`).Scan(&count)
if err != nil {
b.Fatalf("Query failed: %v", err)
}
}
})
}
func BenchmarkReadUncompressedSQLiteFTS5Porter(b *testing.B) {
dbPath, _ := setupDB(b)
client, err := sql.Open("sqlite3", dbPath)
if err != nil {
b.Fatalf("Failed to open database: %v", err)
}
defer client.Close()
client.SetMaxOpenConns(max(4, runtime.NumCPU()))
b.ResetTimer() // Start timing now.
b.RunParallel(func(pb *testing.PB) {
var count int
for pb.Next() {
err = client.QueryRow("SELECT COUNT(*) FROM entries_porter WHERE entries_porter MATCH 'alligator OR work'").Scan(&count)
if err != nil {
b.Fatalf("Query failed: %v", err)
}
}
})
}
// Benchmark reading from the compressed SQLite file.
func BenchmarkReadCompressedSQLite(b *testing.B) {
_, zstPath := setupDB(b)
client, err := sql.Open("sqlite3", fmt.Sprintf("%s?vfs=zstd", zstPath))
if err != nil {
b.Fatalf("Failed to open database: %v", err)
}
defer client.Close()
client.SetMaxOpenConns(max(4, runtime.NumCPU()))
b.ResetTimer() // Start timing now.
b.RunParallel(func(pb *testing.PB) {
var count int
for pb.Next() {
err = client.QueryRow("SELECT MAX(value) FROM entries").Scan(&count)
if err != nil {
b.Fatalf("Query failed: %v", err)
}
}
})
}
func BenchmarkReadCompressedSQLiteFTS5Porter(b *testing.B) {
_, zstPath := setupDB(b)
client, err := sql.Open("sqlite3", fmt.Sprintf("%s?vfs=zstd", zstPath))
if err != nil {
b.Fatalf("Failed to open database: %v", err)
}
defer client.Close()
client.SetMaxOpenConns(max(4, runtime.NumCPU()))
b.ResetTimer() // Start timing now.
b.RunParallel(func(pb *testing.PB) {
var count int
for pb.Next() {
err = client.QueryRow("SELECT COUNT(*) FROM entries_porter WHERE entries_porter MATCH 'alligator OR work'").Scan(&count)
if err != nil {
b.Fatalf("Query failed: %v", err)
}
}
})
}
func BenchmarkReadCompressedHTTPSQLite(b *testing.B) {
_, zstPath := setupDB(b)
zstDir := filepath.Dir(zstPath)
server := httptest.NewServer(http.FileServer(http.Dir(zstDir)))
defer server.Close()
client, err := sql.Open("sqlite3", fmt.Sprintf("%s/%s?vfs=zstd", server.URL, filepath.Base(zstPath)))
if err != nil {
b.Fatalf("Query failed: %v", err)
}
defer client.Close()
client.SetMaxOpenConns(max(4, runtime.NumCPU()))
b.ResetTimer() // Start timing now.
b.RunParallel(func(pb *testing.PB) {
var count int
for pb.Next() {
err = client.QueryRow("SELECT MAX(value) FROM entries").Scan(&count)
if err != nil {
b.Fatalf("Query failed: %v", err)
}
}
})
}
func BenchmarkReadCompressedRtreeSQLite(b *testing.B) {
_, zstPath := setupDB(b)
client, err := sql.Open("sqlite3", fmt.Sprintf("%s?vfs=zstd", zstPath))
if err != nil {
b.Fatalf("Failed to open database: %v", err)
}
defer client.Close()
client.SetMaxOpenConns(max(4, runtime.NumCPU()))
b.ResetTimer() // Start timing now.
b.RunParallel(func(pb *testing.PB) {
var count int
for pb.Next() {
err = client.QueryRow(`
SELECT
COUNT(*)
FROM demo_rtree
WHERE
minX <= -1 AND 1 <= maxX AND
minY <= -1 AND 1 <= maxY
`).Scan(&count)
if err != nil {
b.Fatalf("Query failed: %v", err)
}
}
})
}