File: CompareQueryTimes.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 (196 lines) | stat: -rw-r--r-- 5,065 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/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;

use ReportFormatter;
use Transformers;
use DSNParser;
use Sandbox;
use CompareQueryTimes;
use PerconaTest;

my $dp  = new DSNParser(opts=>$dsn_opts);
my $sb  = new Sandbox(basedir => '/tmp', DSNParser => $dp);
my $dbh = $sb->get_dbh_for('master');

if ( !$dbh ) {
   plan skip_all => "Cannot connect to sandbox master";
}
else {
   plan tests => 25;
}

$sb->create_dbs($dbh, ['test']);

Transformers->import(qw(make_checksum));

my $ct;
my $report;
my $hosts = [
   { dbh => $dbh, name => 'server1' },
   { dbh => $dbh, name => 'server2' },
];

sub get_id {
   return make_checksum(@_);
}

# #############################################################################
# Test it.
# #############################################################################

# diag(`/tmp/12345/use < samples/compare-warnings.sql`);

$ct = new CompareQueryTimes(
   get_id => \&get_id,
);

isa_ok($ct, 'CompareQueryTimes');

# #############################################################################
# Test query time comparison.
# #############################################################################
sub compare {
   my ( $t1, $t2 ) = @_;
   return $ct->compare(
      events => [
         { fingerprint => 'foo', Query_time => $t1, },
         { fingerprint => 'foo', Query_time => $t2, },
      ],
   );
}

sub test_compare_query_times {
   my ( $t1, $t2, $diff, $comment ) = @_;
   my %diff = compare($t1, $t2);
   my $msg  = sprintf("compare t %.6f vs. %.6f %s",
      $t1, $t2, ($comment || ''));
   is(
      $diff{different_query_times},
      $diff,
      $msg,
   );
}

test_compare_query_times(0, 0, 0);
test_compare_query_times(0, 0.000001, 1, 'increase from zero');
test_compare_query_times(0.000001, 0.000005, 0, 'no increase in bucket');
test_compare_query_times(0.000001, 0.000010, 1, '1 bucket diff on edge');
test_compare_query_times(0.000008, 0.000018, 1, '1 bucket diff');
test_compare_query_times(0.000001, 10, 1, 'full bucket range diff on edges');
test_compare_query_times(0.000008, 1000000, 1, 'huge diff');

# Thresholds
test_compare_query_times(0.000001, 0.000006, 1, '1us threshold');
test_compare_query_times(0.000010, 0.000020, 1, '10us threshold');
test_compare_query_times(0.000100, 0.000200, 1, '100us threshold');
test_compare_query_times(0.001000, 0.006000, 1, '1ms threshold');
test_compare_query_times(0.010000, 0.015000, 1, '10ms threshold');
test_compare_query_times(0.100000, 0.150000, 1, '100ms threshold');
test_compare_query_times(1.000000, 1.200000, 1, '1s threshold');
test_compare_query_times(10.0,     10.1,     1, '10s threshold');

# #############################################################################
# Test the main actions, which don't do much.
# #############################################################################
my $event = {
   fingerprint => 'set @a=?',
   arg         => 'set @a=3',
   sampleno    => 4,
};

$dbh->do('set @a=1');
is_deeply(
   $dbh->selectcol_arrayref('select @a'),
   [1],
   '@a set'
);

is_deeply(
   $ct->before_execute(event => $event),
   $event,
   "before_execute() doesn't modify event"
);

$ct->execute(event => $event, dbh => $dbh);

ok(
   exists $event->{Query_time}
   && $event->{Query_time} >= 0,
   'execute() set Query_time'
);

is_deeply(
   $dbh->selectcol_arrayref('select @a'),
   [3],
   'Query was actually executed'
);

is_deeply(
   $ct->after_execute(event => $event),
   $event,
   "after_execute() doesn't modify event"
);


# #############################################################################
# Test the reports.
# #############################################################################
$ct->reset();
compare(0.000100, 0.000250);

$report = <<EOF;
# Significant query time differences
# Query ID           host1 host2 %Increase %Threshold
# ================== ===== ===== ========= ==========
# EDEF654FCCC4A4D8-0 100us 250us    150.00        100
EOF

is(
   $ct->report(hosts => $hosts),
   $report,
   'report in bucket difference'
);

$ct->reset();
compare(0.000100, 1.100251);

$report = <<EOF;
# Big query time differences
# Query ID           host1 host2 Difference
# ================== ===== ===== ==========
# EDEF654FCCC4A4D8-0 100us    1s         1s
EOF

is(
   $ct->report(hosts => $hosts),
   $report,
   'report in bucket difference'
);

# #############################################################################
# Done.
# #############################################################################
my $output = '';
{
   local *STDERR;
   open STDERR, '>', \$output;
   $ct->_d('Complete test coverage');
}
like(
   $output,
   qr/Complete test coverage/,
   '_d() works'
);
$sb->wipe_clean($dbh);
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
exit;