-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslowquery_test.go
120 lines (99 loc) · 2.46 KB
/
slowquery_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
package querydigest
import (
"bytes"
"os"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestSlowQueryScanner_Next(t *testing.T) {
// test fixtures created by using https://github.com/isucon/isucon9-qualify application.
cases := []struct {
name string
fixturesPath string
expect SlowQueryInfo
}{
{
name: "header",
fixturesPath: "header",
expect: SlowQueryInfo{
RawQuery: bytes.NewBufferString("select @@version_comment limit 1;").Bytes(),
QueryTime: QueryTime{
QueryTime: 0.000126,
LockTime: 0,
RowsSent: 1,
RowsExamined: 0,
},
},
},
{
name: "insert",
fixturesPath: "insert",
expect: SlowQueryInfo{
RawQuery: bytes.NewBufferString("INSERT INTO categories (`id`,`parent_id`,`category_name`) VALUES" +
"(1,0,\"ソファー\")," +
"(2,1,\"一人掛けソファー\")," +
"(3,1,\"二人掛けソファー\")," +
"(4,1,\"コーナーソファー\");").Bytes(),
QueryTime: QueryTime{
QueryTime: 0.012964,
LockTime: 0.001197,
RowsSent: 0,
RowsExamined: 0,
},
},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
f, err := os.Open("./testdata/mysql-slow." + c.fixturesPath + ".log")
if err != nil {
t.Fatal(err)
}
defer f.Close()
scanner := NewSlowQueryScanner(f)
scanner.Next()
if scanner.Err() != nil {
t.Fatal(scanner.Err())
}
info := scanner.SlowQueryInfo()
if info == nil {
t.Fatal("info is nil")
}
if diff := cmp.Diff(*info, c.expect); diff != "" {
t.Errorf("diff: %s", diff)
}
})
}
}
func Test_parseHeader(t *testing.T) {
src := `# Query_time: 0.004370 Lock_time: 0.001289 Rows_sent: 2 Rows_examined: 2`
queryTime, lockTime, rowsSent, rowsExamined := parseHeader(src)
if queryTime != "0.004370" {
t.Errorf("expect: `%s` but `%s`", "0.004370", queryTime)
}
if lockTime != "0.001289" {
t.Errorf("expect: `%s` but `%s`", "0.001289", lockTime)
}
if rowsSent != "2" {
t.Errorf("expect: `%s` but `%s`", "2", rowsSent)
}
if rowsExamined != "2" {
t.Errorf("expect: `%s` but `%s`", "2", rowsExamined)
}
}
func BenchmarkSlowQueryScanner_SlowQueryInfo(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
f, err := os.Open("./benchdata/mysql-slow.log")
if err != nil {
b.Fatal(err)
}
sc := NewSlowQueryScanner(f)
for sc.Next() {
}
if err := sc.Err(); err != nil {
b.Fatal(err)
}
f.Close()
}
}