forked from patrickxb/fgosqlite
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbench_test.go
169 lines (141 loc) · 3.96 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
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
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sqlite_test
import (
"strings"
"testing"
. "github.com/gwenn/gosqlite"
)
func panicOnError(b *testing.B, err error) {
if err != nil {
panic(err)
}
}
func fill(b *testing.B, db *Conn, n int) {
panicOnError(b, db.Exec("DROP TABLE IF EXISTS test"))
panicOnError(b, db.Exec("CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL, float_num REAL, int_num INTEGER, a_string TEXT)"))
s, err := db.Prepare("INSERT INTO test (float_num, int_num, a_string) VALUES (?, ?, ?)")
panicOnError(b, err)
panicOnError(b, db.Begin())
for i := 0; i < n; i++ {
panicOnError(b, s.Exec(float64(i)*3.14, i, "hello"))
}
panicOnError(b, s.Finalize())
panicOnError(b, db.Commit())
}
func BenchmarkValuesScan(b *testing.B) {
b.StopTimer()
db, err := Open(":memory:")
panicOnError(b, err)
defer db.Close()
fill(b, db, 1)
cs, err := db.Prepare("SELECT float_num, int_num, a_string FROM test")
panicOnError(b, err)
defer cs.Finalize()
b.StartTimer()
for i := 0; i < b.N; i++ {
values := make([]interface{}, 3)
if Must(cs.Next()) {
cs.ScanValues(values)
}
/*panicOnError(b, */ cs.Reset() /*)*/
}
}
func BenchmarkScan(b *testing.B) {
b.StopTimer()
db, err := Open(":memory:")
panicOnError(b, err)
defer db.Close()
fill(b, db, 1)
cs, err := db.Prepare("SELECT float_num, int_num, a_string FROM test")
panicOnError(b, err)
defer cs.Finalize()
b.StartTimer()
for i := 0; i < b.N; i++ {
var fnum float64
var inum int64
var sstr string
if Must(cs.Next()) {
/*panicOnError(b, */ cs.Scan(&fnum, &inum, &sstr) /*)*/
}
/*panicOnError(b, */ cs.Reset() /*)*/
}
}
func BenchmarkNamedScan(b *testing.B) {
b.StopTimer()
db, err := Open(":memory:")
panicOnError(b, err)
defer db.Close()
fill(b, db, 1)
cs, err := db.Prepare("SELECT float_num, int_num, a_string FROM test")
panicOnError(b, err)
defer cs.Finalize()
b.StartTimer()
for i := 0; i < b.N; i++ {
var fnum float64
var inum int64
var sstr string
if Must(cs.Next()) {
/*panicOnError(b, */ cs.NamedScan("float_num", &fnum, "int_num", &inum, "a_string", &sstr) /*)*/
}
/*panicOnError(b, */ cs.Reset() /*)*/
}
}
func BenchmarkInsert(b *testing.B) {
b.StopTimer()
db, err := Open(":memory:")
panicOnError(b, err)
defer db.Close()
panicOnError(b, db.Exec("DROP TABLE IF EXISTS test"))
panicOnError(b, db.Exec("CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL, float_num REAL, int_num INTEGER, a_string TEXT)"))
s, err := db.Prepare("INSERT INTO test (float_num, int_num, a_string) VALUES (?, ?, ?)")
panicOnError(b, err)
defer s.Finalize()
b.StartTimer()
panicOnError(b, db.Begin())
for i := 0; i < b.N; i++ {
/*panicOnError(b, */ s.Exec(float64(i)*3.14, i, "hello") /*)*/
}
panicOnError(b, db.Commit())
}
func BenchmarkNamedInsert(b *testing.B) {
b.StopTimer()
db, err := Open(":memory:")
panicOnError(b, err)
defer db.Close()
panicOnError(b, db.Exec("DROP TABLE IF EXISTS test"))
panicOnError(b, db.Exec("CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL,"+
" float_num REAL, int_num INTEGER, a_string TEXT)"))
s, err := db.Prepare("INSERT INTO test (float_num, int_num, a_string)" +
" VALUES (:f, :i, :s)")
panicOnError(b, err)
defer s.Finalize()
b.StartTimer()
panicOnError(b, db.Begin())
for i := 0; i < b.N; i++ {
/*panicOnError(b, */ s.NamedBind(":f", float64(i)*3.14, ":i", i, ":s", "hello") /*)*/
Must(s.Next())
}
panicOnError(b, db.Commit())
}
func BenchmarkExec(b *testing.B) {
b.StopTimer()
db, err := Open(":memory:")
panicOnError(b, err)
defer db.Close()
b.StartTimer()
for i := 0; i < b.N; i++ {
panicOnError(b, db.Exec(strings.Repeat("BEGIN;ROLLBACK;", 5)))
}
}
func BenchmarkFastExec(b *testing.B) {
b.StopTimer()
db, err := Open(":memory:")
panicOnError(b, err)
defer db.Close()
b.StartTimer()
for i := 0; i < b.N; i++ {
panicOnError(b, db.FastExec(strings.Repeat("BEGIN;ROLLBACK;", 5)))
}
}