| 1 |
package Test2::Harness::Auditor::TimeTracker; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
our $VERSION = '1.000179'; |
| 6 |
|
| 7 |
use Test2::Harness::Util qw/hub_truth/; |
| 8 |
use Test2::Util::Times qw/render_duration/; |
| 9 |
|
| 10 |
use Test2::Harness::Util::HashBase qw{ |
| 11 |
-start -start_id |
| 12 |
-stop -stop_id |
| 13 |
-first -first_id |
| 14 |
-last -last_id |
| 15 |
-complete_id |
| 16 |
|
| 17 |
-_source |
| 18 |
-_totals |
| 19 |
}; |
| 20 |
|
| 21 |
sub process { |
| 22 |
my $self = shift; |
| 23 |
my ($event, $f, $assertion_count) = @_; |
| 24 |
|
| 25 |
# Invalidate cache |
| 26 |
delete $self->{+_TOTALS}; |
| 27 |
delete $self->{+_SOURCE}; |
| 28 |
|
| 29 |
my $stamp = $event->{stamp} or return; |
| 30 |
my $id = $event->{event_id} // 'N/A'; |
| 31 |
|
| 32 |
$f //= $event->{facet_data}; |
| 33 |
|
| 34 |
if ($f->{harness_job_exit}) { |
| 35 |
$self->{+STOP} = $stamp; |
| 36 |
$self->{+STOP_ID} = $id; |
| 37 |
} |
| 38 |
|
| 39 |
return if $self->{+COMPLETE_ID}; |
| 40 |
|
| 41 |
if ($f->{harness_job_start}) { |
| 42 |
$self->{+START} = $stamp; |
| 43 |
$self->{+START_ID} = $id; |
| 44 |
} |
| 45 |
|
| 46 |
# These events absolutely end the events phase, and do not count as part of |
| 47 |
# it. |
| 48 |
$self->{+COMPLETE_ID} //= $event->{event_id} if $f->{harness_job_exit}; |
| 49 |
$self->{+COMPLETE_ID} //= $event->{event_id} if $f->{control} && $f->{control}->{phase} && $f->{control}->{phase} eq 'END'; |
| 50 |
|
| 51 |
return if $self->{+COMPLETE_ID}; |
| 52 |
|
| 53 |
# Plan still counts as 'event' phase, so do not return if we are setting this now |
| 54 |
$self->{+COMPLETE_ID} //= $event->{event_id} if $assertion_count && $f->{plan} && !$f->{plan}->{none}; |
| 55 |
|
| 56 |
return unless $f->{trace}; # Events with traces are "event" phase. |
| 57 |
|
| 58 |
# Always replace the last, if we got this far. |
| 59 |
$self->{+LAST} = $stamp; |
| 60 |
$self->{+LAST_ID} = $id; |
| 61 |
|
| 62 |
# Only set the first one once |
| 63 |
return if $self->{+FIRST}; |
| 64 |
$self->{+FIRST} = $stamp; |
| 65 |
$self->{+FIRST_ID} = $id; |
| 66 |
|
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
sub useful { |
| 71 |
my $self = shift; |
| 72 |
|
| 73 |
my @got = grep { defined $self->{$_} } START, FIRST, LAST, STOP; |
| 74 |
return @got > 1; |
| 75 |
} |
| 76 |
|
| 77 |
my @TOTAL_FIELDS = qw/startup events cleanup total/; |
| 78 |
my %TOTAL_SOURCES = ( |
| 79 |
startup => [FIRST, START], |
| 80 |
events => [LAST, FIRST], |
| 81 |
cleanup => [STOP, LAST], |
| 82 |
total => [STOP, START] |
| 83 |
); |
| 84 |
my %TOTAL_DESC = ( |
| 85 |
startup => "Time from launch to first test event.", |
| 86 |
events => "Time spent generating test events.", |
| 87 |
cleanup => "Time from last test event to test exit.", |
| 88 |
total => "Total time", |
| 89 |
); |
| 90 |
|
| 91 |
sub totals { |
| 92 |
my $self = shift; |
| 93 |
|
| 94 |
return $self->{+_TOTALS} if $self->{+_TOTALS}; |
| 95 |
|
| 96 |
my $out = {}; |
| 97 |
|
| 98 |
for my $field (@TOTAL_FIELDS) { |
| 99 |
my $sources = $TOTAL_SOURCES{$field} or die "Invalid field: $field"; |
| 100 |
my @vals = @{$self}{@$sources}; |
| 101 |
next unless defined($vals[0]) && defined($vals[1]); |
| 102 |
|
| 103 |
my $delta = $vals[0] - $vals[1]; |
| 104 |
$out->{$field} = $delta; |
| 105 |
$out->{"h_$field"} = render_duration($delta); |
| 106 |
} |
| 107 |
|
| 108 |
return $self->{+_TOTALS} = $out; |
| 109 |
} |
| 110 |
|
| 111 |
sub source { |
| 112 |
my $self = shift; |
| 113 |
|
| 114 |
return $self->{+_SOURCE} if $self->{+_SOURCE}; |
| 115 |
|
| 116 |
my @fields = ( |
| 117 |
START, START_ID, |
| 118 |
STOP, STOP_ID, |
| 119 |
FIRST, FIRST_ID, |
| 120 |
LAST, LAST_ID, |
| 121 |
COMPLETE_ID, |
| 122 |
); |
| 123 |
|
| 124 |
my %out; |
| 125 |
@out{@fields} = @{$self}{@fields}; |
| 126 |
|
| 127 |
return $self->{+_SOURCE} = \%out; |
| 128 |
} |
| 129 |
|
| 130 |
sub data_dump { |
| 131 |
my $self = shift; |
| 132 |
|
| 133 |
return { |
| 134 |
totals => $self->totals, |
| 135 |
source => $self->source, |
| 136 |
}; |
| 137 |
} |
| 138 |
|
| 139 |
sub summary { |
| 140 |
my $self = shift; |
| 141 |
my $totals = $self->totals; |
| 142 |
|
| 143 |
my $summary = ""; |
| 144 |
for my $field (@TOTAL_FIELDS) { |
| 145 |
my $hval = $totals->{"h_$field"} // next; |
| 146 |
my $title = ucfirst($field); |
| 147 |
|
| 148 |
$summary .= " | " if $summary; |
| 149 |
$summary .= "$title: $hval"; |
| 150 |
} |
| 151 |
|
| 152 |
return $summary; |
| 153 |
} |
| 154 |
|
| 155 |
sub table { |
| 156 |
my $self = shift; |
| 157 |
my $totals = $self->totals; |
| 158 |
|
| 159 |
my $table = { |
| 160 |
header => ["Phase", "Time", "Raw", "Explanation"], |
| 161 |
rows => [], |
| 162 |
}; |
| 163 |
|
| 164 |
for my $field (@TOTAL_FIELDS) { |
| 165 |
my $val = $totals->{$field} // next; |
| 166 |
my $hval = $totals->{"h_$field"}; |
| 167 |
my $title = ucfirst($field); |
| 168 |
|
| 169 |
push @{$table->{rows}} => [$title, $hval, $val, $TOTAL_DESC{$field}]; |
| 170 |
} |
| 171 |
|
| 172 |
return $table; |
| 173 |
} |
| 174 |
|
| 175 |
sub job_fields { |
| 176 |
my $self = shift; |
| 177 |
my $totals = $self->totals; |
| 178 |
|
| 179 |
my @out; |
| 180 |
|
| 181 |
for my $field (@TOTAL_FIELDS) { |
| 182 |
my $val = $totals->{$field} // next; |
| 183 |
my $hval = $totals->{"h_$field"}; |
| 184 |
|
| 185 |
my $data = {}; |
| 186 |
my $sources = $TOTAL_SOURCES{$field}; |
| 187 |
for my $source (@$sources) { |
| 188 |
$data->{$source} = { |
| 189 |
stamp => $self->{$source}, |
| 190 |
event_id => $self->{"${source}_id"}, |
| 191 |
}; |
| 192 |
} |
| 193 |
|
| 194 |
push @out => {name => "time_$field", details => $hval, raw => $val, data => $data}; |
| 195 |
} |
| 196 |
|
| 197 |
return @out; |
| 198 |
} |
| 199 |
|
| 200 |
1; |
| 201 |
|
| 202 |
__END__ |
| 203 |
|
| 204 |
=pod |
| 205 |
|
| 206 |
=encoding UTF-8 |
| 207 |
|
| 208 |
=head1 NAME |
| 209 |
|
| 210 |
Test2::Harness::Auditor::TimeTracker - Module that tracks timing data while an |
| 211 |
event stream is processed. |
| 212 |
|
| 213 |
=head1 DESCRIPTION |
| 214 |
|
| 215 |
The timetracker module tracks timing data of an event stream. All events for a |
| 216 |
given job should be run through a timetracker, which can then give data on how |
| 217 |
long the test took in each of several stages. |
| 218 |
|
| 219 |
=over 4 |
| 220 |
|
| 221 |
=item startup - Time from launch to first test event. |
| 222 |
|
| 223 |
=item events - Time spent generating test events. |
| 224 |
|
| 225 |
=item cleanup - Time from last test event to test exit. |
| 226 |
|
| 227 |
=item total - Total time. |
| 228 |
|
| 229 |
=back |
| 230 |
|
| 231 |
=head1 SYNOPSIS |
| 232 |
|
| 233 |
use Test2::Harness::Auditor::TimeTracker; |
| 234 |
|
| 235 |
my $tracker = Test2::Harness::Auditor::TimeTracker->new(); |
| 236 |
|
| 237 |
my $assert_count = 0; |
| 238 |
for my $event (@events) { |
| 239 |
my $facet_data = $events->facet_data; |
| 240 |
$assert_count++ if $facet_data->{assert}; |
| 241 |
$tracker->process($event, $facet_data, $assert_count); |
| 242 |
} |
| 243 |
|
| 244 |
print $tracker->summary; |
| 245 |
# Startup: 0.00708s | Events: 0.00000s | Cleanup: 0.10390s | Total: 0.11098s |
| 246 |
|
| 247 |
=head1 METHODS |
| 248 |
|
| 249 |
=over 4 |
| 250 |
|
| 251 |
=item $tracker->process($event, $facet_data, $assert_count) |
| 252 |
|
| 253 |
=item $tracker->process($event, undef, $assert_count) |
| 254 |
|
| 255 |
TimeTracker builds its state from multiple events, each event should be |
| 256 |
processed by this method. |
| 257 |
|
| 258 |
The second argument is optional, if no facet_data is provided it will pull the |
| 259 |
facet_data from the event itself. This is mainly a micro-optimization to avoid |
| 260 |
calling the C<facet_data()> method on the event multiple times if you have |
| 261 |
already called it. |
| 262 |
|
| 263 |
=item $bool = $tracker->useful() |
| 264 |
|
| 265 |
Returns true if there is any useful data to display. |
| 266 |
|
| 267 |
=item $totals = $tracker->totals() |
| 268 |
|
| 269 |
Returns the totals like this: |
| 270 |
|
| 271 |
{ |
| 272 |
# Raw numbers |
| 273 |
startup => ..., |
| 274 |
events => ..., |
| 275 |
cleanup => ..., |
| 276 |
total => ..., |
| 277 |
|
| 278 |
# Human friendly versions |
| 279 |
h_startup => ..., |
| 280 |
h_events => ..., |
| 281 |
h_cleanup => ..., |
| 282 |
h_total => ..., |
| 283 |
} |
| 284 |
|
| 285 |
=item $source = $tracker->source() |
| 286 |
|
| 287 |
This method returns the data from which the totals are derived. |
| 288 |
|
| 289 |
{ |
| 290 |
start => ..., # timestamp of the job starting |
| 291 |
stop => ..., # timestamp of the job ending |
| 292 |
first => ..., # timestamp of the first non-harness event |
| 293 |
last => ..., # timestamp of the last non-harness event |
| 294 |
|
| 295 |
# These are event_id's of the events that provided the above stamps. |
| 296 |
start_id => ..., |
| 297 |
stop_id => ..., |
| 298 |
first_id => ..., |
| 299 |
last_id => ..., |
| 300 |
complete_id => ..., |
| 301 |
} |
| 302 |
|
| 303 |
=item $data = $tracker->data_dump |
| 304 |
|
| 305 |
This dumps the totals and source data: |
| 306 |
|
| 307 |
{ |
| 308 |
totals => $tracker->totals, |
| 309 |
source => $tracker->source, |
| 310 |
} |
| 311 |
|
| 312 |
=item $string = $tracker->summary |
| 313 |
|
| 314 |
This produces a summary string of the totals data: |
| 315 |
|
| 316 |
Startup: 0.00708s | Events: 0.00000s | Cleanup: 0.10390s | Total: 0.11098s |
| 317 |
|
| 318 |
Fields that have no data will be omitted from the string. |
| 319 |
|
| 320 |
=item $table = $tracker->table |
| 321 |
|
| 322 |
Returns this structure that is good for use in L<Term::Table>. |
| 323 |
|
| 324 |
{ |
| 325 |
header => ["Phase", "Time", "Raw", "Explanation"], |
| 326 |
rows => [ |
| 327 |
['startup', $human_readible, $raw, "Time from launch to first test event."], |
| 328 |
['events', $human_radible, $raw, 'Time spent generating test events.'], |
| 329 |
['cleanup', $human_radible, $raw, 'Time from last test event to test exit.'], |
| 330 |
['total', $human_radible, $raw, 'Total time.'], |
| 331 |
], |
| 332 |
} |
| 333 |
|
| 334 |
=item @items = $tracker->job_fields() |
| 335 |
|
| 336 |
This is used to obtain extra data to attach to the job completion event. |
| 337 |
|
| 338 |
=back |
| 339 |
|
| 340 |
=head1 SOURCE |
| 341 |
|
| 342 |
The source code repository for Test2-Harness can be found at |
| 343 |
F<http://github.com/Test-More/Test2-Harness/>. |
| 344 |
|
| 345 |
=head1 MAINTAINERS |
| 346 |
|
| 347 |
=over 4 |
| 348 |
|
| 349 |
=item Chad Granum E<lt>[email protected]<gt> |
| 350 |
|
| 351 |
=back |
| 352 |
|
| 353 |
=head1 AUTHORS |
| 354 |
|
| 355 |
=over 4 |
| 356 |
|
| 357 |
=item Chad Granum E<lt>[email protected]<gt> |
| 358 |
|
| 359 |
=back |
| 360 |
|
| 361 |
=head1 COPYRIGHT |
| 362 |
|
| 363 |
Copyright 2020 Chad Granum E<lt>[email protected]<gt>. |
| 364 |
|
| 365 |
This program is free software; you can redistribute it and/or |
| 366 |
modify it under the same terms as Perl itself. |
| 367 |
|
| 368 |
See F<http://dev.perl.org/licenses/> |
| 369 |
|
| 370 |
=cut |