-
Notifications
You must be signed in to change notification settings - Fork 17
/
bench_test.go
142 lines (121 loc) · 3.02 KB
/
bench_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
package ctlstore
import (
"context"
"database/sql"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/segmentio/ctlstore/pkg/ldb"
"github.com/segmentio/ctlstore/pkg/tests"
)
func BenchmarkLDBQueryBaseline(b *testing.B) {
b.StopTimer()
ctx := context.TODO()
tmpDir, err := ioutil.TempDir("", "")
if err != nil {
b.Fatalf("Unexpected error creating temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
ldbPath := filepath.Join(tmpDir, "tmp.db")
localDB, err := sql.Open("sqlite3", ldbPath)
if err != nil {
b.Fatalf("Unexpected error opening LDB: %v", err)
}
defer localDB.Close()
err = ldb.EnsureLdbInitialized(ctx, localDB)
if err != nil {
b.Fatalf("Unexpected error initializing LDB: %v", err)
}
_, err = localDB.ExecContext(ctx, `
CREATE TABLE foo___bar (
key VARCHAR PRIMARY KEY,
val VARCHAR
);
INSERT INTO foo___bar VALUES('foo', 'bar');
`)
if err != nil {
b.Fatalf("Unexpected error inserting value into LDB: %v", err)
}
prepQ, err := localDB.PrepareContext(ctx, "SELECT * FROM foo___bar WHERE key = ?")
if err != nil {
b.Fatalf("Unexpected error preparing query: %v", err)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
rows, err := prepQ.QueryContext(ctx, "foo")
if err != nil {
b.Errorf("Error querying: %v", err)
continue
}
for rows.Next() {
var keyReceiver, valReceiver string
err = rows.Scan(&keyReceiver, &valReceiver)
if err != nil {
b.Errorf("Error scanning: %v", err)
continue
}
if keyReceiver != "foo" {
b.Errorf("Received unexpected key: %v", keyReceiver)
}
if valReceiver != "bar" {
b.Errorf("Received unexpected val: %v", valReceiver)
}
}
}
}
func BenchmarkGetRowByKey(b *testing.B) {
ctx := context.TODO()
type benchContext struct {
ldb *sql.DB
ctx context.Context
r *LDBReader
}
type benchKVRow struct {
Key string `ctlstore:"key"`
Value string `ctlstore:"val"`
}
testTmpDir, teardown := tests.WithTmpDir(b)
defer teardown()
ldbPath := filepath.Join(testTmpDir, "tmp.db")
localDB, err := sql.Open("sqlite3", ldbPath)
if err != nil {
b.Fatalf("Unexpected error opening LDB: %v", err)
}
err = ldb.EnsureLdbInitialized(ctx, localDB)
if err != nil {
b.Fatalf("Unexpected error initializing LDB: %v", err)
}
_, err = localDB.ExecContext(ctx, `
CREATE TABLE foo___bar (
key VARCHAR PRIMARY KEY,
val VARCHAR
);
INSERT INTO foo___bar VALUES('foo', 'bar');
`)
if err != nil {
b.Fatalf("Unexpected error inserting value into LDB: %v", err)
}
r := NewLDBReaderFromDB(localDB)
benchSetup := &benchContext{
ldb: localDB,
ctx: ctx,
r: r,
}
for i := 0; i < b.N; i++ {
var row benchKVRow
found, err := benchSetup.r.GetRowByKey(benchSetup.ctx, &row, "foo", "bar", "foo")
if err != nil {
b.Fatalf("Unexpected error calling GetRowByKey: %v", err)
}
if !found {
b.Fatal("Should have found a row")
}
if row.Key != "foo" {
b.Fatalf("Unexpected value in row key: %v", row.Key)
}
if row.Value != "bar" {
b.Fatalf("Unexpected value in row val: %v", row.Value)
}
}
}