Test pt-fingerprint.

This commit is contained in:
Daniel Nichter
2012-03-26 17:24:44 -06:00
parent beaa9240e7
commit 9c0e642443
6 changed files with 82 additions and 3 deletions

View File

@@ -1871,6 +1871,9 @@ sub main {
while ( <> ) {
my $query = $_;
chomp $query;
$query =~ s/^#.+$//mg;
$query =~ s/^\s+//;
next unless $query =~ m/^\w/;
print $qr->fingerprint($query), "\n";
}
}

View File

@@ -9,14 +9,15 @@ BEGIN {
use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More tests => 1;
use Test::More tests => 7;
use PerconaTest;
require "$trunk/bin/pt-fingerprint";
my @args = qw(--report-format=query_report --limit 10);
my $sample = "$trunk/t/lib/samples/slowlogs/";
my @args = qw();
my $output;
my $sample = "$trunk/t/pt-fingerprint/samples";
my $pqd = "$trunk/bin/pt-query-digest";
$output = `$trunk/bin/pt-fingerprint --help`;
like(
@@ -25,6 +26,75 @@ like(
"It runs"
);
sub test_query_file {
my ($file) = @_;
if ( ! -f "$sample/$file.fingerprint" ) {
`$pqd --fingerprint $sample/$file | awk '/Fingerprint/ { getline; print; exit; }' | sed -e 's/^#[ ]*//' > $sample/$file.fingerprint`;
diag("Created $sample/$file.fingerprint");
}
chomp(my $expect = `cat $sample/$file.fingerprint`);
my $got = output(
sub { pt_fingerprint::main("$sample/$file") }
);
chomp($got);
is(
$got,
$expect,
"$file fingerprint"
);
};
opendir my $dir, $sample or die "Cannot open $sample: $OS_ERROR\n";
while (defined(my $file = readdir($dir))) {
next unless $file =~ m/^query\d+$/;
test_query_file($file);
}
closedir $dir;
sub test_query {
my (%args) = @_;
my $query = $args{query};
my $expect = $args{expect};
my @ops = $args{ops} ? @{$args{ops}} : ();
$output = output(
sub { pt_fingerprint::main('--query', $query, @ops) }
);
chomp($output);
is(
$output,
$expect,
$args{name} ? $args{name} : "Fingerprint " . substr($query, 0, 70)
);
}
test_query(
query => 'select * from tbl where id=1',
expect => 'select * from tbl where id=?',
);
test_query(
name => "Fingerprint MD5_word",
query => "SELECT c FROM db.fbc5e685a5d3d45aa1d0347fdb7c4d35_temp where id=1",
expect => "select c from db.?_temp where id=?",
ops => [qw(--match-md5-checksums)],
);
test_query(
name => "Fingerprint word_MD5",
query => "SELECT c FROM db.temp_fbc5e685a5d3d45aa1d0347fdb7c4d35 where id=1",
expect => "select c from db.temp_? where id=?",
ops => [qw(--match-md5-checksums)],
);
test_query(
name => "Fingerprint word<number>",
query => "SELECT c FROM db.catch22 WHERE id is null",
expect => "select c from db.catch22 where id is ?",
ops => [qw(--match-embedded-numbers)],
);
# #############################################################################
# Done.
# #############################################################################

View File

@@ -0,0 +1,2 @@
# Query_time: 1
select * from db.tbl where id=1 or foo='bar';

View File

@@ -0,0 +1 @@
select * from db.tbl where id=? or foo=?

View File

@@ -0,0 +1,2 @@
# Query_time: 1
select col from db.tbl1 where id in (1, 2, 3);

View File

@@ -0,0 +1 @@
select col from db.tbl? where id in(?+)