Debian Codegraph @

event/aggregator_test.go

raw symbols SCIP
1 /*
2 Copyright (c) 2019, Percona LLC.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10
11 * Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14
15 * Neither the name of the copyright holder nor the names of its
16 contributors may be used to endorse or promote products derived from
17 this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 package event_test
32
33 import (
34 "encoding/json"
35 "io/ioutil"
36 l "log"
37 "os"
38 "path/filepath"
39 "sort"
40 "strings"
41 "testing"
42 "time"
43
44 "github.com/stretchr/testify/assert"
45
46 "github.com/percona/go-mysql/event"
47 "github.com/percona/go-mysql/log"
48 parser "github.com/percona/go-mysql/log/slow"
49 "github.com/percona/go-mysql/query"
50 "github.com/percona/go-mysql/test"
51 )
52
53 var (
54 rootDir = test.RootDir()
55 sample = filepath.Join(rootDir, "test/slow-logs")
56 )
57
58 func aggregateSlowLog(input, output string, utcOffset time.Duration, examples bool) (string, string) {
59 file, err := os.Open(filepath.Join(sample, input))
60 if err != nil {
61 l.Fatal(err)
62 }
63 opt := log.Options{}
64 opt.DefaultLocation = time.UTC
65 p := parser.NewSlowLogParser(file, opt)
66 go p.Start()
67 a := event.NewAggregator(examples, utcOffset, 10)
68 for e := range p.EventChan() {
69 f := query.Fingerprint(e.Query)
70 id := query.Id(f)
71 a.AddEvent(e, id, e.User, e.Host, e.Db, e.Server, f)
72 }
73 got := a.Finalize()
74 gotJson, err := json.MarshalIndent(got, "", " ")
75 if err != nil {
76 l.Fatal(err)
77 }
78
79 resultOutputPath := filepath.Join("testdata", output)
80 if *test.Update {
81 if err := ioutil.WriteFile(resultOutputPath, gotJson, 0o666); err != nil {
82 l.Fatal(err)
83 }
84 }
85 expectJson, err := ioutil.ReadFile(resultOutputPath)
86 if err != nil {
87 l.Fatal(err)
88 }
89
90 return string(gotJson), string(expectJson)
91 }
92
93 func zeroPercentiles(r *event.Result) {
94 for _, metrics := range r.Global.Metrics.TimeMetrics {
95 metrics.P99 = event.Float64(0)
96 }
97 for _, metrics := range r.Global.Metrics.NumberMetrics {
98 metrics.P99 = event.Uint64(0)
99 }
100 for _, class := range r.Class {
101 for _, metrics := range class.Metrics.TimeMetrics {
102 metrics.P99 = event.Float64(0)
103 }
104 for _, metrics := range class.Metrics.NumberMetrics {
105 metrics.P99 = event.Uint64(0)
106 }
107 }
108 }
109
110 func ordered(in map[string]*event.Class) (out []*event.Class) {
111 var keys []string
112 for k := range in {
113 keys = append(keys, k)
114 }
115 sort.Strings(keys)
116 for _, k := range keys {
117 out = append(out, in[k])
118 }
119
120 return out
121 }
122
123 // --------------------------------------------------------------------------
124
125 func TestSlow001(t *testing.T) {
126 got, expect := aggregateSlowLog("slow001.log", "slow001.golden", 0, true)
127 assert.JSONEq(t, expect, got)
128 }
129
130 func TestSlow001WithTzOffset(t *testing.T) {
131 got, expect := aggregateSlowLog("slow001.log", "slow001.golden", -1*time.Hour, true)
132 // Use the same files as TestSlow001NoExamples but with a tz=-1
133 expect = strings.Replace(expect, "2007-10-15 21:43:52", "2007-10-15 20:43:52", 1)
134 expect = strings.Replace(expect, "2007-10-15 21:45:10", "2007-10-15 20:45:10", 1)
135 assert.JSONEq(t, expect, got)
136 }
137
138 func TestSlow001NoExamples(t *testing.T) {
139 got, expect := aggregateSlowLog("slow001.log", "slow001-no-examples.golden", 0, false)
140 assert.JSONEq(t, expect, got)
141 }
142
143 // Test p99 and Cnt.
144 func TestSlow010(t *testing.T) {
145 got, expect := aggregateSlowLog("slow010.log", "slow010.golden", 0, true)
146 assert.JSONEq(t, expect, got)
147 }
148
149 func TestAddClassSlow001(t *testing.T) {
150 expect, _ := aggregateSlowLog("slow001.log", "slow001.golden", 0, true)
151 expectEventResult := event.Result{}
152 if err := json.Unmarshal([]byte(expect), &expectEventResult); err != nil {
153 t.Fatal(err)
154 }
155 zeroPercentiles(&expectEventResult)
156
157 global := event.NewClass("", "", "", "", "", "", false)
158 for _, class := range expectEventResult.Class {
159 global.AddClass(class)
160 }
161 expectGlobalBytes, err := json.Marshal(expectEventResult.Global)
162 if err != nil {
163 t.Fatal(err)
164 }
165 gotGlobalBytes, err := json.Marshal(global)
166 if err != nil {
167 t.Fatal(err)
168 }
169 assert.JSONEq(t, string(expectGlobalBytes), string(gotGlobalBytes))
170 }
171
172 func TestAddClassSlow023(t *testing.T) {
173 expect, _ := aggregateSlowLog("slow023.log", "slow023.golden", 0, true)
174 expectEventResult := event.Result{}
175 if err := json.Unmarshal([]byte(expect), &expectEventResult); err != nil {
176 t.Fatal(err)
177 }
178 zeroPercentiles(&expectEventResult)
179
180 global := event.NewClass("", "", "", "", "", "", false)
181 for _, class := range ordered(expectEventResult.Class) {
182 global.AddClass(class)
183 }
184 expectGlobalBytes, err := json.Marshal(expectEventResult.Global)
185 if err != nil {
186 t.Fatal(err)
187 }
188 gotGlobalBytes, err := json.Marshal(global)
189 if err != nil {
190 t.Fatal(err)
191 }
192 assert.JSONEq(t, string(expectGlobalBytes), string(gotGlobalBytes))
193 }
194
195 func TestSlow018(t *testing.T) {
196 got, expect := aggregateSlowLog("slow018.log", "slow018.golden", 0, true)
197 assert.JSONEq(t, expect, got)
198 }
199
200 // Tests for PCT-1006 & PCT-1085
201 func TestUseDb(t *testing.T) {
202 // Test db is not inherited
203 got, expect := aggregateSlowLog("slow020.log", "slow020.golden", 0, true)
204 assert.JSONEq(t, expect, got)
205 // Test "use" is not case sensitive
206 got, expect = aggregateSlowLog("slow021.log", "slow021.golden", 0, true)
207 assert.JSONEq(t, expect, got)
208 // Test we are parsing db names in backticks
209 got, expect = aggregateSlowLog("slow022.log", "slow022.golden", 0, true)
210 assert.JSONEq(t, expect, got)
211 }
212
213 func TestOutlierSlow025(t *testing.T) {
214 got, expect := aggregateSlowLog("slow025.log", "slow025.golden", 0, true)
215 assert.JSONEq(t, expect, got)
216 }
217
218 func TestMariaDB102WithExplain(t *testing.T) {
219 got, expect := aggregateSlowLog("mariadb102-with-explain.log", "mariadb102-with-explain.golden", 0, true)
220 assert.JSONEq(t, expect, got)
221 }
222
223 func TestSlow026(t *testing.T) {
224 got, expect := aggregateSlowLog("slow027.log", "slow027.golden", 0, true)
225 assert.JSONEq(t, expect, got)
226 }