Merge clean-up-pqd-2.2.

This commit is contained in:
Daniel Nichter
2013-03-09 10:24:56 -07:00
35 changed files with 1533 additions and 1652 deletions

View File

@@ -1526,11 +1526,14 @@ ok(
'New event class has new attrib; default unroll_limit(issue 514)'
);
$ea = new EventAggregator(
groupby => 'arg',
worst => 'Query_time',
unroll_limit => 50,
);
$ea = do {
local $ENV{PT_QUERY_DIGEST_CHECK_ATTRIB_LIMIT} = 50;
new EventAggregator(
groupby => 'arg',
worst => 'Query_time'
);
};
parse_file('t/lib/samples/slowlogs/slow030.txt', $p, $ea);
ok(
!exists $ea->{unrolled_for}->{InnoDB_rec_lock_wait},

View File

@@ -11,6 +11,7 @@ use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More;
use ProtocolParser;
use MySQLProtocolParser;
use TcpdumpParser;
use PerconaTest;
@@ -285,16 +286,9 @@ test_protocol_parser(
# #############################################################################
# Check the individual packet parsing subs.
# #############################################################################
MySQLProtocolParser->import(qw(
parse_error_packet
parse_ok_packet
parse_server_handshake_packet
parse_client_handshake_packet
parse_com_packet
));
is_deeply(
parse_error_packet(load_data("t/lib/samples/mysql_proto_001.txt")),
MySQLProtocolParser::parse_error_packet(load_data("t/lib/samples/mysql_proto_001.txt")),
{
errno => '1046',
sqlstate => '#3D000',
@@ -304,7 +298,7 @@ is_deeply(
);
is_deeply(
parse_ok_packet('010002000100'),
MySQLProtocolParser::parse_ok_packet('010002000100'),
{
affected_rows => 1,
insert_id => 0,
@@ -316,7 +310,7 @@ is_deeply(
);
is_deeply(
parse_server_handshake_packet(load_data("t/lib/samples/mysql_proto_002.txt")),
MySQLProtocolParser::parse_server_handshake_packet(load_data("t/lib/samples/mysql_proto_002.txt")),
{
thread_id => '9',
server_version => '5.0.67-0ubuntu6-log',
@@ -345,7 +339,7 @@ is_deeply(
);
is_deeply(
parse_client_handshake_packet(load_data("t/lib/samples/mysql_proto_003.txt")),
MySQLProtocolParser::parse_client_handshake_packet(load_data("t/lib/samples/mysql_proto_003.txt")),
{
db => 'mysql',
user => 'msandbox',
@@ -374,7 +368,7 @@ is_deeply(
);
is_deeply(
parse_com_packet('0373686f77207761726e696e67738d2dacbc', 14),
MySQLProtocolParser::parse_com_packet('0373686f77207761726e696e67738d2dacbc', 14),
{
code => '03',
com => 'COM_QUERY',
@@ -1274,6 +1268,7 @@ test_protocol_parser(
# Issue 761: mk-query-digest --tcpdump does not handle incomplete packets
# #############################################################################
$protocol = new MySQLProtocolParser(server=>'127.0.0.1',port=>'3306');
$protocol->{_no_save_error} = 1;
test_protocol_parser(
parser => $tcpdump,
protocol => $protocol,
@@ -1449,6 +1444,7 @@ test_protocol_parser(
);
$protocol = new MySQLProtocolParser();
$protocol->{_no_save_error} = 1;
test_protocol_parser(
parser => $tcpdump,
protocol => $protocol,
@@ -1528,6 +1524,7 @@ test_protocol_parser(
);
$protocol = new MySQLProtocolParser();
$protocol->{_no_save_error} = 1;
test_protocol_parser(
parser => $tcpdump,
protocol => $protocol,
@@ -1576,6 +1573,7 @@ test_protocol_parser(
# client query
# #############################################################################
$protocol = new MySQLProtocolParser(server => '127.0.0.1',port=>'12345');
$protocol->{_no_save_error} = 1;
$e = test_protocol_parser(
parser => $tcpdump,
protocol => $protocol,
@@ -1595,6 +1593,7 @@ $protocol = new MySQLProtocolParser(
server => '127.0.0.1',
port => '3306',
);
$protocol->{_no_save_error} = 1;
test_protocol_parser(
parser => $tcpdump,
protocol => $protocol,
@@ -1745,7 +1744,41 @@ test_protocol_parser(
],
);
# #############################################################################
# Save errors by default
# #############################################################################
$protocol = new MySQLProtocolParser(server=>'127.0.0.1',port=>'3306');
my $out = output(sub {
open my $fh, "<", "$sample/tcpdump032.txt" or die "Cannot open tcpdump032.txt: $OS_ERROR";
my %parser_args = (
next_event => sub { return <$fh>; },
tell => sub { return tell($fh); },
);
while ( my $p = $tcpdump->parse_event(%parser_args) ) {
$protocol->parse_event(%parser_args, event => $p);
}
close $fh;
});
like(
$out,
qr/had errors, will save them in /,
"Saves errors by default"
);
close $protocol->{errors_fh}; # flush the handle
like(
slurp_file($protocol->{errors_file}),
qr/got server response before full buffer/,
"The right error is saved"
);
# #############################################################################
# Done.
# #############################################################################
# Get rid of error files
`rm /tmp/MySQLProtocolParser.t-errors.*`;
done_testing;

199
t/lib/QueryHistory.t Normal file
View File

@@ -0,0 +1,199 @@
#!/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 QueryHistory;
use TableParser;
use Quoter;
use OptionParser;
use DSNParser;
use Sandbox;
use PerconaTest;
my $dp = new DSNParser(opts=>$dsn_opts);
my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp);
my $dbh = $sb->get_dbh_for('master', {no_lc=>1});
if ( !$dbh ) {
plan skip_all => "Cannot connect to sandbox master";
}
$sb->create_dbs($dbh, ['test']);
$sb->load_file('master', "t/lib/samples/query_review.sql");
my $output = "";
my $tp = new TableParser(Quoter => 'Quoter');
my $opt_parser = new OptionParser( description => 'QueryHistory tests' );
my $pat = $opt_parser->read_para_after("$trunk/bin/pt-query-digest", qr/\bMAGIC_history_columns\b/);
$pat =~ s/\s+//g;
$pat = qr/^(.*?)_($pat)$/;
my $qh = QueryHistory->new(
history_dbh => $dbh,
column_pattern => $pat,
);
isa_ok($qh, 'QueryHistory');
# ##############################################################################
# Test review history stuff
# ##############################################################################
my $create_table = $opt_parser->read_para_after(
"$trunk/bin/pt-query-digest", qr/MAGIC_create_history_table/);
$create_table =~ s/query_history/test.query_review_history/;
$dbh->do($create_table);
my $hist_struct = $tp->parse(
$tp->get_create_table($dbh, 'test', 'query_review_history'));
$qh->set_history_options(
table => 'test.query_review_history',
tbl_struct => $hist_struct,
);
$qh->set_review_history(
'foo',
'foo sample',
Query_time => {
pct => 1/3,
sum => '0.000682',
cnt => 1,
min => '0.000682',
max => '0.000682',
avg => '0.000682',
median => '0.000682',
stddev => 0,
pct_95 => '0.000682',
},
ts => {
min => '090101 12:39:12',
max => '090101 13:19:12',
cnt => 1,
},
);
my $res = $dbh->selectall_arrayref(
'SELECT Lock_time_median, Lock_time_stddev, Query_time_sum, checksum, Rows_examined_stddev, ts_cnt, sample, Rows_examined_median, Rows_sent_min, Rows_examined_min, Rows_sent_sum, Query_time_min, Query_time_pct_95, Rows_examined_sum, Rows_sent_stddev, Rows_sent_pct_95, Query_time_max, Rows_examined_max, Query_time_stddev, Rows_sent_median, Lock_time_pct_95, ts_min, Lock_time_min, Lock_time_max, ts_max, Rows_examined_pct_95 ,Rows_sent_max, Query_time_median, Lock_time_sum
FROM test.query_review_history',
{ Slice => {} });
is_deeply(
$res,
[ { checksum => '17145033699835028696',
sample => 'foo sample',
ts_min => '2009-01-01 12:39:12',
ts_max => '2009-01-01 13:19:12',
ts_cnt => 1,
Query_time_sum => '0.000682',
Query_time_min => '0.000682',
Query_time_max => '0.000682',
Query_time_median => '0.000682',
Query_time_stddev => 0,
Query_time_pct_95 => '0.000682',
Lock_time_sum => undef,
Lock_time_min => undef,
Lock_time_max => undef,
Lock_time_pct_95 => undef,
Lock_time_stddev => undef,
Lock_time_median => undef,
Rows_sent_sum => undef,
Rows_sent_min => undef,
Rows_sent_max => undef,
Rows_sent_pct_95 => undef,
Rows_sent_stddev => undef,
Rows_sent_median => undef,
Rows_examined_sum => undef,
Rows_examined_min => undef,
Rows_examined_max => undef,
Rows_examined_pct_95 => undef,
Rows_examined_stddev => undef,
Rows_examined_median => undef,
},
],
'Review history information is in the DB',
);
eval {
$qh->set_review_history(
'foo',
'foo sample',
ts => {
min => undef,
max => undef,
cnt => 1,
},
);
};
is($EVAL_ERROR, '', 'No error on undef ts_min and ts_max');
# #############################################################################
# Issue 1265: mk-query-digest --review-history table with minimum 2 columns
# #############################################################################
$dbh->do('truncate table test.query_review');
$dbh->do('drop table test.query_review_history');
# mqd says "The table must have at least the following columns:"
my $min_tbl = "CREATE TABLE query_review_history (
checksum BIGINT UNSIGNED NOT NULL,
sample TEXT NOT NULL
)";
$dbh->do($min_tbl);
$hist_struct = $tp->parse(
$tp->get_create_table($dbh, 'test', 'query_review_history'));
$qh->set_history_options(
table => 'test.query_review_history',
tbl_struct => $hist_struct,
);
eval {
$qh->set_review_history(
'foo',
'foo sample',
Query_time => {
pct => 1/3,
sum => '0.000682',
cnt => 1,
min => '0.000682',
max => '0.000682',
avg => '0.000682',
median => '0.000682',
stddev => 0,
pct_95 => '0.000682',
},
ts => {
min => '090101 12:39:12',
max => '090101 13:19:12',
cnt => 1,
},
);
};
is(
$EVAL_ERROR,
"",
"Minimum 2-column review history table (issue 1265)"
);
# #############################################################################
# Done.
# #############################################################################
{
local *STDERR;
open STDERR, '>', \$output;
$qh->_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");
done_testing;
exit;

View File

@@ -464,7 +464,7 @@ ok(
# #############################################################################
# This test uses the $ea from the Bool pretty printer test above.
my $sorted = $qrf->sort_attribs($ea->get_attributes(), $ea);
my $sorted = $qrf->sort_attribs($ea);
is_deeply(
$sorted,
{
@@ -511,7 +511,7 @@ foreach my $event (@$events) {
}
$ea->calculate_statistical_metrics();
$sorted = $qrf->sort_attribs($ea->get_attributes(), $ea);
$sorted = $qrf->sort_attribs($ea);
is_deeply(
$sorted,
{
@@ -689,7 +689,7 @@ ok(
"t/lib/samples/QueryReportFormatter/report010.txt",
cmd_output => 1,
),
'Truncate one long string'
"Don't truncate one long string"
);
$ea->reset_aggregated_data();
@@ -721,7 +721,7 @@ ok(
"t/lib/samples/QueryReportFormatter/report011.txt",
cmd_output => 1,
),
'Truncate multiple long strings'
"Don't truncate multiple long strings"
);
$ea->reset_aggregated_data();
@@ -753,7 +753,7 @@ ok(
"t/lib/samples/QueryReportFormatter/report012.txt",
cmd_output => 1,
),
'Truncate multiple strings longer than whole line'
"Don't truncate multiple strings longer than whole line"
);
# #############################################################################
@@ -881,16 +881,6 @@ ok(
"IPs not shortened with more"
);
# Test show_all.
@ARGV = qw(--show-all host);
$o->get_opts();
$qrf = new QueryReportFormatter(
OptionParser => $o,
QueryRewriter => $qr,
QueryParser => $qp,
Quoter => $q,
ExplainAnalyzer => $ex,
);
$result = $qrf->event_report(
ea => $ea,
select => [ qw(Query_time host) ],

View File

@@ -17,7 +17,6 @@ use QueryRewriter;
use TableParser;
use Quoter;
use SlowLogParser;
use OptionParser;
use DSNParser;
use Sandbox;
use PerconaTest;
@@ -29,9 +28,6 @@ my $dbh = $sb->get_dbh_for('master', {no_lc=>1});
if ( !$dbh ) {
plan skip_all => "Cannot connect to sandbox master";
}
else {
plan tests => 9;
}
$sb->create_dbs($dbh, ['test']);
$sb->load_file('master', "t/lib/samples/query_review.sql");
@@ -40,7 +36,7 @@ my $qr = new QueryRewriter();
my $lp = new SlowLogParser;
my $q = new Quoter();
my $tp = new TableParser(Quoter => $q);
my $opt_parser = new OptionParser( description => 'hi' );
my $tbl_struct = $tp->parse(
$tp->get_create_table($dbh, 'test', 'query_review'));
@@ -146,148 +142,6 @@ is_deeply([$qv->review_cols],
[qw(first_seen last_seen reviewed_by reviewed_on comments)],
'review columns');
# ##############################################################################
# Test review history stuff
# ##############################################################################
my $pat = $opt_parser->read_para_after("$trunk/bin/pt-query-digest",
qr/MAGIC_history_cols/);
$pat =~ s/\s+//g;
my $create_table = $opt_parser->read_para_after(
"$trunk/bin/pt-query-digest", qr/MAGIC_create_review_history/);
$create_table =~ s/query_review_history/test.query_review_history/;
$dbh->do($create_table);
my $hist_struct = $tp->parse(
$tp->get_create_table($dbh, 'test', 'query_review_history'));
$qv->set_history_options(
table => 'test.query_review_history',
quoter => $q,
tbl_struct => $hist_struct,
col_pat => qr/^(.*?)_($pat)$/,
);
$qv->set_review_history(
'foo',
'foo sample',
Query_time => {
pct => 1/3,
sum => '0.000682',
cnt => 1,
min => '0.000682',
max => '0.000682',
avg => '0.000682',
median => '0.000682',
stddev => 0,
pct_95 => '0.000682',
},
ts => {
min => '090101 12:39:12',
max => '090101 13:19:12',
cnt => 1,
},
);
$res = $dbh->selectall_arrayref(
'SELECT Lock_time_median, Lock_time_stddev, Query_time_sum, checksum, Rows_examined_stddev, ts_cnt, sample, Rows_examined_median, Rows_sent_min, Rows_examined_min, Rows_sent_sum, Query_time_min, Query_time_pct_95, Rows_examined_sum, Rows_sent_stddev, Rows_sent_pct_95, Query_time_max, Rows_examined_max, Query_time_stddev, Rows_sent_median, Lock_time_pct_95, ts_min, Lock_time_min, Lock_time_max, ts_max, Rows_examined_pct_95 ,Rows_sent_max, Query_time_median, Lock_time_sum
FROM test.query_review_history',
{ Slice => {} });
is_deeply(
$res,
[ { checksum => '17145033699835028696',
sample => 'foo sample',
ts_min => '2009-01-01 12:39:12',
ts_max => '2009-01-01 13:19:12',
ts_cnt => 1,
Query_time_sum => '0.000682',
Query_time_min => '0.000682',
Query_time_max => '0.000682',
Query_time_median => '0.000682',
Query_time_stddev => 0,
Query_time_pct_95 => '0.000682',
Lock_time_sum => undef,
Lock_time_min => undef,
Lock_time_max => undef,
Lock_time_pct_95 => undef,
Lock_time_stddev => undef,
Lock_time_median => undef,
Rows_sent_sum => undef,
Rows_sent_min => undef,
Rows_sent_max => undef,
Rows_sent_pct_95 => undef,
Rows_sent_stddev => undef,
Rows_sent_median => undef,
Rows_examined_sum => undef,
Rows_examined_min => undef,
Rows_examined_max => undef,
Rows_examined_pct_95 => undef,
Rows_examined_stddev => undef,
Rows_examined_median => undef,
},
],
'Review history information is in the DB',
);
eval {
$qv->set_review_history(
'foo',
'foo sample',
ts => {
min => undef,
max => undef,
cnt => 1,
},
);
};
is($EVAL_ERROR, '', 'No error on undef ts_min and ts_max');
# #############################################################################
# Issue 1265: mk-query-digest --review-history table with minimum 2 columns
# #############################################################################
$dbh->do('truncate table test.query_review');
$dbh->do('drop table test.query_review_history');
# mqd says "The table must have at least the following columns:"
my $min_tbl = "CREATE TABLE query_review_history (
checksum BIGINT UNSIGNED NOT NULL,
sample TEXT NOT NULL
)";
$dbh->do($min_tbl);
$hist_struct = $tp->parse(
$tp->get_create_table($dbh, 'test', 'query_review_history'));
$qv->set_history_options(
table => 'test.query_review_history',
quoter => $q,
tbl_struct => $hist_struct,
col_pat => qr/^(.*?)_($pat)$/,
);
eval {
$qv->set_review_history(
'foo',
'foo sample',
Query_time => {
pct => 1/3,
sum => '0.000682',
cnt => 1,
min => '0.000682',
max => '0.000682',
avg => '0.000682',
median => '0.000682',
stddev => 0,
pct_95 => '0.000682',
},
ts => {
min => '090101 12:39:12',
max => '090101 13:19:12',
cnt => 1,
},
);
};
is(
$EVAL_ERROR,
"",
"Minimum 2-column review history table (issue 1265)"
);
# #############################################################################
# Done.
# #############################################################################
@@ -303,4 +157,5 @@ like(
);
$sb->wipe_clean($dbh);
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
done_testing;
exit;

View File

@@ -7,4 +7,4 @@
# Count 100 3
# Exec time 100 6s 1s 3s 2s 3s 780ms 2s
# String:
# foo Hi. I'm a... (1/33%), Me too! I'... (1/33%)... 1 more
# foo Hi. I'm a... (1/33%), Me too! I'... (1/33%), Number 3 l... (1/33%)

View File

@@ -5,4 +5,4 @@
# Count 100 2
# Exec time 100 16s 8s 8s 8s 8s 0 8s
# String:
# Hosts 123.123.123.456 (1/50%)... 1 more
# Hosts 123.123.123.456 (1/50%), 123.123.123.789 (1/50%)

View File

@@ -5,4 +5,4 @@
# Count 100 3
# Exec time 100 24s 8s 8s 8s 8s 0 8s
# String:
# Hosts 123.123.123.456 (1/33%)... 2 more
# Hosts 123.123.123.456 (1/33%), 123.123.123.789 (1/33%), 123.123.123.999 (1/33%)