forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_test.go
194 lines (177 loc) · 4.28 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
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
package tidb
import (
"fmt"
"testing"
"github.com/ngaut/log"
"github.com/pingcap/tidb/ast"
)
var smallCount = 100
func prepareBenchSession() Session {
store, err := NewStore("memory://bench")
if err != nil {
log.Fatal(err)
}
log.SetLevel(log.LOG_LEVEL_ERROR)
se, err := CreateSession(store)
if err != nil {
log.Fatal(err)
}
mustExecute(se, "use test")
return se
}
func prepareBenchData(se Session, colType string, valueFormat string, valueCount int) {
mustExecute(se, "drop table if exists t")
mustExecute(se, fmt.Sprintf("create table t (pk int primary key auto_increment, col %s, index idx (col))", colType))
mustExecute(se, "begin")
for i := 0; i < valueCount; i++ {
mustExecute(se, "insert t (col) values ("+fmt.Sprintf(valueFormat, i)+")")
}
mustExecute(se, "commit")
}
func readResult(rs ast.RecordSet, count int) {
for count > 0 {
x, err := rs.Next()
if err != nil {
log.Fatal(err)
}
if x == nil {
log.Fatal(count)
}
count--
}
rs.Close()
}
func BenchmarkBasic(b *testing.B) {
se := prepareBenchSession()
for i := 0; i < b.N; i++ {
rs, err := se.Execute("select 1")
if err != nil {
b.Fatal(err)
}
readResult(rs[0], 1)
}
}
func BenchmarkTableScan(b *testing.B) {
b.StopTimer()
se := prepareBenchSession()
prepareBenchData(se, "int", "%v", smallCount)
b.StartTimer()
for i := 0; i < b.N; i++ {
rs, err := se.Execute("select * from t")
if err != nil {
b.Fatal(err)
}
readResult(rs[0], smallCount)
}
}
func BenchmarkTableLookup(b *testing.B) {
b.StopTimer()
se := prepareBenchSession()
prepareBenchData(se, "int", "%d", smallCount)
b.StartTimer()
for i := 0; i < b.N; i++ {
rs, err := se.Execute("select * from t where pk = 64")
if err != nil {
b.Fatal(err)
}
readResult(rs[0], 1)
}
}
func BenchmarkStringIndexScan(b *testing.B) {
b.StopTimer()
se := prepareBenchSession()
prepareBenchData(se, "varchar(255)", "'hello %d'", smallCount)
b.StartTimer()
for i := 0; i < b.N; i++ {
rs, err := se.Execute("select * from t where col > 'hello'")
if err != nil {
b.Fatal(err)
}
readResult(rs[0], smallCount)
}
}
func BenchmarkStringIndexLookup(b *testing.B) {
b.StopTimer()
se := prepareBenchSession()
prepareBenchData(se, "varchar(255)", "'hello %d'", smallCount)
b.StartTimer()
for i := 0; i < b.N; i++ {
rs, err := se.Execute("select * from t where col = 'hello 64'")
if err != nil {
b.Fatal(err)
}
readResult(rs[0], 1)
}
}
func BenchmarkIntegerIndexScan(b *testing.B) {
b.StopTimer()
se := prepareBenchSession()
prepareBenchData(se, "int", "%v", smallCount)
b.StartTimer()
for i := 0; i < b.N; i++ {
rs, err := se.Execute("select * from t where col >= 0")
if err != nil {
b.Fatal(err)
}
readResult(rs[0], smallCount)
}
}
func BenchmarkIntegerIndexLookup(b *testing.B) {
b.StopTimer()
se := prepareBenchSession()
prepareBenchData(se, "int", "%v", smallCount)
b.StartTimer()
for i := 0; i < b.N; i++ {
rs, err := se.Execute("select * from t where col = 64")
if err != nil {
b.Fatal(err)
}
readResult(rs[0], 1)
}
}
func BenchmarkDecimalIndexScan(b *testing.B) {
b.StopTimer()
se := prepareBenchSession()
prepareBenchData(se, "decimal(32,6)", "%v.1234", smallCount)
b.StartTimer()
for i := 0; i < b.N; i++ {
rs, err := se.Execute("select * from t where col >= 0")
if err != nil {
b.Fatal(err)
}
readResult(rs[0], smallCount)
}
}
func BenchmarkDecimalIndexLookup(b *testing.B) {
b.StopTimer()
se := prepareBenchSession()
prepareBenchData(se, "decimal(32,6)", "%v.1234", smallCount)
b.StartTimer()
for i := 0; i < b.N; i++ {
rs, err := se.Execute("select * from t where col = 64.1234")
if err != nil {
b.Fatal(err)
}
readResult(rs[0], 1)
}
}
func BenchmarkInsertWithIndex(b *testing.B) {
b.StopTimer()
se := prepareBenchSession()
mustExecute(se, "drop table if exists t")
mustExecute(se, "create table t (pk int primary key, col int, index idx (col))")
b.StartTimer()
for i := 0; i < b.N; i++ {
mustExecute(se, fmt.Sprintf("insert t values (%d, %d)", i, i))
}
}
func BenchmarkInsertNoIndex(b *testing.B) {
b.StopTimer()
se := prepareBenchSession()
mustExecute(se, "drop table if exists t")
mustExecute(se, "create table t (pk int primary key, col int)")
b.StartTimer()
for i := 0; i < b.N; i++ {
mustExecute(se, fmt.Sprintf("insert t values (%d, %d)", i, i))
}
}