mirror of
https://github.com/percona/percona-toolkit.git
synced 2026-04-11 01:01:36 +08:00
Test pt-fingerprint.
This commit is contained in:
@@ -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";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
# #############################################################################
|
||||
|
||||
2
t/pt-fingerprint/samples/query001
Normal file
2
t/pt-fingerprint/samples/query001
Normal file
@@ -0,0 +1,2 @@
|
||||
# Query_time: 1
|
||||
select * from db.tbl where id=1 or foo='bar';
|
||||
1
t/pt-fingerprint/samples/query001.fingerprint
Normal file
1
t/pt-fingerprint/samples/query001.fingerprint
Normal file
@@ -0,0 +1 @@
|
||||
select * from db.tbl where id=? or foo=?
|
||||
2
t/pt-fingerprint/samples/query002
Normal file
2
t/pt-fingerprint/samples/query002
Normal file
@@ -0,0 +1,2 @@
|
||||
# Query_time: 1
|
||||
select col from db.tbl1 where id in (1, 2, 3);
|
||||
1
t/pt-fingerprint/samples/query002.fingerprint
Normal file
1
t/pt-fingerprint/samples/query002.fingerprint
Normal file
@@ -0,0 +1 @@
|
||||
select col from db.tbl? where id in(?+)
|
||||
Reference in New Issue
Block a user