-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaggregator_test.go
153 lines (137 loc) · 3.64 KB
/
aggregator_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
/*
Copyright 2017 Daniel Nichter
Copyright 2014-2016 Percona LLC and/or its affiliates
*/
package slowlog_test
import (
"encoding/json"
"io/ioutil"
"os"
"path"
"testing"
"time"
"github.com/go-mysql/query"
"github.com/go-mysql/slowlog"
"github.com/go-test/deep"
)
var examples = true
func aggregateSlowLog(t *testing.T, input, output string, utcOffset time.Duration) (got slowlog.Result, expect slowlog.Result) {
bytes, err := ioutil.ReadFile(path.Join("test", "results", output))
if err != nil {
t.Fatal(err)
}
expect = slowlog.Result{}
if err := json.Unmarshal(bytes, &expect); err != nil {
t.Fatal(err)
}
file, err := os.Open(path.Join("test", "slow-logs", input))
if err != nil {
t.Fatal(err)
}
p := slowlog.NewFileParser(file)
if err != nil {
t.Fatal(err)
}
if err := p.Start(slowlog.Options{}); err != nil {
t.Fatal(err)
}
defer p.Stop()
a := slowlog.NewAggregator(examples, utcOffset, 10)
for e := range p.Events() {
f := query.Fingerprint(e.Query)
id := query.Id(f)
a.AddEvent(e, id, f)
}
got = a.Finalize()
return got, expect
}
func zeroPercentiles(r *slowlog.Result) {
for _, metrics := range r.Global.Metrics.TimeMetrics {
metrics.Med = 0
metrics.P95 = 0
}
for _, metrics := range r.Global.Metrics.NumberMetrics {
metrics.Med = 0
metrics.P95 = 0
}
for _, class := range r.Class {
for _, metrics := range class.Metrics.TimeMetrics {
metrics.Med = 0
metrics.P95 = 0
}
for _, metrics := range class.Metrics.NumberMetrics {
metrics.Med = 0
metrics.P95 = 0
}
}
}
// --------------------------------------------------------------------------
func TestSlow001(t *testing.T) {
got, expect := aggregateSlowLog(t, "slow001.log", "slow001.json", 0)
if diff := deep.Equal(got, expect); diff != nil {
dump(got)
t.Error(diff)
}
}
func TestSlow001WithTzOffset(t *testing.T) {
got, expect := aggregateSlowLog(t, "slow001.log", "slow001.json", -1*time.Hour)
// Use the same files as TestSlow001NoExamples but with a tz=-1
expect.Class["7F7D57ACDD8A346E"].Example.Ts = "2007-10-15 20:43:52"
expect.Class["3A99CC42AEDCCFCD"].Example.Ts = "2007-10-15 20:45:10"
if diff := deep.Equal(got, expect); diff != nil {
dump(got)
t.Error(diff)
}
}
func TestSlow001NoExamples(t *testing.T) {
examples = false
defer func() { examples = true }()
got, expect := aggregateSlowLog(t, "slow001.log", "slow001-no-examples.json", 0)
if diff := deep.Equal(got, expect); diff != nil {
dump(got)
t.Error(diff)
}
}
// Test p95 and median.
func TestSlow010(t *testing.T) {
got, expect := aggregateSlowLog(t, "slow010.log", "slow010.json", 0)
if diff := deep.Equal(got, expect); diff != nil {
dump(got)
t.Error(diff)
}
}
func TestSlow018(t *testing.T) {
got, expect := aggregateSlowLog(t, "slow018.log", "slow018.json", 0)
if diff := deep.Equal(got, expect); diff != nil {
dump(got)
t.Error(diff)
}
}
// Tests for PCT-1006 & PCT-1085
func TestUseDb(t *testing.T) {
// Test db is not inherited
got, expect := aggregateSlowLog(t, "slow020.log", "slow020.json", 0)
if diff := deep.Equal(got, expect); diff != nil {
dump(got)
t.Error(diff)
}
// Test "use" is not case sensitive
got, expect = aggregateSlowLog(t, "slow021.log", "slow021.json", 0)
if diff := deep.Equal(got, expect); diff != nil {
dump(got)
t.Error(diff)
}
// Test we are parsing db names in backticks
got, expect = aggregateSlowLog(t, "slow022.log", "slow022.json", 0)
if diff := deep.Equal(got, expect); diff != nil {
dump(got)
t.Error(diff)
}
}
func TestOutlierSlow025(t *testing.T) {
got, expect := aggregateSlowLog(t, "slow025.log", "slow025.json", 0)
if diff := deep.Equal(got, expect); diff != nil {
dump(got)
t.Error(diff)
}
}