File: resume.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 (170 lines) | stat: -rw-r--r-- 3,914 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
#!/usr/bin/env 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 IO::File;
use Fcntl      qw(:seek);
use File::Temp qw(tempfile);

use PerconaTest;
require "$trunk/bin/pt-query-digest";

my $samples = "$trunk/t/lib/samples/slowlogs";
my $output;

my $resume_file = (tempfile())[1];
diag(`echo 0 > $resume_file`);

my ($fh, $filename) = tempfile(UNLINK => 1);
$fh->autoflush(1);

sub resume_offset_ok {
   my ($resume_file, $file, $msg) = @_;
   chomp(my $offset = slurp_file($resume_file));
   open my $tmp_fh, q{<}, $file or die $OS_ERROR;
   seek $tmp_fh, 0, SEEK_END;
   is(tell($tmp_fh), $offset, $msg);
}

sub run_pqd {
   my @extra_args = @_;
   my $run = output(sub { pt_query_digest::main(qw(--limit 10), @extra_args, $filename) }, stderr => 1);
   $run =~ s/[\d.]+m?s user time.+//;
   $run =~ s/Current date: .+//;
   return $run;
}

print { $fh } slurp_file("$samples/slow006.txt");

my @runs;
push @runs, run_pqd() for 1, 2;

is(
   $runs[0],
   $runs[1],
   "Sanity check: Behaves the same between runs without --resume"
);

my @resume_runs;
push @resume_runs, run_pqd('--resume', $resume_file) for 1, 2;

# TODO
#(my $without_resume_line = $resume_runs[0]) =~ s/\n\n. Saved resume file offset.+//;
#is(
#   $runs[1],
#   $runs[0],
#   "First time with --resume just like the first time without"
#);

like(
   $resume_runs[0],
   qr/\QSaved resume file offset\E/,
   "Saves offset with --resume"
);

like(
   $resume_runs[1],
   qr/\QNo events processed.\E/,
   "..and there are no events on the second run"
);

resume_offset_ok(
   $resume_file,
   $filename,
   "The resume file has the correct offset"
);

print { $fh } slurp_file("$samples/slow002.txt");

push @resume_runs, run_pqd('--resume', $resume_file) for 1, 2;

unlike(
   $resume_runs[2],
   qr/\QNo events processed.\E/,
   "New run detects new events"
);

like(
   $resume_runs[3],
   qr/\QNo events processed.\E/,
   "And running again after that finds nothing new"
);

resume_offset_ok(
   $resume_file,
   $filename,
   "The resume file has the updated offset"
);

# #############################################################################
# Now test the itneraction with --run-time-mode interval
# #############################################################################

close $fh;
diag(`echo 0 > $resume_file`);

($fh, $filename) = tempfile(UNLINK => 1);
$fh->autoflush(1);

print { $fh } slurp_file("$trunk/t/lib/samples/slowlogs/slow033.txt");

my @run_args = (qw(--run-time-mode interval --run-time 1d --iterations 0),
                qw(--report-format query_report));
my @resume_args = (@run_args, '--resume', $resume_file);

my @run_time;
push @run_time, run_pqd(@resume_args) for 1,2;

resume_offset_ok(
   $resume_file,
   $filename,
   "The resume file has the correct offset when using --run-time-mode interval"
);

print { $fh } slurp_file("$samples/slow002.txt");

push @run_time, run_pqd(@resume_args) for 1,2;

resume_offset_ok(
   $resume_file,
   $filename,
   "...and it updates correctly"
);

like(
   $_,
   qr/\QNo events processed.\E/,
   "Runs 2 & 4 find no new data"
) for @run_time[1, 3];

# This shows up in the first report, but shouldn't show up in there
# third run, after we add new events to the file.
my $re = qr/\QSELECT * FROM foo\E/;

unlike(
   $run_time[2],
   $re,
   "Events from the first run are correctly ignored"
);

my $no_resume = run_pqd(@run_args);

like(
   $no_resume,
   $re,
   "...but do show up if run without resume"
);

# #############################################################################
# Done.
# #############################################################################
done_testing;