Debian Codegraph @

lib/App/Yath/Command/speedtag.pm

raw
1 package App::Yath::Command::speedtag;
2 use strict;
3 use warnings;
4
5 our $VERSION = '1.000179';
6
7 use Test2::Harness::Util::File::JSONL;
8
9 use App::Yath::Options;
10
11 use Cwd qw/getcwd/;
12
13 use parent 'App::Yath::Command';
14 use Test2::Harness::Util::HashBase qw/-log_file -max_short -max_medium/;
15 use Test2::Harness::Util qw/clean_path/;
16
17 include_options(
18 'App::Yath::Options::Debug',
19 );
20
21 option_group {prefix => 'speedtag', category => 'speedtag options'} => sub {
22 option generate_durations_file => (
23 type => 'd',
24 alt => ['durations', 'duration'],
25 description => "Write out a duration json file, if no path is provided 'duration.json' will be used. The .json extension is added automatically if omitted.",
26
27 long_examples => ['', '=/path/to/durations.json'],
28
29 normalize => \&normalize_duration,
30 action => \&duration_action,
31 );
32
33 option pretty => (
34 description => "Generate a pretty 'durations.json' file when combined with --generate-durations-file. (sorted and multilines)",
35 default => 0,
36 );
37 };
38
39 sub group { 'log' }
40
41 sub summary { "Tag tests with duration (short medium long) using a source log" }
42
43 sub cli_args { "[--] event_log.jsonl[.gz|.bz2] max_short_duration_seconds max_medium_duration_seconds" }
44
45 sub description {
46 return <<" EOT";
47 This command will read the test durations from a log and tag/retag all tests
48 from the log based on the max durations for each type.
49 EOT
50 }
51
52 sub init {
53 my $self = shift;
54
55 $self->{+MAX_SHORT} //= 15;
56 $self->{+MAX_MEDIUM} //= 30;
57 }
58
59 sub normalize_duration {
60 my $val = shift;
61
62 return $val if $val eq '1';
63
64 $val =~ s/\.json$//g;
65 $val .= '.json';
66
67 return clean_path($val);
68 }
69
70 sub duration_action {
71 my ($prefix, $field, $raw, $norm, $slot, $settings) = @_;
72
73 return $$slot = clean_path($norm)
74 unless $norm eq '1';
75
76 return if $$slot;
77 return $$slot = clean_path('durations.json');
78 }
79
80 sub run {
81 my $self = shift;
82
83 my $settings = $self->settings;
84 my $args = $self->args;
85
86 shift @$args if @$args && $args->[0] eq '--';
87
88 my $initial_dir = clean_path(getcwd());
89
90 $self->{+LOG_FILE} = shift @$args or die "You must specify a log file";
91 die "'$self->{+LOG_FILE}' is not a valid log file" unless -f $self->{+LOG_FILE};
92 die "'$self->{+LOG_FILE}' does not look like a log file" unless $self->{+LOG_FILE} =~ m/\.jsonl(\.(gz|bz2))?$/;
93
94 $self->{+MAX_SHORT} = shift @$args if @$args;
95 $self->{+MAX_MEDIUM} = shift @$args if @$args;
96
97 die "max short duration must be an integer, got '$self->{+MAX_SHORT}'" unless $self->{+MAX_SHORT} && $self->{+MAX_SHORT} =~ m/^\d+$/;
98 die "max short duration must be an integer, got '$self->{+MAX_MEDIUM}'" unless $self->{+MAX_MEDIUM} && $self->{+MAX_MEDIUM} =~ m/^\d+$/;
99
100 my $stream = Test2::Harness::Util::File::JSONL->new(name => $self->{+LOG_FILE});
101
102 my $durations_file = $self->settings->speedtag->generate_durations_file;
103 my %durations;
104
105 while(1) {
106 my @events = $stream->poll(max => 1000) or last;
107
108 for my $event (@events) {
109 my $stamp = $event->{stamp} or next;
110 my $job_id = $event->{job_id} or next;
111 my $f = $event->{facet_data} or next;
112
113 next unless $f->{harness_job_end};
114
115 my $job = {};
116 $job->{file} = clean_path( $f->{harness_job_end}->{file} ) if $f->{harness_job_end} && $f->{harness_job_end}->{file};
117 $job->{time} = $f->{harness_job_end}->{times}->{totals}->{total} if $f->{harness_job_end} && $f->{harness_job_end}->{times};
118
119 next unless $job->{file} && $job->{time};
120
121 my $dur;
122 if ($job->{time} < $self->{+MAX_SHORT}) {
123 $dur = 'short';
124 }
125 elsif ($job->{time} < $self->{+MAX_MEDIUM}) {
126 $dur = 'medium';
127 }
128 else {
129 $dur = 'long';
130 }
131
132 my $fh;
133 unless (open($fh, '<', $job->{file})) {
134 warn "Could not open file $job->{file} for reading\n";
135 next;
136 }
137
138 my @lines;
139 my $injected;
140 for my $line (<$fh>) {
141 if ($line =~ m/^(\s*)#(\s*)HARNESS-(CAT(EGORY)?|DUR(ATION))-(LONG|MEDIUM|SHORT)$/i) {
142 next if $injected++;
143 $line = "${1}#${2}HARNESS-DURATION-" . uc($dur) . "\n";
144 }
145 push @lines => $line;
146 }
147 unless ($injected) {
148 my $new_line = "# HARNESS-DURATION-" . uc($dur) . "\n";
149 my @header;
150 while (@lines && $lines[0] =~ m/^(#|use\s|package\s)/) {
151 push @header => shift @lines;
152 }
153
154 unshift @lines => (@header, $new_line);
155 }
156
157 close($fh);
158 unless (open($fh, '>', $job->{file})) {
159 warn "Could not open file $job->{file} for writing\n";
160 next;
161 }
162
163 print $fh @lines;
164 close($fh);
165
166 if ( $durations_file ) {
167 my $tfile = $job->{file};
168 $tfile =~ s{^\Q$initial_dir\E/+}{};
169 $durations{ $tfile } = uc( $dur );
170 }
171
172 print "Tagged '$dur': $job->{file}\n";
173 }
174 }
175
176 if ( $durations_file ) {
177 my $jfile = Test2::Harness::Util::File::JSON->new(name => $durations_file, pretty => $self->settings->speedtag->pretty );
178 $jfile->write( \%durations );
179 }
180
181 return 0;
182 }
183
184 1;
185
186 __END__
187
188 =pod
189
190 =encoding UTF-8
191
192 =head1 NAME
193
194 App::Yath::Command::speedtag - Tag tests with duration (short medium long) using a source log
195
196 =head1 DESCRIPTION
197
198 This command will read the test durations from a log and tag/retag all tests
199 from the log based on the max durations for each type.
200
201
202 =head1 USAGE
203
204 $ yath [YATH OPTIONS] speedtag [COMMAND OPTIONS]
205
206 =head2 YATH OPTIONS
207
208 =head3 Developer
209
210 =over 4
211
212 =item --dev-lib
213
214 =item --dev-lib=lib
215
216 =item -D
217
218 =item -D=lib
219
220 =item -Dlib
221
222 =item --no-dev-lib
223
224 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.
225
226 Can be specified multiple times
227
228
229 =back
230
231 =head3 Environment
232
233 =over 4
234
235 =item --persist-dir ARG
236
237 =item --persist-dir=ARG
238
239 =item --no-persist-dir
240
241 Where to find persistence files.
242
243
244 =item --persist-file ARG
245
246 =item --persist-file=ARG
247
248 =item --pfile ARG
249
250 =item --pfile=ARG
251
252 =item --no-persist-file
253
254 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.
255
256
257 =item --project ARG
258
259 =item --project=ARG
260
261 =item --project-name ARG
262
263 =item --project-name=ARG
264
265 =item --no-project
266
267 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.
268
269
270 =back
271
272 =head3 Help and Debugging
273
274 =over 4
275
276 =item --show-opts
277
278 =item --no-show-opts
279
280 Exit after showing what yath thinks your options mean
281
282
283 =item --version
284
285 =item -V
286
287 =item --no-version
288
289 Exit after showing a helpful usage message
290
291
292 =back
293
294 =head3 Plugins
295
296 =over 4
297
298 =item --no-scan-plugins
299
300 =item --no-no-scan-plugins
301
302 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.
303
304
305 =item --plugins PLUGIN
306
307 =item --plugins +App::Yath::Plugin::PLUGIN
308
309 =item --plugins PLUGIN=arg1,arg2,...
310
311 =item --plugin PLUGIN
312
313 =item --plugin +App::Yath::Plugin::PLUGIN
314
315 =item --plugin PLUGIN=arg1,arg2,...
316
317 =item -pPLUGIN
318
319 =item --no-plugins
320
321 Load a yath plugin.
322
323 Can be specified multiple times
324
325
326 =back
327
328 =head2 COMMAND OPTIONS
329
330 =head3 Cover Options
331
332 =over 4
333
334 =item --cover-aggregator ByTest
335
336 =item --cover-aggregator ByRun
337
338 =item --cover-aggregator +Custom::Aggregator
339
340 =item --cover-agg ByTest
341
342 =item --cover-agg ByRun
343
344 =item --cover-agg +Custom::Aggregator
345
346 =item --no-cover-aggregator
347
348 Choose a custom aggregator subclass
349
350
351 =item --cover-class ARG
352
353 =item --cover-class=ARG
354
355 =item --no-cover-class
356
357 Choose a Test2::Plugin::Cover subclass
358
359
360 =item --cover-dirs ARG
361
362 =item --cover-dirs=ARG
363
364 =item --cover-dir ARG
365
366 =item --cover-dir=ARG
367
368 =item --no-cover-dirs
369
370 NO DESCRIPTION - FIX ME
371
372 Can be specified multiple times
373
374
375 =item --cover-exclude-dirs ARG
376
377 =item --cover-exclude-dirs=ARG
378
379 =item --cover-exclude-dir ARG
380
381 =item --cover-exclude-dir=ARG
382
383 =item --no-cover-exclude-dirs
384
385 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.
386
387 Can be specified multiple times
388
389
390 =item --cover-exclude-private
391
392 =item --no-cover-exclude-private
393
394
395
396
397 =item --cover-files
398
399 =item --no-cover-files
400
401 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)
402
403
404 =item --cover-from path/to/log.jsonl
405
406 =item --cover-from http://example.com/coverage
407
408 =item --cover-from path/to/coverage.jsonl
409
410 =item --no-cover-from
411
412 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.
413
414
415 =item --cover-from-type json
416
417 =item --cover-from-type jsonl
418
419 =item --cover-from-type log
420
421 =item --no-cover-from-type
422
423 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.
424
425
426 =item --cover-manager My::Coverage::Manager
427
428 =item --no-cover-manager
429
430 Coverage 'from' manager to use when coverage data does not provide one
431
432
433 =item --cover-maybe-from path/to/log.jsonl
434
435 =item --cover-maybe-from http://example.com/coverage
436
437 =item --cover-maybe-from path/to/coverage.jsonl
438
439 =item --no-cover-maybe-from
440
441 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.
442
443
444 =item --cover-maybe-from-type json
445
446 =item --cover-maybe-from-type jsonl
447
448 =item --cover-maybe-from-type log
449
450 =item --no-cover-maybe-from-type
451
452 Same as "from_type" but for "maybe_from". Defaults to "from_type" if that is specified, otherwise auto-detect
453
454
455 =item --cover-metrics
456
457 =item --no-cover-metrics
458
459
460
461
462 =item --cover-types ARG
463
464 =item --cover-types=ARG
465
466 =item --cover-type ARG
467
468 =item --cover-type=ARG
469
470 =item --no-cover-types
471
472 NO DESCRIPTION - FIX ME
473
474 Can be specified multiple times
475
476
477 =item --cover-write
478
479 =item --cover-write=coverage.jsonl
480
481 =item --cover-write=coverage.json
482
483 =item --no-cover-write
484
485 Create a json or jsonl file of all coverage data seen during the run (This implies --cover-files).
486
487
488 =back
489
490 =head3 Git Options
491
492 =over 4
493
494 =item --git-change-base master
495
496 =item --git-change-base HEAD^
497
498 =item --git-change-base df22abe4
499
500 =item --no-git-change-base
501
502 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.
503
504
505 =back
506
507 =head3 Help and Debugging
508
509 =over 4
510
511 =item --dummy
512
513 =item -d
514
515 =item --no-dummy
516
517 Dummy run, do not actually execute anything
518
519 Can also be set with the following environment variables: C<T2_HARNESS_DUMMY>
520
521
522 =item --help
523
524 =item -h
525
526 =item --no-help
527
528 exit after showing help information
529
530
531 =item --interactive
532
533 =item -i
534
535 =item --no-interactive
536
537 Use interactive mode, 1 test at a time, stdin forwarded to it
538
539
540 =item --keep-dirs
541
542 =item --keep_dir
543
544 =item -k
545
546 =item --no-keep-dirs
547
548 Do not delete directories when done. This is useful if you want to inspect the directories used for various commands.
549
550
551 =item --procname-prefix ARG
552
553 =item --procname-prefix=ARG
554
555 =item --no-procname-prefix
556
557 Add a prefix to all proc names (as seen by ps).
558
559
560 =back
561
562 =head3 YathUI Options
563
564 =over 4
565
566 =item --yathui-api-key ARG
567
568 =item --yathui-api-key=ARG
569
570 =item --no-yathui-api-key
571
572 Yath-UI API key. This is not necessary if your Yath-UI instance is set to single-user
573
574
575 =item --yathui-grace
576
577 =item --no-yathui-grace
578
579 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.
580
581
582 =item --yathui-long-duration 10
583
584 =item --no-yathui-long-duration
585
586 Minimum duration length (seconds) before a test goes from MEDIUM to LONG
587
588
589 =item --yathui-medium-duration 5
590
591 =item --no-yathui-medium-duration
592
593 Minimum duration length (seconds) before a test goes from SHORT to MEDIUM
594
595
596 =item --yathui-mode summary
597
598 =item --yathui-mode qvf
599
600 =item --yathui-mode qvfd
601
602 =item --yathui-mode complete
603
604 =item --no-yathui-mode
605
606 Set the upload mode (default 'qvfd')
607
608
609 =item --yathui-project ARG
610
611 =item --yathui-project=ARG
612
613 =item --no-yathui-project
614
615 The Yath-UI project for your test results
616
617
618 =item --yathui-retry
619
620 =item --no-yathui-retry
621
622 How many times to try an operation before giving up
623
624 Can be specified multiple times
625
626
627 =item --yathui-url http://my-yath-ui.com/...
628
629 =item --uri http://my-yath-ui.com/...
630
631 =item --no-yathui-url
632
633 Yath-UI url
634
635
636 =back
637
638 =head3 speedtag options
639
640 =over 4
641
642 =item --generate-durations-file
643
644 =item --generate-durations-file=/path/to/durations.json
645
646 =item --durations
647
648 =item --durations=/path/to/durations.json
649
650 =item --duration
651
652 =item --duration=/path/to/durations.json
653
654 =item --no-generate-durations-file
655
656 Write out a duration json file, if no path is provided 'duration.json' will be used. The .json extension is added automatically if omitted.
657
658
659 =item --pretty
660
661 =item --no-pretty
662
663 Generate a pretty 'durations.json' file when combined with --generate-durations-file. (sorted and multilines)
664
665
666 =back
667
668 =head1 SOURCE
669
670 The source code repository for Test2-Harness can be found at
671 F<http://github.com/Test-More/Test2-Harness/>.
672
673 =head1 MAINTAINERS
674
675 =over 4
676
677 =item Chad Granum E<lt>[email protected]<gt>
678
679 =back
680
681 =head1 AUTHORS
682
683 =over 4
684
685 =item Chad Granum E<lt>[email protected]<gt>
686
687 =back
688
689 =head1 COPYRIGHT
690
691 Copyright 2026 Chad Granum E<lt>[email protected]<gt>.
692
693 This program is free software; you can redistribute it and/or
694 modify it under the same terms as Perl itself.
695
696 See F<http://dev.perl.org/licenses/>
697
698 =cut
699