Debian Codegraph @

log/slow/parser_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 slow_test
32
33 import (
34 l "log"
35 "os"
36 "path"
37 "testing"
38 "time"
39
40 "github.com/stretchr/testify/assert"
41 "github.com/stretchr/testify/require"
42
43 "github.com/percona/go-mysql/log"
44 parser "github.com/percona/go-mysql/log/slow"
45 "github.com/percona/go-mysql/test"
46 )
47
48 var (
49 sample = test.RootDir() + "/test/slow-logs"
50 opt = log.Options{
51 Debug: false,
52 DefaultLocation: time.UTC,
53 }
54 )
55
56 func parseSlowLog(t *testing.T, filename string, o log.Options) []log.Event {
57 file, err := os.Open(path.Join(sample, "/", filename))
58 require.NoError(t, err)
59 p := parser.NewSlowLogParser(file, o)
60 require.NoError(t, err)
61 got := []log.Event{}
62 go p.Start()
63 for e := range p.EventChan() {
64 got = append(got, *e)
65 }
66 return got
67 }
68
69 // --------------------------------------------------------------------------
70
71 // No input, no events.
72 func TestParserEmptySlowLog(t *testing.T) {
73 got := parseSlowLog(t, "empty.log", opt)
74 expect := []log.Event{}
75 assert.EqualValues(t, expect, got)
76 }
77
78 // slow001 is a most basic basic, normal slow log--nothing exotic.
79 func TestParserSlowLog001(t *testing.T) {
80 got := parseSlowLog(t, "slow001.log", opt)
81 expect := []log.Event{
82 {
83 Ts: time.Date(2007, 10, 15, 21, 43, 52, 0, time.UTC),
84 Admin: false,
85 Query: `select sleep(2) from n`,
86 User: "root",
87 Host: "localhost",
88 Db: "test",
89 Offset: 199,
90 OffsetEnd: 358,
91 TimeMetrics: map[string]float64{
92 "Query_time": 2,
93 "Lock_time": 0,
94 },
95 NumberMetrics: map[string]uint64{
96 "Rows_sent": 1,
97 "Rows_examined": 0,
98 },
99 BoolMetrics: map[string]bool{},
100 },
101 {
102 Ts: time.Date(2007, 10, 15, 21, 45, 10, 0, time.UTC),
103 Admin: false,
104 Query: `select sleep(2) from test.n`,
105 User: "root",
106 Host: "localhost",
107 Db: "sakila",
108 Offset: 358,
109 OffsetEnd: 524,
110 TimeMetrics: map[string]float64{
111 "Query_time": 2,
112 "Lock_time": 0,
113 },
114 NumberMetrics: map[string]uint64{
115 "Rows_sent": 1,
116 "Rows_examined": 0,
117 },
118 BoolMetrics: map[string]bool{},
119 },
120 }
121 assert.EqualValues(t, expect, got)
122 }
123
124 // slow002 is a basic slow log like slow001 but with more metrics, multi-line queries, etc.
125 func TestParseSlowLog002(t *testing.T) {
126 got := parseSlowLog(t, "slow002.log", opt)
127 expect := []log.Event{
128 {
129 Query: "BEGIN",
130 Ts: time.Date(2007, 12, 18, 11, 48, 27, 0, time.UTC),
131 Admin: false,
132 User: "[SQL_SLAVE]",
133 Host: "",
134 Offset: 0,
135 OffsetEnd: 337,
136 TimeMetrics: map[string]float64{
137 "Query_time": 0.000012,
138 "Lock_time": 0.000000,
139 },
140 NumberMetrics: map[string]uint64{
141 "Merge_passes": 0,
142 "Thread_id": 10,
143 "Rows_examined": 0,
144 "Rows_sent": 0,
145 },
146 BoolMetrics: map[string]bool{
147 "Filesort": false,
148 "Full_scan": false,
149 "Filesort_on_disk": false,
150 "Full_join": false,
151 "Tmp_table_on_disk": false,
152 "Tmp_table": false,
153 "QC_Hit": false,
154 },
155 },
156 {
157 Db: "db1",
158 Query: `update db2.tuningdetail_21_265507 n
159 inner join db1.gonzo a using(gonzo)
160 set n.column1 = a.column1, n.word3 = a.word3`,
161 Admin: false,
162 User: "[SQL_SLAVE]",
163 Host: "",
164 Offset: 337,
165 OffsetEnd: 813,
166 TimeMetrics: map[string]float64{
167 "Query_time": 0.726052,
168 "Lock_time": 0.000091,
169 },
170 NumberMetrics: map[string]uint64{
171 "Merge_passes": 0,
172 "Thread_id": 10,
173 "Rows_examined": 62951,
174 "Rows_sent": 0,
175 },
176 BoolMetrics: map[string]bool{
177 "Filesort": false,
178 "Full_scan": true,
179 "Filesort_on_disk": false,
180 "Full_join": false,
181 "Tmp_table_on_disk": false,
182 "Tmp_table": false,
183 "QC_Hit": false,
184 },
185 },
186 {
187 Query: `INSERT INTO db3.vendor11gonzo (makef, bizzle)
188 VALUES ('', 'Exact')`,
189 Admin: false,
190 User: "[SQL_SLAVE]",
191 Host: "",
192 Offset: 813,
193 OffsetEnd: 1332,
194 TimeMetrics: map[string]float64{
195 "InnoDB_queue_wait": 0.000000,
196 "Lock_time": 0.000077,
197 "InnoDB_rec_lock_wait": 0.000000,
198 "Query_time": 0.000512,
199 "InnoDB_IO_r_wait": 0.000000,
200 },
201 NumberMetrics: map[string]uint64{
202 "InnoDB_IO_r_bytes": 0,
203 "Merge_passes": 0,
204 "InnoDB_pages_distinct": 24,
205 "Rows_sent": 0,
206 "Thread_id": 10,
207 "Rows_examined": 0,
208 "InnoDB_IO_r_ops": 0,
209 },
210 BoolMetrics: map[string]bool{
211 "Filesort": false,
212 "Full_scan": false,
213 "Filesort_on_disk": false,
214 "Full_join": false,
215 "Tmp_table_on_disk": false,
216 "Tmp_table": false,
217 "QC_Hit": false,
218 },
219 },
220 {
221 Query: `UPDATE db4.vab3concept1upload
222 SET vab3concept1id = '91848182522'
223 WHERE vab3concept1upload='6994465'`,
224 Admin: false,
225 User: "[SQL_SLAVE]",
226 Host: "",
227 Offset: 1332,
228 OffsetEnd: 1862,
229 TimeMetrics: map[string]float64{
230 "Query_time": 0.033384,
231 "InnoDB_IO_r_wait": 0.000000,
232 "InnoDB_queue_wait": 0.000000,
233 "Lock_time": 0.000028,
234 "InnoDB_rec_lock_wait": 0.000000,
235 },
236 NumberMetrics: map[string]uint64{
237 "InnoDB_IO_r_bytes": 0,
238 "Merge_passes": 0,
239 "InnoDB_pages_distinct": 11,
240 "Rows_sent": 0,
241 "Thread_id": 10,
242 "Rows_examined": 0,
243 "InnoDB_IO_r_ops": 0,
244 },
245 BoolMetrics: map[string]bool{
246 "Filesort": false,
247 "Full_scan": false,
248 "Filesort_on_disk": false,
249 "Full_join": false,
250 "Tmp_table_on_disk": false,
251 "Tmp_table": false,
252 "QC_Hit": false,
253 },
254 },
255 {
256 Query: `INSERT INTO db1.conch (word3, vid83)
257 VALUES ('211', '18')`,
258 Admin: false,
259 User: "[SQL_SLAVE]",
260 Host: "",
261 Offset: 1862,
262 OffsetEnd: 2391,
263 TimeMetrics: map[string]float64{
264 "InnoDB_queue_wait": 0.000000,
265 "Query_time": 0.000530,
266 "InnoDB_IO_r_wait": 0.000000,
267 "Lock_time": 0.000027,
268 "InnoDB_rec_lock_wait": 0.000000,
269 },
270 NumberMetrics: map[string]uint64{
271 "InnoDB_IO_r_bytes": 0,
272 "Merge_passes": 0,
273 "InnoDB_pages_distinct": 18,
274 "Rows_sent": 0,
275 "Thread_id": 10,
276 "Rows_examined": 0,
277 "InnoDB_IO_r_ops": 0,
278 },
279 BoolMetrics: map[string]bool{
280 "Filesort": false,
281 "Full_scan": false,
282 "Filesort_on_disk": false,
283 "Full_join": false,
284 "Tmp_table_on_disk": false,
285 "Tmp_table": false,
286 "QC_Hit": false,
287 },
288 },
289 {
290 Query: `UPDATE foo.bar
291 SET biz = '91848182522'`,
292 Admin: false,
293 User: "[SQL_SLAVE]",
294 Host: "",
295 Offset: 2391,
296 OffsetEnd: 2859,
297 TimeMetrics: map[string]float64{
298 "Lock_time": 0.000027,
299 "InnoDB_rec_lock_wait": 0.000000,
300 "InnoDB_queue_wait": 0.000000,
301 "Query_time": 0.000530,
302 "InnoDB_IO_r_wait": 0.000000,
303 },
304 NumberMetrics: map[string]uint64{
305 "InnoDB_IO_r_bytes": 0,
306 "Merge_passes": 0,
307 "InnoDB_pages_distinct": 18,
308 "Rows_sent": 0,
309 "Thread_id": 10,
310 "Rows_examined": 0,
311 "InnoDB_IO_r_ops": 0,
312 },
313 BoolMetrics: map[string]bool{
314 "Filesort": false,
315 "Full_scan": false,
316 "Filesort_on_disk": false,
317 "Full_join": false,
318 "Tmp_table_on_disk": false,
319 "Tmp_table": false,
320 "QC_Hit": false,
321 },
322 },
323 {
324 Query: `UPDATE bizzle.bat
325 SET boop='bop: 899'
326 WHERE fillze='899'`,
327 Admin: false,
328 User: "[SQL_SLAVE]",
329 Host: "",
330 Offset: 2859,
331 OffsetEnd: 3372,
332 TimeMetrics: map[string]float64{
333 "Query_time": 0.000530,
334 "InnoDB_IO_r_wait": 0.000000,
335 "InnoDB_queue_wait": 0.000000,
336 "Lock_time": 0.000027,
337 "InnoDB_rec_lock_wait": 0.000000,
338 },
339 NumberMetrics: map[string]uint64{
340 "InnoDB_IO_r_bytes": 0,
341 "Merge_passes": 0,
342 "InnoDB_pages_distinct": 18,
343 "Rows_sent": 0,
344 "Thread_id": 10,
345 "Rows_examined": 0,
346 "InnoDB_IO_r_ops": 0,
347 },
348 BoolMetrics: map[string]bool{
349 "Filesort": false,
350 "Full_scan": false,
351 "Filesort_on_disk": false,
352 "Full_join": false,
353 "Tmp_table_on_disk": false,
354 "Tmp_table": false,
355 "QC_Hit": false,
356 },
357 },
358 {
359 Query: `UPDATE foo.bar
360 SET biz = '91848182522'`,
361 Admin: false,
362 User: "[SQL_SLAVE]",
363 Host: "",
364 Offset: 3372,
365 OffsetEnd: 3840,
366 TimeMetrics: map[string]float64{
367 "Query_time": 0.000530,
368 "Lock_time": 0.000027,
369 "InnoDB_rec_lock_wait": 0.000000,
370 "InnoDB_queue_wait": 0.000000,
371 "InnoDB_IO_r_wait": 0.000000,
372 },
373 NumberMetrics: map[string]uint64{
374 "InnoDB_IO_r_bytes": 0,
375 "Merge_passes": 0,
376 "InnoDB_pages_distinct": 18,
377 "Rows_sent": 0,
378 "Thread_id": 10,
379 "Rows_examined": 0,
380 "InnoDB_IO_r_ops": 0,
381 },
382 BoolMetrics: map[string]bool{
383 "Filesort": false,
384 "Full_scan": false,
385 "Filesort_on_disk": false,
386 "Full_join": false,
387 "Tmp_table_on_disk": false,
388 "Tmp_table": false,
389 "QC_Hit": false,
390 },
391 },
392 }
393 assert.EqualValues(t, expect, got)
394 }
395
396 // slow003 starts with a blank line. I guess this once messed up SlowLogParser.pm?
397 func TestParserSlowLog003(t *testing.T) {
398 got := parseSlowLog(t, "slow003.log", opt)
399 expect := []log.Event{
400 {
401 Query: "BEGIN",
402 Admin: false,
403 Host: "",
404 Ts: time.Date(2007, 12, 18, 11, 48, 27, 0, time.UTC),
405 User: "[SQL_SLAVE]",
406 Offset: 1,
407 OffsetEnd: 338,
408 BoolMetrics: map[string]bool{
409 "Filesort_on_disk": false,
410 "Tmp_table_on_disk": false,
411 "Filesort": false,
412 "Full_join": false,
413 "Full_scan": false,
414 "QC_Hit": false,
415 "Tmp_table": false,
416 },
417 TimeMetrics: map[string]float64{
418 "Lock_time": 0.000000,
419 "Query_time": 0.000012,
420 },
421 NumberMetrics: map[string]uint64{
422 "Merge_passes": 0,
423 "Rows_examined": 0,
424 "Rows_sent": 0,
425 "Thread_id": 10,
426 },
427 },
428 }
429 assert.EqualValues(t, expect, got)
430 }
431
432 // I don't know what's special about this slow004.
433 func TestParserSlowLog004(t *testing.T) {
434 got := parseSlowLog(t, "slow004.log", opt)
435 expect := []log.Event{
436 {
437 Query: "select 12_13_foo from (select 12foo from 123_bar) as 123baz",
438 Admin: false,
439 Host: "localhost",
440 Ts: time.Date(2007, 10, 15, 21, 43, 52, 0, time.UTC),
441 User: "root",
442 Offset: 199,
443 OffsetEnd: 385,
444 BoolMetrics: map[string]bool{},
445 TimeMetrics: map[string]float64{
446 "Lock_time": 0.000000,
447 "Query_time": 2.000000,
448 },
449 NumberMetrics: map[string]uint64{
450 "Rows_sent": 1,
451 "Rows_examined": 0,
452 },
453 },
454 }
455 assert.EqualValues(t, expect, got)
456 }
457
458 // slow005 has a multi-line query with tabs in it. A pathological case that
459 // would probably break the parser is a query like:
460 //
461 // SELECT * FROM foo WHERE col = "Hello
462 // # Query_time: 10
463 // " LIMIT 1;
464 //
465 // There's no easy way to detect that "# Query_time" is part of the query and
466 // not part of the next event's header.
467 func TestParserSlowLog005(t *testing.T) {
468 got := parseSlowLog(t, "slow005.log", opt)
469 expect := []log.Event{
470 {
471 Query: "foo\nbar\n\t\t\t0 AS counter\nbaz",
472 Admin: false,
473 Host: "",
474 Ts: time.Date(2007, 12, 18, 11, 48, 27, 0, time.UTC),
475 User: "[SQL_SLAVE]",
476 Offset: 0,
477 OffsetEnd: 359,
478 BoolMetrics: map[string]bool{
479 "Filesort_on_disk": false,
480 "Tmp_table_on_disk": false,
481 "Filesort": false,
482 "Full_join": false,
483 "Full_scan": false,
484 "QC_Hit": false,
485 "Tmp_table": false,
486 },
487 TimeMetrics: map[string]float64{
488 "Query_time": 0.000012,
489 "Lock_time": 0.000000,
490 },
491 NumberMetrics: map[string]uint64{
492 "Merge_passes": 0,
493 "Rows_examined": 0,
494 "Rows_sent": 0,
495 "Thread_id": 10,
496 },
497 },
498 }
499 assert.EqualValues(t, expect, got)
500 }
501
502 // slow006 has the Schema: db metric _or_ use db; lines before the queries.
503 // Schema value should be used for log.Event.Db is no use db; line is present.
504 func TestParserSlowLog006(t *testing.T) {
505 got := parseSlowLog(t, "slow006.log", opt)
506 expect := []log.Event{
507 {
508 Query: "SELECT col FROM foo_tbl",
509 Db: "foo",
510 Admin: false,
511 Host: "",
512 Ts: time.Date(2007, 12, 18, 11, 48, 27, 0, time.UTC),
513 User: "[SQL_SLAVE]",
514 Offset: 0,
515 OffsetEnd: 368,
516 BoolMetrics: map[string]bool{
517 "Filesort_on_disk": false,
518 "Tmp_table_on_disk": false,
519 "Filesort": false,
520 "Full_join": false,
521 "Full_scan": false,
522 "QC_Hit": false,
523 "Tmp_table": false,
524 },
525 TimeMetrics: map[string]float64{
526 "Query_time": 0.000012,
527 "Lock_time": 0.000000,
528 },
529 NumberMetrics: map[string]uint64{
530 "Merge_passes": 0,
531 "Rows_examined": 0,
532 "Rows_sent": 0,
533 "Thread_id": 10,
534 },
535 },
536 {
537 Query: "SELECT col FROM foo_tbl",
538 Db: "foo",
539 Admin: false,
540 Host: "",
541 Ts: time.Date(2007, 12, 18, 11, 48, 57, 0, time.UTC),
542 User: "[SQL_SLAVE]",
543 Offset: 368,
544 OffsetEnd: 736,
545 BoolMetrics: map[string]bool{
546 "Filesort_on_disk": false,
547 "Tmp_table_on_disk": false,
548 "Filesort": false,
549 "Full_join": false,
550 "Full_scan": false,
551 "QC_Hit": false,
552 "Tmp_table": false,
553 },
554 TimeMetrics: map[string]float64{
555 "Query_time": 0.000012,
556 "Lock_time": 0.000000,
557 },
558 NumberMetrics: map[string]uint64{
559 "Merge_passes": 0,
560 "Rows_examined": 0,
561 "Rows_sent": 0,
562 "Thread_id": 10,
563 },
564 },
565 {
566 Query: "SELECT col FROM bar_tbl",
567 Db: "bar",
568 Admin: false,
569 Host: "",
570 Ts: time.Date(2007, 12, 18, 11, 48, 57, 0, time.UTC),
571 User: "[SQL_SLAVE]",
572 Offset: 736,
573 OffsetEnd: 1100,
574 BoolMetrics: map[string]bool{
575 "Filesort_on_disk": false,
576 "Tmp_table_on_disk": false,
577 "Filesort": false,
578 "Full_join": false,
579 "Full_scan": false,
580 "QC_Hit": false,
581 "Tmp_table": false,
582 },
583 TimeMetrics: map[string]float64{
584 "Query_time": 0.000012,
585 "Lock_time": 0.000000,
586 },
587 NumberMetrics: map[string]uint64{
588 "Merge_passes": 0,
589 "Rows_examined": 0,
590 "Rows_sent": 0,
591 "Thread_id": 20,
592 },
593 },
594 {
595 Query: "SELECT col FROM bar_tbl",
596 Db: "bar",
597 Admin: false,
598 Host: "",
599 Ts: time.Date(2007, 12, 18, 11, 49, 5, 0, time.UTC),
600 User: "[SQL_SLAVE]",
601 Offset: 1100,
602 OffsetEnd: 1468,
603 BoolMetrics: map[string]bool{
604 "Filesort_on_disk": false,
605 "Tmp_table_on_disk": false,
606 "Filesort": false,
607 "Full_join": false,
608 "Full_scan": false,
609 "QC_Hit": false,
610 "Tmp_table": false,
611 },
612 TimeMetrics: map[string]float64{
613 "Query_time": 0.000012,
614 "Lock_time": 0.000000,
615 },
616 NumberMetrics: map[string]uint64{
617 "Merge_passes": 0,
618 "Rows_examined": 0,
619 "Rows_sent": 0,
620 "Thread_id": 10,
621 },
622 },
623 {
624 Query: "SELECT col FROM bar_tbl",
625 Db: "bar",
626 Admin: false,
627 Host: "",
628 Ts: time.Date(2007, 12, 18, 11, 49, 7, 0, time.UTC),
629 User: "[SQL_SLAVE]",
630 Offset: 1468,
631 OffsetEnd: 1832,
632 BoolMetrics: map[string]bool{
633 "Filesort_on_disk": false,
634 "Tmp_table_on_disk": false,
635 "Filesort": false,
636 "Full_join": false,
637 "Full_scan": false,
638 "QC_Hit": false,
639 "Tmp_table": false,
640 },
641 TimeMetrics: map[string]float64{
642 "Query_time": 0.000012,
643 "Lock_time": 0.000000,
644 },
645 NumberMetrics: map[string]uint64{
646 "Merge_passes": 0,
647 "Rows_examined": 0,
648 "Rows_sent": 0,
649 "Thread_id": 20,
650 },
651 },
652 {
653 Query: "SELECT col FROM foo_tbl",
654 Db: "foo",
655 Admin: false,
656 Host: "",
657 Ts: time.Date(2007, 12, 18, 11, 49, 30, 0, time.UTC),
658 User: "[SQL_SLAVE]",
659 Offset: 1832,
660 OffsetEnd: 2200,
661 BoolMetrics: map[string]bool{
662 "Filesort_on_disk": false,
663 "Tmp_table_on_disk": false,
664 "Filesort": false,
665 "Full_join": false,
666 "Full_scan": false,
667 "QC_Hit": false,
668 "Tmp_table": false,
669 },
670 TimeMetrics: map[string]float64{
671 "Query_time": 0.000012,
672 "Lock_time": 0.000000,
673 },
674 NumberMetrics: map[string]uint64{
675 "Merge_passes": 0,
676 "Rows_examined": 0,
677 "Rows_sent": 0,
678 "Thread_id": 30,
679 },
680 },
681 }
682 assert.EqualValues(t, expect, got)
683 }
684
685 // slow007 has Schema: db1 _and_ use db2;. db2 should be used.
686 func TestParserSlowLog007(t *testing.T) {
687 got := parseSlowLog(t, "slow007.log", opt)
688 expect := []log.Event{
689 {
690 Query: "SELECT fruit FROM trees",
691 Db: "db2",
692 Admin: false,
693 Host: "",
694 Ts: time.Date(2007, 12, 18, 11, 48, 27, 0, time.UTC),
695 User: "[SQL_SLAVE]",
696 Offset: 0,
697 OffsetEnd: 193,
698 BoolMetrics: map[string]bool{},
699 TimeMetrics: map[string]float64{
700 "Query_time": 0.000012,
701 "Lock_time": 0.000000,
702 },
703 NumberMetrics: map[string]uint64{
704 "Rows_examined": 0,
705 "Rows_sent": 0,
706 "Thread_id": 3,
707 },
708 },
709 }
710 assert.EqualValues(t, expect, got)
711 }
712
713 // slow008 has 4 interesting things (which makes it a poor test case since we're
714 // testing many things at once):
715 // 1. an admin command, e.g.: # administrator command: Quit;
716 // 2. a SET NAMES query; SET <certain vars> are ignored
717 // 3. No Time metrics
718 // 4. IPs in the host metric, but we don't currently support these
719 func TestParserSlowLog008(t *testing.T) {
720 got := parseSlowLog(t, "slow008.log", opt)
721 expect := []log.Event{
722 {
723 Query: "Quit",
724 Db: "db1",
725 Admin: true,
726 Host: "",
727 User: "meow",
728 Offset: 0,
729 OffsetEnd: 220,
730 BoolMetrics: map[string]bool{},
731 TimeMetrics: map[string]float64{
732 "Query_time": 0.000002,
733 "Lock_time": 0.000000,
734 },
735 NumberMetrics: map[string]uint64{
736 "Rows_examined": 0,
737 "Rows_sent": 0,
738 "Thread_id": 5,
739 },
740 },
741 {
742 Query: "SET NAMES utf8",
743 Db: "db",
744 Admin: false,
745 Host: "",
746 User: "meow",
747 Offset: 220,
748 OffsetEnd: 434,
749 BoolMetrics: map[string]bool{},
750 TimeMetrics: map[string]float64{
751 "Query_time": 0.000899,
752 "Lock_time": 0.000000,
753 },
754 NumberMetrics: map[string]uint64{
755 "Rows_examined": 0,
756 "Rows_sent": 0,
757 "Thread_id": 6,
758 },
759 },
760 {
761 Query: "SELECT MIN(id),MAX(id) FROM tbl",
762 Db: "db2",
763 Admin: false,
764 Host: "",
765 User: "meow",
766 Offset: 434,
767 OffsetEnd: 656,
768 BoolMetrics: map[string]bool{},
769 TimeMetrics: map[string]float64{
770 "Query_time": 0.018799,
771 "Lock_time": 0.009453,
772 },
773 NumberMetrics: map[string]uint64{
774 "Rows_examined": 0,
775 "Rows_sent": 0,
776 "Thread_id": 6,
777 },
778 },
779 }
780 assert.EqualValues(t, expect, got)
781 }
782
783 // Filter admin commands
784 func TestParserSlowLog009(t *testing.T) {
785 opt := opt
786 opt.FilterAdminCommand = map[string]bool{
787 "Quit": true,
788 }
789 got := parseSlowLog(t, "slow009.log", opt)
790 expect := []log.Event{
791 {
792 Query: "Refresh",
793 Db: "",
794 Admin: true,
795 Host: "localhost",
796 User: "root",
797 Offset: 196,
798 OffsetEnd: 562,
799 Ts: time.Date(2009, 3, 11, 18, 11, 50, 0, time.UTC),
800 TimeMetrics: map[string]float64{
801 "Query_time": 0.017850,
802 "Lock_time": 0.000000,
803 },
804 NumberMetrics: map[string]uint64{
805 "Merge_passes": 0,
806 "Rows_affected": 0,
807 "Rows_examined": 0,
808 "Rows_read": 0,
809 "Rows_sent": 0,
810 "Thread_id": 47,
811 },
812 BoolMetrics: map[string]bool{
813 "QC_Hit": false,
814 "Full_scan": false,
815 "Full_join": false,
816 "Tmp_table": false,
817 "Tmp_table_on_disk": false,
818 "Filesort": false,
819 "Filesort_on_disk": false,
820 },
821 },
822 }
823 assert.EqualValues(t, expect, got)
824 }
825
826 // Rate limit
827 func TestParserSlowLog011(t *testing.T) {
828 got := parseSlowLog(t, "slow011.log", opt)
829 expect := []log.Event{
830 {
831 Offset: 0,
832 OffsetEnd: 732,
833 Query: "SELECT foo FROM bar WHERE id=1",
834 Db: "maindb",
835 Host: "localhost",
836 User: "user1",
837 Ts: time.Date(2013, 11, 28, 1, 5, 31, 0, time.UTC),
838 RateType: "query",
839 RateLimit: 2,
840 TimeMetrics: map[string]float64{
841 "Query_time": 0.000228,
842 "Lock_time": 0.000114,
843 "InnoDB_IO_r_wait": 0.000000,
844 "InnoDB_rec_lock_wait": 0.000000,
845 "InnoDB_queue_wait": 0.000000,
846 },
847 NumberMetrics: map[string]uint64{
848 "InnoDB_trx_id": 0,
849 "Killed": 0,
850 "Last_errno": 0,
851 "Rows_sent": 1,
852 "Rows_examined": 1,
853 "Rows_affected": 0,
854 "Bytes_sent": 545,
855 "Tmp_tables": 0,
856 "Tmp_disk_tables": 0,
857 "Tmp_table_sizes": 0,
858 "Merge_passes": 0,
859 "InnoDB_IO_r_ops": 0,
860 "InnoDB_IO_r_bytes": 0,
861 "InnoDB_pages_distinct": 2,
862 },
863 BoolMetrics: map[string]bool{
864 "QC_Hit": false,
865 "Full_scan": false,
866 "Full_join": false,
867 "Tmp_table": false,
868 "Tmp_table_on_disk": false,
869 "Filesort": false,
870 "Filesort_on_disk": false,
871 },
872 },
873 {
874 Offset: 732,
875 OffsetEnd: 1440,
876 Query: "SELECT foo FROM bar WHERE id=2",
877 Db: "maindb",
878 Host: "localhost",
879 User: "user1",
880 RateType: "query",
881 RateLimit: 2,
882 TimeMetrics: map[string]float64{
883 "Query_time": 0.000237,
884 "Lock_time": 0.000122,
885 "InnoDB_IO_r_wait": 0.000000,
886 "InnoDB_rec_lock_wait": 0.000000,
887 "InnoDB_queue_wait": 0.000000,
888 },
889 NumberMetrics: map[string]uint64{
890 "InnoDB_trx_id": 0,
891 "Killed": 0,
892 "Last_errno": 0,
893 "Rows_sent": 1,
894 "Rows_examined": 1,
895 "Rows_affected": 0,
896 "Bytes_sent": 545,
897 "Tmp_tables": 0,
898 "Tmp_disk_tables": 0,
899 "Tmp_table_sizes": 0,
900 "Merge_passes": 0,
901 "InnoDB_IO_r_ops": 0,
902 "InnoDB_IO_r_bytes": 0,
903 "InnoDB_pages_distinct": 2,
904 },
905 BoolMetrics: map[string]bool{
906 "QC_Hit": false,
907 "Full_scan": false,
908 "Full_join": false,
909 "Tmp_table": false,
910 "Tmp_table_on_disk": false,
911 "Filesort": false,
912 "Filesort_on_disk": false,
913 },
914 },
915 {
916 Offset: 1440,
917 OffsetEnd: 2152,
918 Query: "INSERT INTO foo VALUES (NULL, 3)",
919 Db: "maindb",
920 Host: "localhost",
921 User: "user1",
922 RateType: "query",
923 RateLimit: 2,
924 TimeMetrics: map[string]float64{
925 "Query_time": 0.000165,
926 "Lock_time": 0.000048,
927 "InnoDB_IO_r_wait": 0.000000,
928 "InnoDB_rec_lock_wait": 0.000000,
929 "InnoDB_queue_wait": 0.000000,
930 },
931 NumberMetrics: map[string]uint64{
932 "InnoDB_trx_id": 0,
933 "Killed": 0,
934 "Last_errno": 0,
935 "Rows_sent": 5,
936 "Rows_examined": 10,
937 "Rows_affected": 0,
938 "Bytes_sent": 481,
939 "Tmp_tables": 0,
940 "Tmp_disk_tables": 0,
941 "Tmp_table_sizes": 0,
942 "Merge_passes": 0,
943 "InnoDB_IO_r_ops": 0,
944 "InnoDB_IO_r_bytes": 0,
945 "InnoDB_pages_distinct": 3,
946 },
947 BoolMetrics: map[string]bool{
948 "QC_Hit": false,
949 "Full_scan": false,
950 "Full_join": false,
951 "Tmp_table": false,
952 "Tmp_table_on_disk": false,
953 "Filesort": true,
954 "Filesort_on_disk": false,
955 },
956 },
957 }
958 assert.EqualValues(t, expect, got)
959 }
960
961 func TestParserSlowLog012(t *testing.T) {
962 got := parseSlowLog(t, "slow012.log", opt)
963 expect := []log.Event{
964 {
965 Query: "select * from mysql.user",
966 Db: "",
967 Host: "localhost",
968 User: "msandbox",
969 Offset: 0,
970 OffsetEnd: 185,
971 TimeMetrics: map[string]float64{
972 "Query_time": 0.000214,
973 "Lock_time": 0.000086,
974 },
975 NumberMetrics: map[string]uint64{
976 "Rows_sent": 2,
977 "Rows_examined": 2,
978 },
979 BoolMetrics: map[string]bool{},
980 },
981 {
982 Query: "Quit",
983 Admin: true,
984 Db: "",
985 Host: "localhost",
986 User: "msandbox",
987 Offset: 185,
988 OffsetEnd: 375,
989 TimeMetrics: map[string]float64{
990 "Query_time": 0.000016,
991 "Lock_time": 0.000000,
992 },
993 NumberMetrics: map[string]uint64{
994 "Rows_sent": 2,
995 "Rows_examined": 2,
996 },
997 BoolMetrics: map[string]bool{},
998 },
999 {
1000 Query: "SELECT @@max_allowed_packet",
1001 Db: "dev_pct",
1002 Host: "localhost",
1003 User: "msandbox",
1004 Offset: 375,
1005 OffsetEnd: 609,
1006 Ts: time.Date(2014, 4, 13, 19, 34, 13, 0, time.UTC),
1007 TimeMetrics: map[string]float64{
1008 "Query_time": 0.000127,
1009 "Lock_time": 0.000000,
1010 },
1011 NumberMetrics: map[string]uint64{
1012 "Rows_sent": 1,
1013 "Rows_examined": 0,
1014 },
1015 BoolMetrics: map[string]bool{},
1016 },
1017 }
1018 assert.EqualValues(t, expect, got)
1019 }
1020
1021 // Stack overflow bug due to meta lines.
1022 func TestParserSlowLog013(t *testing.T) {
1023 got := parseSlowLog(t, "slow013.log", opt)
1024 expect := []log.Event{
1025 {
1026 Offset: 0,
1027 OffsetEnd: 353,
1028 Ts: time.Date(2014, 2, 24, 22, 39, 34, 0, time.UTC),
1029 Query: "select 950,q.* from qcm q INTO OUTFILE '/mnt/pct/exp/qcm_db950.txt'",
1030 User: "root",
1031 Host: "localhost",
1032 Db: "db950",
1033 TimeMetrics: map[string]float64{
1034 "Query_time": 21.876617,
1035 "Lock_time": 0.002991,
1036 },
1037 NumberMetrics: map[string]uint64{
1038 "Bytes_sent": 14,
1039 "Killed": 0,
1040 "Last_errno": 0,
1041 "Rows_affected": 1605306,
1042 "Rows_examined": 1605306,
1043 "Rows_sent": 1605306,
1044 },
1045 BoolMetrics: map[string]bool{},
1046 },
1047 {
1048 Offset: 353,
1049 OffsetEnd: 6138,
1050 Ts: time.Date(2014, 2, 24, 22, 39, 59, 0, time.UTC),
1051 Query: "select 961,q.* from qcm q INTO OUTFILE '/mnt/pct/exp/qcm_db961.txt'",
1052 User: "root",
1053 Host: "localhost",
1054 Db: "db961",
1055 TimeMetrics: map[string]float64{
1056 "Query_time": 20.304536,
1057 "Lock_time": 0.103324,
1058 },
1059 NumberMetrics: map[string]uint64{
1060 "Bytes_sent": 14,
1061 "Killed": 0,
1062 "Last_errno": 0,
1063 "Rows_affected": 1197472,
1064 "Rows_examined": 1197472,
1065 "Rows_sent": 1197472,
1066 },
1067 BoolMetrics: map[string]bool{},
1068 },
1069 {
1070 Offset: 6138,
1071 OffsetEnd: 6666,
1072 Ts: time.Date(2014, 3, 11, 16, 7, 40, 0, time.UTC),
1073 Query: "select count(*) into @discard from `information_schema`.`PARTITIONS`",
1074 User: "debian-sys-maint",
1075 Host: "localhost",
1076 Db: "",
1077 TimeMetrics: map[string]float64{
1078 "Query_time": 94.38144,
1079 "Lock_time": 0.000174,
1080 },
1081 NumberMetrics: map[string]uint64{
1082 "Bytes_sent": 11,
1083 "Killed": 0,
1084 "Last_errno": 1146,
1085 "Rows_affected": 1,
1086 "Rows_examined": 17799,
1087 "Rows_sent": 0,
1088 },
1089 BoolMetrics: map[string]bool{},
1090 },
1091 {
1092 Offset: 6666,
1093 OffsetEnd: 7014,
1094 Ts: time.Date(2014, 3, 12, 20, 28, 40, 0, time.UTC),
1095 Query: "select 1,q.* from qcm q INTO OUTFILE '/mnt/pct/exp/qcm_db1.txt'",
1096 User: "root",
1097 Host: "localhost",
1098 Db: "db1",
1099 TimeMetrics: map[string]float64{
1100 "Query_time": 407.540262,
1101 "Lock_time": 0.122377,
1102 },
1103 NumberMetrics: map[string]uint64{
1104 "Bytes_sent": 19,
1105 "Killed": 0,
1106 "Last_errno": 0,
1107 "Rows_affected": 34621308,
1108 "Rows_examined": 34621308,
1109 "Rows_sent": 34621308,
1110 },
1111 BoolMetrics: map[string]bool{},
1112 },
1113 {
1114 Offset: 7014,
1115 OffsetEnd: 7370,
1116 Ts: time.Date(2014, 3, 12, 20, 29, 40, 0, time.UTC),
1117 Query: "select 1006,q.* from qcm q INTO OUTFILE '/mnt/pct/exp/qcm_db1006.txt'",
1118 User: "root",
1119 Host: "localhost",
1120 Db: "db1006",
1121 TimeMetrics: map[string]float64{
1122 "Query_time": 60.507698,
1123 "Lock_time": 0.002719,
1124 },
1125 NumberMetrics: map[string]uint64{
1126 "Bytes_sent": 14,
1127 "Killed": 0,
1128 "Last_errno": 0,
1129 "Rows_affected": 4937738,
1130 "Rows_examined": 4937738,
1131 "Rows_sent": 4937738,
1132 },
1133 BoolMetrics: map[string]bool{},
1134 },
1135 }
1136 assert.EqualValues(t, expect, got)
1137 }
1138
1139 // Query line looks like header line.
1140 func TestParserSlowLog014(t *testing.T) {
1141 got := parseSlowLog(t, "slow014.log", opt)
1142 expect := []log.Event{
1143 {
1144 Offset: 0,
1145 OffsetEnd: 690,
1146 Admin: false,
1147 Query: "SELECT * FROM cache\n WHERE `cacheid` IN ('id15965')",
1148 User: "root",
1149 Host: "localhost",
1150 Db: "db1",
1151 TimeMetrics: map[string]float64{
1152 "InnoDB_IO_r_wait": 0,
1153 "InnoDB_queue_wait": 0,
1154 "InnoDB_rec_lock_wait": 0,
1155 "Lock_time": 4.7e-05,
1156 "Query_time": 0.000179,
1157 },
1158 NumberMetrics: map[string]uint64{
1159 "Bytes_sent": 2004,
1160 "InnoDB_IO_r_bytes": 0,
1161 "InnoDB_IO_r_ops": 0,
1162 "InnoDB_pages_distinct": 3,
1163 "InnoDB_trx_id": 0,
1164 "Killed": 0,
1165 "Last_errno": 0,
1166 "Merge_passes": 0,
1167 "Rows_affected": 0,
1168 "Rows_examined": 1,
1169 "Rows_read": 1,
1170 "Rows_sent": 1,
1171 "Thread_id": 103375137,
1172 "Tmp_disk_tables": 0,
1173 "Tmp_table_sizes": 0,
1174 "Tmp_tables": 0,
1175 },
1176 BoolMetrics: map[string]bool{
1177 "Filesort": false,
1178 "Filesort_on_disk": false,
1179 "Full_join": false,
1180 "Full_scan": false,
1181 "QC_Hit": false,
1182 "Tmp_table": false,
1183 "Tmp_table_on_disk": false,
1184 },
1185 },
1186 {
1187 /**
1188 * Here it is:
1189 */
1190 Offset: 690,
1191 OffsetEnd: 2104,
1192 Admin: false,
1193 Query: "### Channels ###\n\u0009\u0009\u0009\u0009\u0009SELECT sourcetable, IF(f.lastcontent = 0, f.lastupdate, f.lastcontent) AS lastactivity,\n\u0009\u0009\u0009\u0009\u0009f.totalcount AS activity, type.class AS type,\n\u0009\u0009\u0009\u0009\u0009(f.nodeoptions \u0026 512) AS noUnsubscribe\n\u0009\u0009\u0009\u0009\u0009FROM node AS f\n\u0009\u0009\u0009\u0009\u0009INNER JOIN contenttype AS type ON type.contenttypeid = f.contenttypeid \n\n\u0009\u0009\u0009\u0009\u0009INNER JOIN subscribed AS sd ON sd.did = f.nodeid AND sd.userid = 15965\n UNION ALL \n\n\u0009\u0009\u0009\u0009\u0009### Users ###\n\u0009\u0009\u0009\u0009\u0009SELECT f.name AS title, f.userid AS keyval, 'user' AS sourcetable, IFNULL(f.lastpost, f.joindate) AS lastactivity,\n\u0009\u0009\u0009\u0009\u0009f.posts as activity, 'Member' AS type,\n\u0009\u0009\u0009\u0009\u00090 AS noUnsubscribe\n\u0009\u0009\u0009\u0009\u0009FROM user AS f\n\u0009\u0009\u0009\u0009\u0009INNER JOIN userlist AS ul ON ul.relationid = f.userid AND ul.userid = 15965\n\u0009\u0009\u0009\u0009\u0009WHERE ul.type = 'f' AND ul.aq = 'yes'\n ORDER BY title ASC LIMIT 100",
1194 User: "root",
1195 Host: "localhost",
1196 Db: "db1",
1197 TimeMetrics: map[string]float64{
1198 "InnoDB_IO_r_wait": 0,
1199 "InnoDB_queue_wait": 0,
1200 "InnoDB_rec_lock_wait": 0,
1201 "Lock_time": 0.000161,
1202 "Query_time": 0.000628,
1203 },
1204 NumberMetrics: map[string]uint64{
1205 "Bytes_sent": 323,
1206 "InnoDB_IO_r_bytes": 0,
1207 "InnoDB_IO_r_ops": 0,
1208 "InnoDB_pages_distinct": 3,
1209 "InnoDB_trx_id": 0,
1210 "Killed": 0,
1211 "Last_errno": 0,
1212 "Merge_passes": 0,
1213 "Rows_affected": 0,
1214 "Rows_examined": 0,
1215 "Rows_read": 0,
1216 "Rows_sent": 0,
1217 "Thread_id": 103375137,
1218 "Tmp_disk_tables": 0,
1219 "Tmp_table_sizes": 0,
1220 "Tmp_tables": 1,
1221 },
1222 BoolMetrics: map[string]bool{
1223 "Filesort": true,
1224 "Filesort_on_disk": false,
1225 "Full_join": false,
1226 "Full_scan": true,
1227 "QC_Hit": false,
1228 "Tmp_table": true,
1229 "Tmp_table_on_disk": false,
1230 },
1231 },
1232 {
1233 Offset: 2104,
1234 OffsetEnd: 3163,
1235 Query: "SELECT COUNT(userfing.keyval) AS total\n\u0009\u0009\u0009FROM\n\u0009\u0009\u0009((### All Content ###\n\u0009\u0009\u0009\u0009\u0009SELECT f.nodeid AS keyval\n\u0009\u0009\u0009\u0009\u0009FROM node AS f\n\u0009\u0009\u0009\u0009\u0009INNER JOIN subscribed AS sd ON sd.did = f.nodeid AND sd.userid = 15965) UNION ALL (\n\u0009\u0009\u0009\u0009\u0009### Users ###\n\u0009\u0009\u0009\u0009\u0009SELECT f.userid AS keyval\n\u0009\u0009\u0009\u0009\u0009FROM user AS f\n\u0009\u0009\u0009\u0009\u0009INNER JOIN userlist AS ul ON ul.relationid = f.userid AND ul.userid = 15965\n\u0009\u0009\u0009\u0009\u0009WHERE ul.type = 'f' AND ul.aq = 'yes')\n) AS userfing",
1236 User: "root",
1237 Host: "localhost",
1238 Db: "db1",
1239 TimeMetrics: map[string]float64{
1240 "InnoDB_IO_r_wait": 0,
1241 "InnoDB_queue_wait": 0,
1242 "InnoDB_rec_lock_wait": 0,
1243 "Lock_time": 0.000116,
1244 "Query_time": 0.00042,
1245 },
1246 NumberMetrics: map[string]uint64{
1247 "Bytes_sent": 60,
1248 "InnoDB_IO_r_bytes": 0,
1249 "InnoDB_IO_r_ops": 0,
1250 "InnoDB_pages_distinct": 3,
1251 "InnoDB_trx_id": 0,
1252 "Killed": 0,
1253 "Last_errno": 0,
1254 "Merge_passes": 0,
1255 "Rows_affected": 0,
1256 "Rows_examined": 0,
1257 "Rows_read": 0,
1258 "Rows_sent": 1,
1259 "Thread_id": 103375137,
1260 "Tmp_disk_tables": 0,
1261 "Tmp_table_sizes": 0,
1262 "Tmp_tables": 2,
1263 },
1264 BoolMetrics: map[string]bool{
1265 "Filesort": false,
1266 "Filesort_on_disk": false,
1267 "Full_join": false,
1268 "Full_scan": true,
1269 "QC_Hit": false,
1270 "Tmp_table": true,
1271 "Tmp_table_on_disk": false,
1272 },
1273 },
1274 {
1275 Offset: 3163,
1276 OffsetEnd: 4410,
1277 Query: "SELECT u.userid, u.name AS name, u.usergroupid AS usergroupid, IFNULL(u.lastactivity, u.joindate) as lastactivity,\n\u0009\u0009\u0009\u0009IFNULL((SELECT userid FROM userlist AS ul2 WHERE ul2.userid = 15965 AND ul2.relationid = u.userid AND ul2.type = 'f' AND ul2.aq = 'yes'), 0) as isFollowing,\n\u0009\u0009\u0009\u0009IFNULL((SELECT userid FROM userlist AS ul2 WHERE ul2.userid = 15965 AND ul2.relationid = u.userid AND ul2.type = 'f' AND ul2.aq = 'pending'), 0) as isPending\nFROM user AS u\n\u0009\u0009\u0009\u0009INNER JOIN userlist AS ul ON (u.userid = ul.userid AND ul.relationid = 15965)\n\n\u0009\u0009\u0009WHERE ul.type = 'f' AND ul.aq = 'yes'\nORDER BY name ASC\nLIMIT 0, 100",
1278 User: "root",
1279 Host: "localhost",
1280 Db: "db1",
1281 TimeMetrics: map[string]float64{
1282 "InnoDB_IO_r_wait": 0,
1283 "InnoDB_queue_wait": 0,
1284 "InnoDB_rec_lock_wait": 0,
1285 "Lock_time": 0.000144,
1286 "Query_time": 0.000457,
1287 },
1288 NumberMetrics: map[string]uint64{
1289 "Bytes_sent": 359,
1290 "InnoDB_IO_r_bytes": 0,
1291 "InnoDB_IO_r_ops": 0,
1292 "InnoDB_pages_distinct": 1,
1293 "InnoDB_trx_id": 0,
1294 "Killed": 0,
1295 "Last_errno": 0,
1296 "Merge_passes": 0,
1297 "Rows_affected": 0,
1298 "Rows_examined": 0,
1299 "Rows_read": 0,
1300 "Rows_sent": 0,
1301 "Thread_id": 103375137,
1302 "Tmp_disk_tables": 0,
1303 "Tmp_table_sizes": 0,
1304 "Tmp_tables": 1,
1305 },
1306 BoolMetrics: map[string]bool{
1307 "Filesort": true,
1308 "Filesort_on_disk": false,
1309 "Full_join": false,
1310 "Full_scan": false,
1311 "QC_Hit": false,
1312 "Tmp_table": true,
1313 "Tmp_table_on_disk": false,
1314 },
1315 },
1316 }
1317 assert.EqualValues(t, expect, got)
1318 }
1319
1320 // Correct event offsets when parsing starts/resumes at an offset.
1321 func TestParserSlowLog001StartOffset(t *testing.T) {
1322 opt := opt
1323 opt.StartOffset = 358
1324 // 358 is the first byte of the second (of 2) events.
1325 got := parseSlowLog(t, "slow001.log", opt)
1326 expect := []log.Event{
1327 {
1328 Ts: time.Date(2007, 10, 15, 21, 45, 10, 0, time.UTC),
1329 Query: `select sleep(2) from test.n`,
1330 User: "root",
1331 Host: "localhost",
1332 Db: "sakila",
1333 Offset: 358,
1334 OffsetEnd: 524,
1335 TimeMetrics: map[string]float64{
1336 "Query_time": 2,
1337 "Lock_time": 0,
1338 },
1339 NumberMetrics: map[string]uint64{
1340 "Rows_sent": 1,
1341 "Rows_examined": 0,
1342 },
1343 BoolMetrics: map[string]bool{},
1344 },
1345 }
1346 assert.EqualValues(t, expect, got)
1347 }
1348
1349 // Line > bufio.MaxScanTokenSize = 64KiB
1350 // https://jira.percona.com/browse/PCT-552
1351 func TestParserSlowLog015(t *testing.T) {
1352 got := parseSlowLog(t, "slow015.log", log.Options{})
1353 assert.Len(t, got, 2)
1354 }
1355
1356 // Start in header
1357 func TestParseSlow016(t *testing.T) {
1358 got := parseSlowLog(t, "slow016.log", log.Options{Debug: false})
1359 expect := []log.Event{
1360 {
1361 Query: `SHOW /*!50002 GLOBAL */ STATUS`,
1362 User: "pt_agent",
1363 Host: "localhost",
1364 Offset: 159,
1365 OffsetEnd: 413,
1366 TimeMetrics: map[string]float64{
1367 "Query_time": 0.003953,
1368 "Lock_time": 0.000059,
1369 },
1370 NumberMetrics: map[string]uint64{
1371 "Killed": 0,
1372 "Last_errno": 0,
1373 "Rows_sent": 571,
1374 "Rows_examined": 571,
1375 "Rows_affected": 0,
1376 },
1377 BoolMetrics: map[string]bool{},
1378 },
1379 }
1380 assert.EqualValues(t, expect, got)
1381 }
1382
1383 // Start in query
1384 func TestParseSlow017(t *testing.T) {
1385 got := parseSlowLog(t, "slow017.log", opt)
1386 expect := []log.Event{
1387 {
1388 Query: `SHOW /*!50002 GLOBAL */ STATUS`,
1389 User: "pt_agent",
1390 Host: "localhost",
1391 Offset: 26,
1392 OffsetEnd: 280,
1393 TimeMetrics: map[string]float64{
1394 "Query_time": 0.003953,
1395 "Lock_time": 0.000059,
1396 },
1397 NumberMetrics: map[string]uint64{
1398 "Killed": 0,
1399 "Last_errno": 0,
1400 "Rows_sent": 571,
1401 "Rows_examined": 571,
1402 "Rows_affected": 0,
1403 },
1404 BoolMetrics: map[string]bool{},
1405 },
1406 }
1407 assert.EqualValues(t, expect, got)
1408 }
1409
1410 func TestParseSlow019(t *testing.T) {
1411 got := parseSlowLog(t, "slow019.log", opt)
1412 expect := []log.Event{
1413 {
1414 Query: `SELECT TABLE_SCHEMA, TABLE_NAME, ROWS_READ, ROWS_CHANGED, ROWS_CHANGED_X_INDEXES FROM INFORMATION_SCHEMA.TABLE_STATISTICS`,
1415 User: "percona-agent",
1416 Host: "localhost",
1417 Offset: 0,
1418 OffsetEnd: 641,
1419 TimeMetrics: map[string]float64{
1420 "Lock_time": 0.0001,
1421 "Query_time": 0.004599,
1422 },
1423 NumberMetrics: map[string]uint64{
1424 "Bytes_sent": 70092,
1425 "Killed": 0,
1426 "Last_errno": 0,
1427 "Merge_passes": 0,
1428 "Rows_affected": 0,
1429 "Rows_examined": 1473,
1430 "Rows_read": 1473,
1431 "Rows_sent": 1473,
1432 "Thread_id": 37911936,
1433 "Tmp_disk_tables": 0,
1434 "Tmp_table_sizes": 0,
1435 "Tmp_tables": 1,
1436 },
1437 BoolMetrics: map[string]bool{
1438 "Filesort": false,
1439 "Filesort_on_disk": false,
1440 "Full_join": false,
1441 "Full_scan": true,
1442 "QC_Hit": false,
1443 "Tmp_table": true,
1444 "Tmp_table_on_disk": false,
1445 },
1446 },
1447 {
1448 Query: `SELECT cid, data, created, expire, serialized FROM cache_field WHERE cid IN ('field_info:bundle_extra:user:user')`,
1449 User: "root",
1450 Host: "localhost",
1451 Offset: 641,
1452 OffsetEnd: 1273,
1453 Db: "cod7_plos15",
1454 TimeMetrics: map[string]float64{
1455 "Lock_time": 0,
1456 "Query_time": 2.2e-05,
1457 },
1458 NumberMetrics: map[string]uint64{
1459 "Bytes_sent": 1333,
1460 "Killed": 0,
1461 "Last_errno": 0,
1462 "Merge_passes": 0,
1463 "Rows_affected": 0,
1464 "Rows_examined": 0,
1465 "Rows_read": 0,
1466 "Rows_sent": 0,
1467 "Thread_id": 57434695,
1468 "Tmp_disk_tables": 0,
1469 "Tmp_table_sizes": 0,
1470 "Tmp_tables": 0,
1471 },
1472 BoolMetrics: map[string]bool{
1473 "Filesort": false,
1474 "Filesort_on_disk": false,
1475 "Full_join": false,
1476 "Full_scan": false,
1477 "QC_Hit": true,
1478 "Tmp_table": false,
1479 "Tmp_table_on_disk": false,
1480 },
1481 },
1482 {
1483 Query: "UPDATE captcha_sessions SET timestamp='1413583348', solution='1'\nWHERE (csid = '28439')",
1484 User: "root",
1485 Host: "localhost",
1486 Offset: 1273,
1487 OffsetEnd: 2005,
1488 Db: "cod7_plos15",
1489 TimeMetrics: map[string]float64{
1490 "InnoDB_IO_r_wait": 0,
1491 "InnoDB_queue_wait": 0,
1492 "InnoDB_rec_lock_wait": 0,
1493 "Lock_time": 7.8e-05,
1494 "Query_time": 0.005241,
1495 },
1496 NumberMetrics: map[string]uint64{
1497 "Bytes_sent": 52,
1498 "InnoDB_IO_r_bytes": 0,
1499 "InnoDB_IO_r_ops": 0,
1500 "InnoDB_pages_distinct": 8,
1501 "InnoDB_trx_id": 0,
1502 "Killed": 0,
1503 "Last_errno": 0,
1504 "Merge_passes": 0,
1505 "Rows_affected": 1,
1506 "Rows_examined": 1,
1507 "Rows_read": 1,
1508 "Rows_sent": 0,
1509 "Thread_id": 57434695,
1510 "Tmp_disk_tables": 0,
1511 "Tmp_table_sizes": 0,
1512 "Tmp_tables": 0,
1513 },
1514 BoolMetrics: map[string]bool{
1515 "Filesort": false,
1516 "Filesort_on_disk": false,
1517 "Full_join": false,
1518 "Full_scan": false,
1519 "QC_Hit": false,
1520 "Tmp_table": false,
1521 "Tmp_table_on_disk": false,
1522 },
1523 },
1524 {
1525 Query: `SELECT TABLE_SCHEMA, TABLE_NAME, INDEX_NAME, ROWS_READ FROM INFORMATION_SCHEMA.INDEX_STATISTICS`,
1526 User: "percona-agent",
1527 Host: "localhost",
1528 Offset: 2005,
1529 OffsetEnd: 2621,
1530 TimeMetrics: map[string]float64{
1531 "Lock_time": 0.000115,
1532 "Query_time": 0.011565,
1533 },
1534 NumberMetrics: map[string]uint64{
1535 "Bytes_sent": 102084,
1536 "Killed": 0,
1537 "Last_errno": 0,
1538 "Merge_passes": 0,
1539 "Rows_affected": 0,
1540 "Rows_examined": 2146,
1541 "Rows_read": 2146,
1542 "Rows_sent": 2146,
1543 "Thread_id": 37911936,
1544 "Tmp_disk_tables": 0,
1545 "Tmp_table_sizes": 0,
1546 "Tmp_tables": 1,
1547 },
1548 BoolMetrics: map[string]bool{
1549 "Filesort": false,
1550 "Filesort_on_disk": false,
1551 "Full_join": false,
1552 "Full_scan": true,
1553 "QC_Hit": false,
1554 "Tmp_table": true,
1555 "Tmp_table_on_disk": false,
1556 },
1557 },
1558 }
1559 assert.EqualValues(t, expect, got)
1560 }
1561
1562 // Test db is not inherited and multiple "use" commands.
1563 func TestParseSlow023(t *testing.T) {
1564 got := parseSlowLog(t, "slow023.log", opt)
1565 expect := []log.Event{
1566 // Slice 0
1567 {
1568 Offset: 176,
1569 OffsetEnd: 418,
1570 Admin: false,
1571 Query: "SELECT field FROM table_a WHERE some_other_field = 'yahoo' LIMIT 1",
1572 User: "bookblogs",
1573 Host: "localhost",
1574 Db: "dbnamea",
1575 TimeMetrics: map[string]float64{
1576 "Query_time": 0.321092,
1577 "Lock_time": 3.8e-05,
1578 },
1579 NumberMetrics: map[string]uint64{
1580 "Rows_sent": 0,
1581 "Rows_examined": 0,
1582 },
1583 BoolMetrics: map[string]bool{},
1584 RateType: "",
1585 RateLimit: 0,
1586 },
1587 // Slice 1
1588 {
1589 Offset: 418,
1590 OffsetEnd: 595,
1591 Admin: false,
1592 Query: "SET NAMES utf8",
1593 User: "bookblogs",
1594 Host: "localhost",
1595 Db: "",
1596 TimeMetrics: map[string]float64{
1597 "Lock_time": 0,
1598 "Query_time": 0.253052,
1599 },
1600 NumberMetrics: map[string]uint64{
1601 "Rows_sent": 0,
1602 "Rows_examined": 0,
1603 },
1604 BoolMetrics: map[string]bool{},
1605 RateType: "",
1606 RateLimit: 0,
1607 },
1608 // Slice 2
1609 {
1610 Offset: 595,
1611 OffsetEnd: 794,
1612 Admin: false,
1613 Query: "SET GLOBAL slow_query_log=ON",
1614 User: "percona-agent",
1615 Host: "localhost",
1616 Db: "",
1617 TimeMetrics: map[string]float64{
1618 "Query_time": 0.31645,
1619 "Lock_time": 0,
1620 },
1621 NumberMetrics: map[string]uint64{
1622 "Rows_sent": 0,
1623 "Rows_examined": 0,
1624 },
1625 BoolMetrics: map[string]bool{},
1626 RateType: "",
1627 RateLimit: 0,
1628 },
1629 // Slice 3
1630 {
1631 Offset: 794,
1632 OffsetEnd: 982,
1633 Admin: false,
1634 Query: "SELECT @@SESSION.sql_mode",
1635 User: "bookblogs",
1636 Host: "localhost",
1637 Db: "",
1638 TimeMetrics: map[string]float64{
1639 "Query_time": 3.7e-05,
1640 "Lock_time": 0,
1641 },
1642 NumberMetrics: map[string]uint64{
1643 "Rows_examined": 0,
1644 "Rows_sent": 1,
1645 },
1646 BoolMetrics: map[string]bool{},
1647 RateType: "",
1648 RateLimit: 0,
1649 },
1650 // Slice 4
1651 {
1652 Offset: 982,
1653 OffsetEnd: 1218,
1654 Admin: false,
1655 Query: "SELECT field FROM table_b WHERE another_field = 'bazinga' AND site_id = 1",
1656 User: "bookblogs",
1657 Host: "localhost",
1658 Db: "",
1659 TimeMetrics: map[string]float64{
1660 "Query_time": 0.000297,
1661 "Lock_time": 0.000141,
1662 },
1663 NumberMetrics: map[string]uint64{
1664 "Rows_sent": 1,
1665 "Rows_examined": 1,
1666 },
1667 BoolMetrics: map[string]bool{},
1668 RateType: "", RateLimit: 0,
1669 },
1670 // Slice 5
1671 {
1672 Offset: 1218,
1673 OffsetEnd: 1388,
1674 Admin: false,
1675 Query: "use `dbnameb`",
1676 User: "backup",
1677 Host: "localhost",
1678 Db: "dbnameb",
1679 TimeMetrics: map[string]float64{
1680 "Lock_time": 0,
1681 "Query_time": 0.000558,
1682 },
1683 NumberMetrics: map[string]uint64{
1684 "Rows_examined": 0,
1685 "Rows_sent": 0,
1686 },
1687 BoolMetrics: map[string]bool{},
1688 RateType: "",
1689 RateLimit: 0,
1690 },
1691 // Slice 6
1692 {
1693 Offset: 1388,
1694 OffsetEnd: 1572,
1695 Admin: false,
1696 Query: "select @@collation_database",
1697 User: "backup",
1698 Host: "localhost",
1699 Db: "",
1700 TimeMetrics: map[string]float64{
1701 "Query_time": 0.000204,
1702 "Lock_time": 0,
1703 },
1704 NumberMetrics: map[string]uint64{
1705 "Rows_sent": 1,
1706 "Rows_examined": 0,
1707 },
1708 BoolMetrics: map[string]bool{},
1709 RateType: "",
1710 RateLimit: 0,
1711 },
1712 // Slice 7
1713 {
1714 Offset: 1572,
1715 OffsetEnd: 1817,
1716 Admin: false,
1717 Query: "SELECT another_field FROM table_c WHERE a_third_field = 'tiruriru' AND site_id = 1",
1718 User: "bookblogs",
1719 Host: "localhost",
1720 Db: "",
1721 TimeMetrics: map[string]float64{
1722 "Query_time": 0.000164,
1723 "Lock_time": 5.9e-05,
1724 },
1725 NumberMetrics: map[string]uint64{
1726 "Rows_sent": 1,
1727 "Rows_examined": 1,
1728 },
1729 BoolMetrics: map[string]bool{},
1730 RateType: "",
1731 RateLimit: 0,
1732 },
1733 }
1734 assert.EqualValues(t, expect, got)
1735 }
1736
1737 func TestParseSlow023A(t *testing.T) {
1738 filename := "slow023.log"
1739 o := log.Options{Debug: false}
1740
1741 file, err := os.Open(path.Join(sample, "/", filename))
1742 if err != nil {
1743 l.Fatal(err)
1744 }
1745 p := parser.NewSlowLogParser(file, o)
1746 if err != nil {
1747 l.Fatal(err)
1748 }
1749 go p.Start()
1750 lastQuery := ""
1751 for e := range p.EventChan() {
1752 if e.Query == "" {
1753 t.Errorf("Empty query at offset: %d. Last valid query: %s\n", e.Offset, lastQuery)
1754 } else {
1755 lastQuery = e.Query
1756 }
1757 }
1758 }
1759
1760 /*
1761 Test header with invalid # Time or invalid # User lines
1762 */
1763 func TestParseSlow024(t *testing.T) {
1764 got := parseSlowLog(t, "slow024.log", opt)
1765 expect := []log.Event{
1766 {
1767 Offset: 199,
1768 OffsetEnd: 361,
1769 Ts: time.Date(2007, 10, 15, 21, 43, 52, 0, time.UTC),
1770 Admin: false,
1771 Query: "select sleep(1) from n",
1772 User: "root",
1773 Host: "localhost",
1774 Db: "test",
1775 TimeMetrics: map[string]float64{
1776 "Lock_time": 0,
1777 "Query_time": 2,
1778 },
1779 NumberMetrics: map[string]uint64{
1780 "Rows_examined": 0,
1781 "Rows_sent": 1,
1782 },
1783 BoolMetrics: map[string]bool{},
1784 RateType: "",
1785 RateLimit: 0,
1786 },
1787 {
1788 Offset: 361,
1789 OffsetEnd: 507,
1790 Admin: false,
1791 Query: "select sleep(2) from n",
1792 User: "root",
1793 Host: "localhost",
1794 Db: "test",
1795 TimeMetrics: map[string]float64{
1796 "Lock_time": 0,
1797 "Query_time": 2,
1798 },
1799 NumberMetrics: map[string]uint64{
1800 "Rows_examined": 0,
1801 "Rows_sent": 1,
1802 },
1803 BoolMetrics: map[string]bool{},
1804 RateType: "",
1805 RateLimit: 0,
1806 },
1807 {
1808 Offset: 507,
1809 OffsetEnd: 644,
1810 Ts: time.Date(2007, 10, 15, 21, 43, 52, 0, time.UTC),
1811 Admin: false,
1812 Query: "select sleep(3) from n",
1813 User: "",
1814 Host: "",
1815 Db: "test",
1816 TimeMetrics: map[string]float64{
1817 "Lock_time": 0,
1818 "Query_time": 2,
1819 },
1820 NumberMetrics: map[string]uint64{
1821 "Rows_examined": 0,
1822 "Rows_sent": 1,
1823 },
1824 BoolMetrics: map[string]bool{},
1825 RateType: "",
1826 RateLimit: 0,
1827 },
1828 }
1829 assert.EqualValues(t, expect, got)
1830 }
1831
1832 // https://jira.percona.com/browse/PMM-1834
1833 func TestParseSlowMariaDBWithExplain(t *testing.T) {
1834 got := parseSlowLog(t, "mariadb102-with-explain.log", opt)
1835 expect := []log.Event{
1836 {
1837 Offset: 205,
1838 OffsetEnd: 630,
1839 Ts: time.Date(2018, 2, 14, 16, 18, 7, 0, time.UTC),
1840 Admin: false,
1841 Query: "SELECT 1",
1842 User: "root",
1843 Host: "localhost",
1844 Db: "",
1845 TimeMetrics: map[string]float64{
1846 "Lock_time": 0,
1847 "Query_time": 0.000277,
1848 },
1849 NumberMetrics: map[string]uint64{
1850 "Rows_affected": 0,
1851 "Rows_examined": 0,
1852 "Rows_sent": 1,
1853 "Thread_id": 8,
1854 },
1855 BoolMetrics: map[string]bool{
1856 "QC_hit": false,
1857 },
1858 RateType: "",
1859 RateLimit: 0,
1860 },
1861 }
1862 assert.EqualValues(t, expect, got)
1863 }
1864
1865 func TestParseSlow026(t *testing.T) {
1866 got := parseSlowLog(t, "slow026.log", opt)
1867 expect := []log.Event{
1868 {
1869 Offset: 0,
1870 OffsetEnd: 463,
1871 Ts: time.Date(2017, 12, 13, 2, 41, 18, 673330000, time.UTC),
1872 Admin: false,
1873 Query: "select 1",
1874 User: "test",
1875 Host: "",
1876 Db: "test",
1877 TimeMetrics: map[string]float64{
1878 "Lock_time": 0,
1879 "Query_time": 1.000249,
1880 },
1881 NumberMetrics: map[string]uint64{
1882 "Bytes_sent": 89,
1883 "Killed": 0,
1884 "Last_errno": 0,
1885 "Merge_passes": 0,
1886 "Rows_affected": 0,
1887 "Rows_examined": 0,
1888 "Rows_sent": 1,
1889 "Tmp_disk_tables": 0,
1890 "Tmp_table_sizes": 0,
1891 "Tmp_tables": 0,
1892 },
1893 BoolMetrics: map[string]bool{
1894 "Filesort": false,
1895 "Filesort_on_disk": false,
1896 "Full_join": false,
1897 "Full_scan": false,
1898 "QC_Hit": false,
1899 "Tmp_table": false,
1900 "Tmp_table_on_disk": false,
1901 },
1902 RateType: "",
1903 RateLimit: 0,
1904 },
1905 }
1906 assert.EqualValues(t, expect, got)
1907 }