File: EventTimeline.t

package info (click to toggle)
percona-toolkit 3.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 68,916 kB
  • sloc: perl: 241,287; sql: 22,868; sh: 19,746; javascript: 6,799; makefile: 353; awk: 38; python: 30; sed: 1
file content (145 lines) | stat: -rw-r--r-- 3,876 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl

BEGIN {
   die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
      unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
   unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
};

use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More tests => 3;

use Transformers;
use QueryRewriter;
use EventTimeline;
use PerconaTest;

my $qr = new QueryRewriter();
my ( $result, $events, $et, $expected );

$et = new EventTimeline(
   groupby    => [qw(fingerprint)],
   attributes => [qw(Query_time ts)],
);

$events = [
   {  cmd           => 'Query',
      ts            => '071015 21:43:52',
      user          => 'root',
      host          => 'localhost',
      ip            => '',
      arg           => "SELECT id FROM users WHERE name='foo'",
      Query_time    => '0.000652',
      Lock_time     => '0.000109',
      Rows_sent     => 1,
      Rows_examined => 1,
      pos_in_log    => 0,
   },
   {  ts   => '071015 21:43:52',
      cmd  => 'Query',
      user => 'root',
      host => 'localhost',
      ip   => '',
      arg =>
         "INSERT IGNORE INTO articles (id, body,)VALUES(3558268,'sample text')",
      Query_time    => '0.001943',
      Lock_time     => '0.000145',
      Rows_sent     => 0,
      Rows_examined => 0,
      pos_in_log    => 1,
   },
   {  ts   => '071015 21:49:52',
      cmd  => 'Query',
      user => 'root',
      host => 'localhost',
      ip   => '',
      arg =>
         "INSERT IGNORE INTO articles (id, body,)VALUES(3558268,'sample text')",
      Query_time    => '0.001943',
      Lock_time     => '0.000145',
      Rows_sent     => 0,
      Rows_examined => 0,
      pos_in_log    => 1,
   },
   {  ts            => '071015 21:43:52',
      cmd           => 'Query',
      user          => 'bob',
      host          => 'localhost',
      ip            => '',
      arg           => "SELECT id FROM users WHERE name='bar'",
      Query_time    => '0.000682',
      Lock_time     => '0.000201',
      Rows_sent     => 1,
      Rows_examined => 2,
      pos_in_log    => 5,
   }
];

$result = [
   [  ['select id from users where name=?'],
      1,
      {  Query_time => {
            min => '0.000652',
            max => '0.000652',
            sum => '0.000652',
         },
         ts => {
            min => '071015 21:43:52',
            max => '071015 21:43:52',
         },
      },
   ],
   [  ['insert ignore into articles (id, body,)values(?+)'],
      2,
      {  Query_time => {
            min => '0.001943',
            max => '0.001943',
            sum => 0.001943 * 2,
         },
         ts => {
            min => '071015 21:43:52',
            max => '071015 21:49:52',
         },
      },
   ],
   [  ['select id from users where name=?'],
      1,
      {  Query_time => {
            min => '0.000682',
            max => '0.000682',
            sum => '0.000682',
         },
         ts => {
            min => '071015 21:43:52',
            max => '071015 21:43:52',
         },
      },
   ],
];

foreach my $event (@$events) {
   $event->{fingerprint} = $qr->fingerprint( $event->{arg} );
   $et->aggregate($event);
}

is_deeply( $et->results,
   $result, 'Simple fingerprint aggregation' );

$expected = <<EOF;
# ########################################################################
# fingerprint report
# ########################################################################
# 2007-10-15 21:43:52    0:00   1 select id from users where name=?
# 2007-10-15 21:43:52   06:00   2 insert ignore into articles (id, body,)values(?+)
# 2007-10-15 21:43:52    0:00   1 select id from users where name=?
EOF

$result = '';
$et->report($et->results, sub { $result .= $_[0] });

$et->reset_aggregated_data();
is_deeply($et->results, [], 'reset_aggregated_data()');

is($result, $expected, 'Report for simple timeline');