/usr/share/perl5/CompareQueryTimes.pm is in percona-toolkit 3.0.6+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | # This program is copyright 2009-2011 Percona Ireland Ltd.
# Feedback and improvements are welcome.
#
# THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, version 2; OR the Perl Artistic License. On UNIX and similar
# systems, you can issue `man perlgpl' or `man perlartistic' to read these
# licenses.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA.
# ###########################################################################
# CompareQueryTimes package
# ###########################################################################
{
# Package: CompareQueryTimes
# CompareQueryTimes compares query execution times.
package CompareQueryTimes;
use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use constant PTDEBUG => $ENV{PTDEBUG} || 0;
Transformers->import(qw(micro_t));
use POSIX qw(floor);
use Data::Dumper;
$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Quotekeys = 0;
# Significant percentage increase for each bucket. For example,
# 1us to 4us is a 300% increase, but in reality that is not significant.
# But a 500% increase to 6us may be significant. In the 1s+ range (last
# bucket), since the time is already so bad, even a 20% increase (e.g. 1s
# to 1.2s) is significant.
my @bucket_threshold = qw(500 100 100 500 50 50 20 1 );
# my @bucket_labels = qw(1us 10us 100us 1ms 10ms 100ms 1s 10s+);
# Sub: new
#
# Parameters:
# %args - Arguments
#
# Required Arguments:
# get_id - Callback used by report() to transform query to its ID
#
# Returns:
# CompareQueryTimes object
sub new {
my ( $class, %args ) = @_;
my @required_args = qw(get_id);
foreach my $arg ( @required_args ) {
die "I need a $arg argument" unless $args{$arg};
}
my $self = {
%args,
diffs => {},
samples => {},
};
return bless $self, $class;
}
sub before_execute {
my ( $self, %args ) = @_;
my @required_args = qw(event);
foreach my $arg ( @required_args ) {
die "I need a $arg argument" unless $args{$arg};
}
return $args{event};
}
# Sub: execute
# Execute query if not already executed.
#
# Parameters:
# %args - Arguments
#
# Required Arguments:
# event - Hashref with event attributes and values
# dbh - dbh on which to execute the event
#
# Returns:
# Hashref of event with Query_time attribute added
sub execute {
my ( $self, %args ) = @_;
my @required_args = qw(event dbh);
foreach my $arg ( @required_args ) {
die "I need a $arg argument" unless $args{$arg};
}
my ($event, $dbh) = @args{@required_args};
if ( exists $event->{Query_time} ) {
PTDEBUG && _d('Query already executed');
return $event;
}
PTDEBUG && _d('Executing query');
my $query = $event->{arg};
my ( $start, $end, $query_time );
$event->{Query_time} = 0;
eval {
$start = time();
$dbh->do($query);
$end = time();
$query_time = sprintf '%.6f', $end - $start;
};
die "Failed to execute query: $EVAL_ERROR" if $EVAL_ERROR;
$event->{Query_time} = $query_time;
return $event;
}
sub after_execute {
my ( $self, %args ) = @_;
my @required_args = qw(event);
foreach my $arg ( @required_args ) {
die "I need a $arg argument" unless $args{$arg};
}
return $args{event};
}
# Sub: compare
# Compare executed events.
#
# Parameters:
# %args - Arguments
#
# Required Arguments:
# events - Arrayref of event hashrefs
#
# Returns:
# Hash of differences
sub compare {
my ( $self, %args ) = @_;
my @required_args = qw(events);
foreach my $arg ( @required_args ) {
die "I need a $arg argument" unless $args{$arg};
}
my ($events) = @args{@required_args};
my $different_query_times = 0;
my $event0 = $events->[0];
my $item = $event0->{fingerprint} || $event0->{arg};
my $sampleno = $event0->{sampleno} || 0;
my $t0 = $event0->{Query_time} || 0;
my $b0 = bucket_for($t0);
my $n_events = scalar @$events;
foreach my $i ( 1..($n_events-1) ) {
my $event = $events->[$i];
my $t = $event->{Query_time};
my $b = bucket_for($t);
if ( $b0 != $b ) {
# Save differences.
my $diff = abs($t0 - $t);
$different_query_times++;
$self->{diffs}->{big}->{$item}->{$sampleno}
= [ micro_t($t0), micro_t($t), micro_t($diff) ];
$self->{samples}->{$item}->{$sampleno} = $event0->{arg};
}
else {
my $inc = percentage_increase($t0, $t);
if ( $inc >= $bucket_threshold[$b0] ) {
# Save differences.
$different_query_times++;
$self->{diffs}->{in_bucket}->{$item}->{$sampleno}
= [ micro_t($t0), micro_t($t), $inc, $bucket_threshold[$b0] ];
$self->{samples}->{$item}->{$sampleno} = $event0->{arg};
}
}
}
return (
different_query_times => $different_query_times,
);
}
# Sub: buck_for
# Calculate bucket for value.
#
# Parameters:
# $val - Value
#
# Returns:
# Bucket number for value
sub bucket_for {
my ( $val ) = @_;
die "I need a val" unless defined $val;
return 0 if $val == 0;
my $bucket = floor(log($val) / log(10)) + 6;
$bucket = $bucket > 7 ? 7 : $bucket < 0 ? 0 : $bucket;
return $bucket;
}
# Sub: percentage_increase
# Calculate percentage increase between two values.
#
# Parameters:
# $x - First value
# $y - Second value
#
# Returns:
# Percentage increase from first to second value
sub percentage_increase {
my ( $x, $y ) = @_;
return 0 if $x == $y;
if ( $x > $y ) {
my $z = $y;
$y = $x;
$x = $z;
}
if ( $x == 0 ) {
return 1000; # This should trigger all buckets' thresholds.
}
return sprintf '%.2f', (($y - $x) / $x) * 100;
}
# Sub: report
# Report differences found.
#
# Parameters:
# %args - Arguments
#
# Required Arguments:
# hosts - Arrayref of hosts
#
# Returns:
# Report text of differences
sub report {
my ( $self, %args ) = @_;
my @required_args = qw(hosts);
foreach my $arg ( @required_args ) {
die "I need a $arg argument" unless $args{$arg};
}
my ($hosts) = @args{@required_args};
return unless keys %{$self->{diffs}};
# These columns are common to all the reports; make them just once.
my $query_id_col = {
name => 'Query ID',
};
my $hostno = 0;
my @host_cols = map {
$hostno++;
my $col = { name => "host$hostno" };
$col;
} @$hosts;
my @reports;
foreach my $diff ( qw(big in_bucket) ) {
my $report = "_report_diff_$diff";
push @reports, $self->$report(
query_id_col => $query_id_col,
host_cols => \@host_cols,
%args
);
}
return join("\n", @reports);
}
# Sub: _report_diff_big
# Report big differences in query times.
#
# Parameters:
# %args - Arguments
#
# Required Arguments:
# query_id_col - Hashref <ReportFormat> column descriptor
# hosts - Arrayref of hosts
#
# Returns:
# Big query time diff report
sub _report_diff_big {
my ( $self, %args ) = @_;
my @required_args = qw(query_id_col hosts);
foreach my $arg ( @required_args ) {
die "I need a $arg argument" unless $args{$arg};
}
my $get_id = $self->{get_id};
return unless keys %{$self->{diffs}->{big}};
my $report = new ReportFormatter();
$report->title('Big query time differences');
my $hostno = 0;
$report->set_columns(
$args{query_id_col},
(map {
$hostno++;
my $col = { name => "host$hostno", right_justify => 1 };
$col;
} @{$args{hosts}}),
{ name => 'Difference', right_justify => 1 },
);
my $diff_big = $self->{diffs}->{big};
foreach my $item ( sort keys %$diff_big ) {
map {
$report->add_line(
$get_id->($item) . '-' . $_,
@{$diff_big->{$item}->{$_}},
);
} sort { $a <=> $b } keys %{$diff_big->{$item}};
}
return $report->get_report();
}
# Sub: _report_diff_big
# Report smaller, "in bucket" query time differences.
#
# Parameters:
# %args - Arguments
#
# Required Arguments:
# query_id_col - Hashref <ReportFormat> column descriptor
# hosts - Arrayref of hosts
#
# Returns:
# In bucket query time diff report
sub _report_diff_in_bucket {
my ( $self, %args ) = @_;
my @required_args = qw(query_id_col hosts);
foreach my $arg ( @required_args ) {
die "I need a $arg argument" unless $args{$arg};
}
my $get_id = $self->{get_id};
return unless keys %{$self->{diffs}->{in_bucket}};
my $report = new ReportFormatter();
$report->title('Significant query time differences');
my $hostno = 0;
$report->set_columns(
$args{query_id_col},
(map {
$hostno++;
my $col = { name => "host$hostno", right_justify => 1 };
$col;
} @{$args{hosts}}),
{ name => '%Increase', right_justify => 1 },
{ name => '%Threshold', right_justify => 1 },
);
my $diff_in_bucket = $self->{diffs}->{in_bucket};
foreach my $item ( sort keys %$diff_in_bucket ) {
map {
$report->add_line(
$get_id->($item) . '-' . $_,
@{$diff_in_bucket->{$item}->{$_}},
);
} sort { $a <=> $b } keys %{$diff_in_bucket->{$item}};
}
return $report->get_report();
}
# Sub: samples
# Return samples of queries with differences.
#
# Parameters:
# $item - Query fingerprint
#
# Returns:
# Array of queries
sub samples {
my ( $self, $item ) = @_;
return unless $item;
my @samples;
foreach my $sampleno ( keys %{$self->{samples}->{$item}} ) {
push @samples, $sampleno, $self->{samples}->{$item}->{$sampleno};
}
return @samples;
}
# Sub: reset
# Reset internal state for another run.
sub reset {
my ( $self ) = @_;
$self->{diffs} = {};
$self->{samples} = {};
return;
}
sub _d {
my ($package, undef, $line) = caller 0;
@_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; }
map { defined $_ ? $_ : 'undef' }
@_;
print STDERR "# $package:$line $PID ", join(' ', @_), "\n";
}
1;
}
# ###########################################################################
# End CompareQueryTimes package
# ###########################################################################
|