Debian Codegraph @

lib/App/Yath/Command/times.pm

raw
1 package App::Yath::Command::times;
2 use strict;
3 use warnings;
4
5 our $VERSION = '1.000179';
6
7 use Test2::Util::Times qw/render_duration/;
8
9 use Test2::Harness::Util::File::JSONL;
10
11 use App::Yath::Options;
12
13 use parent 'App::Yath::Command';
14 use Test2::Harness::Util::HashBase qw/-log_file <fields/;
15
16 include_options(
17 'App::Yath::Options::Debug',
18 );
19
20 sub summary { "Get times from a test log" }
21
22 sub group { 'log' }
23
24 sub cli_args { "[--] event_log.jsonl[.gz|.bz2] [Field1] [Field2]" }
25
26 sub description {
27 return <<" EOT";
28 This command will consume the log of a previous run, and output all timing data
29 from shortest test to longest. You can specify a sort order by listing fields
30 in your desired order after the log file on the command line.
31 EOT
32 }
33
34 my @NUMERIC = qw/total startup events cleanup/;
35 my %NUMERIC = map { $_ => 1 } @NUMERIC;
36
37 my @ALPHA = qw/file/;
38 my %ALPHA = map { $_ => 1 } @ALPHA;
39
40 my @FIELDS = (@NUMERIC, @ALPHA);
41 my %FIELDS = map { $_ => 1 } @FIELDS;
42
43 sub run {
44 my $self = shift;
45
46 my $args = $self->args;
47
48 shift @$args if @$args && $args->[0] eq '--';
49
50 $self->{+LOG_FILE} = shift @$args or die "You must specify a log file";
51 die "'$self->{+LOG_FILE}' is not a valid log file" unless -f $self->{+LOG_FILE};
52 die "'$self->{+LOG_FILE}' does not look like a log file" unless $self->{+LOG_FILE} =~ m/\.jsonl(\.(gz|bz2))?$/;
53
54 my %seen;
55 my @fields;
56 for my $field (@$args, @FIELDS) {
57 $field = lc($field);
58 next if $seen{$field}++;
59 die "'$field' is not a valid field\n" unless $FIELDS{$field};
60 push @fields => $field;
61 }
62
63 $self->{+FIELDS} = \@fields;
64
65 my $stream = Test2::Harness::Util::File::JSONL->new(name => $self->{+LOG_FILE});
66
67 my @jobs;
68 while (1) {
69 my @events = $stream->poll(max => 1000) or last;
70
71 for my $event (@events) {
72 my $stamp = $event->{stamp} or next;
73 my $job_id = $event->{job_id} or next;
74 my $f = $event->{facet_data} or next;
75
76 next unless $f->{harness_job_end};
77
78 my $job = {};
79 $job->{file} = $f->{harness_job_end}->{rel_file} if $f->{harness_job_end} && $f->{harness_job_end}->{rel_file};
80 $job->{time} = $f->{harness_job_end}->{times}->{totals} if $f->{harness_job_end} && $f->{harness_job_end}->{times};
81
82 push @jobs => $job;
83 }
84 }
85
86 my @rows;
87 my $totals = {file => 'TOTAL'};
88
89 @jobs = sort { $self->sort_compare($a, $b) } @jobs;
90
91 for my $job (@jobs) {
92 my $data = $job->{time};
93 push @rows => $self->build_row({%$data, file => $job->{file}});
94 $totals->{$_} += $data->{$_} for @NUMERIC;
95 }
96
97 push @rows => [map { '--' } @fields];
98 push @rows => $self->build_row($totals);
99
100 require Term::Table;
101 my $table = Term::Table->new(
102 header => [map { ucfirst($_) } @fields],
103 rows => \@rows,
104 );
105
106 print "$_\n" for $table->render;
107
108 return 0;
109 }
110
111 sub build_row {
112 my $self = shift;
113 my ($data) = @_;
114
115 return [map { $NUMERIC{$_} && defined($data->{$_}) ? render_duration($data->{$_}) : $data->{$_} } @{$self->{+FIELDS}}];
116 }
117
118 sub sort_compare {
119 my $self = shift;
120 my ($ja, $jb) = @_;
121
122 my $order = $self->{+FIELDS};
123
124 my $ta = $ja->{time};
125 my $tb = $jb->{time};
126
127 for my $field (@$order) {
128 my $fa = $ta->{$field};
129 my $fb = $tb->{$field};
130
131 my $da = defined $fa;
132 my $db = defined $fb;
133
134 next unless $da || $db;
135 return 1 if $da && !$db;
136 return -1 if $db && !$da;
137
138 my $delta = $ALPHA{$field} ? lc($fa) cmp lc($fb) : $fa <=> $fb;
139 return $delta if $delta;
140 }
141
142 return 0;
143 }
144
145 1;
146
147 __END__
148
149 =pod
150
151 =encoding UTF-8
152
153 =head1 NAME
154
155 App::Yath::Command::times - Get times from a test log
156
157 =head1 DESCRIPTION
158
159 This command will consume the log of a previous run, and output all timing data
160 from shortest test to longest. You can specify a sort order by listing fields
161 in your desired order after the log file on the command line.
162
163
164 =head1 USAGE
165
166 $ yath [YATH OPTIONS] times [COMMAND OPTIONS]
167
168 =head2 YATH OPTIONS
169
170 =head3 Developer
171
172 =over 4
173
174 =item --dev-lib
175
176 =item --dev-lib=lib
177
178 =item -D
179
180 =item -D=lib
181
182 =item -Dlib
183
184 =item --no-dev-lib
185
186 Add paths to @INC before loading ANYTHING. This is what you use if you are developing yath or yath plugins to make sure the yath script finds the local code instead of the installed versions of the same code. You can provide an argument (-Dfoo) to provide a custom path, or you can just use -D without and arg to add lib, blib/lib and blib/arch.
187
188 Can be specified multiple times
189
190
191 =back
192
193 =head3 Environment
194
195 =over 4
196
197 =item --persist-dir ARG
198
199 =item --persist-dir=ARG
200
201 =item --no-persist-dir
202
203 Where to find persistence files.
204
205
206 =item --persist-file ARG
207
208 =item --persist-file=ARG
209
210 =item --pfile ARG
211
212 =item --pfile=ARG
213
214 =item --no-persist-file
215
216 Where to find the persistence file. The default is /{system-tempdir}/project-yath-persist.json. If no project is specified then it will fall back to the current directory. If the current directory is not writable it will default to /tmp/yath-persist.json which limits you to one persistent runner on your system.
217
218
219 =item --project ARG
220
221 =item --project=ARG
222
223 =item --project-name ARG
224
225 =item --project-name=ARG
226
227 =item --no-project
228
229 This lets you provide a label for your current project/codebase. This is best used in a .yath.rc file. This is necessary for a persistent runner.
230
231
232 =back
233
234 =head3 Help and Debugging
235
236 =over 4
237
238 =item --show-opts
239
240 =item --no-show-opts
241
242 Exit after showing what yath thinks your options mean
243
244
245 =item --version
246
247 =item -V
248
249 =item --no-version
250
251 Exit after showing a helpful usage message
252
253
254 =back
255
256 =head3 Plugins
257
258 =over 4
259
260 =item --no-scan-plugins
261
262 =item --no-no-scan-plugins
263
264 Normally yath scans for and loads all App::Yath::Plugin::* modules in order to bring in command-line options they may provide. This flag will disable that. This is useful if you have a naughty plugin that is loading other modules when it should not.
265
266
267 =item --plugins PLUGIN
268
269 =item --plugins +App::Yath::Plugin::PLUGIN
270
271 =item --plugins PLUGIN=arg1,arg2,...
272
273 =item --plugin PLUGIN
274
275 =item --plugin +App::Yath::Plugin::PLUGIN
276
277 =item --plugin PLUGIN=arg1,arg2,...
278
279 =item -pPLUGIN
280
281 =item --no-plugins
282
283 Load a yath plugin.
284
285 Can be specified multiple times
286
287
288 =back
289
290 =head2 COMMAND OPTIONS
291
292 =head3 Cover Options
293
294 =over 4
295
296 =item --cover-aggregator ByTest
297
298 =item --cover-aggregator ByRun
299
300 =item --cover-aggregator +Custom::Aggregator
301
302 =item --cover-agg ByTest
303
304 =item --cover-agg ByRun
305
306 =item --cover-agg +Custom::Aggregator
307
308 =item --no-cover-aggregator
309
310 Choose a custom aggregator subclass
311
312
313 =item --cover-class ARG
314
315 =item --cover-class=ARG
316
317 =item --no-cover-class
318
319 Choose a Test2::Plugin::Cover subclass
320
321
322 =item --cover-dirs ARG
323
324 =item --cover-dirs=ARG
325
326 =item --cover-dir ARG
327
328 =item --cover-dir=ARG
329
330 =item --no-cover-dirs
331
332 NO DESCRIPTION - FIX ME
333
334 Can be specified multiple times
335
336
337 =item --cover-exclude-dirs ARG
338
339 =item --cover-exclude-dirs=ARG
340
341 =item --cover-exclude-dir ARG
342
343 =item --cover-exclude-dir=ARG
344
345 =item --no-cover-exclude-dirs
346
347 Directory to leave out of file coverage and coverage metrics, along with everything under it at any depth. May be specified multiple times. Wildcards are expanded when the option is parsed, so a pattern matching nothing excludes nothing. Values are word-split first, so a path containing whitespace must be quoted or backslash-escaped. A resolved path may not contain a comma.
348
349 Can be specified multiple times
350
351
352 =item --cover-exclude-private
353
354 =item --no-cover-exclude-private
355
356
357
358
359 =item --cover-files
360
361 =item --no-cover-files
362
363 Use Test2::Plugin::Cover to collect coverage data for what files are touched by what tests. Unlike Devel::Cover this has very little performance impact (About 4% difference)
364
365
366 =item --cover-from path/to/log.jsonl
367
368 =item --cover-from http://example.com/coverage
369
370 =item --cover-from path/to/coverage.jsonl
371
372 =item --no-cover-from
373
374 This can be a test log, a coverage dump (old style json or new jsonl format), or a url to any of the previous. Tests will not be run if the file/url is invalid.
375
376
377 =item --cover-from-type json
378
379 =item --cover-from-type jsonl
380
381 =item --cover-from-type log
382
383 =item --no-cover-from-type
384
385 File type for coverage source. Usually it can be detected, but when it cannot be you should specify. "json" is old style single-blob coverage data, "jsonl" is the new by-test style, "log" is a logfile from a previous run.
386
387
388 =item --cover-manager My::Coverage::Manager
389
390 =item --no-cover-manager
391
392 Coverage 'from' manager to use when coverage data does not provide one
393
394
395 =item --cover-maybe-from path/to/log.jsonl
396
397 =item --cover-maybe-from http://example.com/coverage
398
399 =item --cover-maybe-from path/to/coverage.jsonl
400
401 =item --no-cover-maybe-from
402
403 This can be a test log, a coverage dump (old style json or new jsonl format), or a url to any of the previous. Tests will continue even if the coverage file/url is invalid.
404
405
406 =item --cover-maybe-from-type json
407
408 =item --cover-maybe-from-type jsonl
409
410 =item --cover-maybe-from-type log
411
412 =item --no-cover-maybe-from-type
413
414 Same as "from_type" but for "maybe_from". Defaults to "from_type" if that is specified, otherwise auto-detect
415
416
417 =item --cover-metrics
418
419 =item --no-cover-metrics
420
421
422
423
424 =item --cover-types ARG
425
426 =item --cover-types=ARG
427
428 =item --cover-type ARG
429
430 =item --cover-type=ARG
431
432 =item --no-cover-types
433
434 NO DESCRIPTION - FIX ME
435
436 Can be specified multiple times
437
438
439 =item --cover-write
440
441 =item --cover-write=coverage.jsonl
442
443 =item --cover-write=coverage.json
444
445 =item --no-cover-write
446
447 Create a json or jsonl file of all coverage data seen during the run (This implies --cover-files).
448
449
450 =back
451
452 =head3 Git Options
453
454 =over 4
455
456 =item --git-change-base master
457
458 =item --git-change-base HEAD^
459
460 =item --git-change-base df22abe4
461
462 =item --no-git-change-base
463
464 Find files changed by all commits in the current branch from most recent stopping when a commit is found that is also present in the history of the branch/commit specified as the change base.
465
466
467 =back
468
469 =head3 Help and Debugging
470
471 =over 4
472
473 =item --dummy
474
475 =item -d
476
477 =item --no-dummy
478
479 Dummy run, do not actually execute anything
480
481 Can also be set with the following environment variables: C<T2_HARNESS_DUMMY>
482
483
484 =item --help
485
486 =item -h
487
488 =item --no-help
489
490 exit after showing help information
491
492
493 =item --interactive
494
495 =item -i
496
497 =item --no-interactive
498
499 Use interactive mode, 1 test at a time, stdin forwarded to it
500
501
502 =item --keep-dirs
503
504 =item --keep_dir
505
506 =item -k
507
508 =item --no-keep-dirs
509
510 Do not delete directories when done. This is useful if you want to inspect the directories used for various commands.
511
512
513 =item --procname-prefix ARG
514
515 =item --procname-prefix=ARG
516
517 =item --no-procname-prefix
518
519 Add a prefix to all proc names (as seen by ps).
520
521
522 =back
523
524 =head3 YathUI Options
525
526 =over 4
527
528 =item --yathui-api-key ARG
529
530 =item --yathui-api-key=ARG
531
532 =item --no-yathui-api-key
533
534 Yath-UI API key. This is not necessary if your Yath-UI instance is set to single-user
535
536
537 =item --yathui-grace
538
539 =item --no-yathui-grace
540
541 If yath cannot connect to yath-ui it normally throws an error, use this to make it fail gracefully. You get a warning, but things keep going.
542
543
544 =item --yathui-long-duration 10
545
546 =item --no-yathui-long-duration
547
548 Minimum duration length (seconds) before a test goes from MEDIUM to LONG
549
550
551 =item --yathui-medium-duration 5
552
553 =item --no-yathui-medium-duration
554
555 Minimum duration length (seconds) before a test goes from SHORT to MEDIUM
556
557
558 =item --yathui-mode summary
559
560 =item --yathui-mode qvf
561
562 =item --yathui-mode qvfd
563
564 =item --yathui-mode complete
565
566 =item --no-yathui-mode
567
568 Set the upload mode (default 'qvfd')
569
570
571 =item --yathui-project ARG
572
573 =item --yathui-project=ARG
574
575 =item --no-yathui-project
576
577 The Yath-UI project for your test results
578
579
580 =item --yathui-retry
581
582 =item --no-yathui-retry
583
584 How many times to try an operation before giving up
585
586 Can be specified multiple times
587
588
589 =item --yathui-url http://my-yath-ui.com/...
590
591 =item --uri http://my-yath-ui.com/...
592
593 =item --no-yathui-url
594
595 Yath-UI url
596
597
598 =back
599
600 =head1 SOURCE
601
602 The source code repository for Test2-Harness can be found at
603 F<http://github.com/Test-More/Test2-Harness/>.
604
605 =head1 MAINTAINERS
606
607 =over 4
608
609 =item Chad Granum E<lt>[email protected]<gt>
610
611 =back
612
613 =head1 AUTHORS
614
615 =over 4
616
617 =item Chad Granum E<lt>[email protected]<gt>
618
619 =back
620
621 =head1 COPYRIGHT
622
623 Copyright 2026 Chad Granum E<lt>[email protected]<gt>.
624
625 This program is free software; you can redistribute it and/or
626 modify it under the same terms as Perl itself.
627
628 See F<http://dev.perl.org/licenses/>
629
630 =cut
631