| 1 | package Test2::Util::Times; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use List::Util qw/sum/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | our $VERSION = '1.302226'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | our @EXPORT_OK = qw/render_bench render_duration/; | |||||||||||||||||||||||||||
| 10 | 2 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 7 | ▶ | sub render_duration { | ||||||||||||||||||||||||||
|
Subroutine
render_duration: called CC=9 SCAR=22.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 13 | 7 | my $time; | |||||||||||||||||||||||||||
| 14 | 7 | ▶ | if (@_ == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | 0 | ($time) = @_; | |||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | else { | ||||||||||||||||||||||||||||
| 18 | 7 | my ($start, $end) = @_; | |||||||||||||||||||||||||||
| 19 | 7 | $time = $end - $start; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 7 | ▶ | return sprintf('%1.5fs', $time) if $time < 10; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 4 | ▶ | return sprintf('%2.4fs', $time) if $time < 60; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 3 | my $msec = substr(sprintf('%0.2f', $time - int($time)), -2, 2); | |||||||||||||||||||||||||||
| 26 | 3 | my $secs = $time % 60; | |||||||||||||||||||||||||||
| 27 | 3 | my $mins = int($time / 60) % 60; | |||||||||||||||||||||||||||
| 28 | 3 | my $hours = int($time / 60 / 60) % 24; | |||||||||||||||||||||||||||
| 29 | 3 | my $days = int($time / 60 / 60 / 24); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 3 | my @units = (qw/d h m/, ''); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 3 | my $duration = ''; | |||||||||||||||||||||||||||
| 34 | 3 | for my $t ($days, $hours, $mins, $secs) { | |||||||||||||||||||||||||||
| 35 | 12 | my $u = shift @units; | |||||||||||||||||||||||||||
| 36 | 12 | ▶ | next unless $t || $duration; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$t or $duration
MC/DC50%1/2
$t or $duration
$t
$duration
Truth table66%2/3
A or B
A= $tB= $duration
| |||||||||||||||||||||||||||||
| 37 | 9 | $duration = join ':' => grep { length($_) } $duration, sprintf('%02u%s', $t, $u); | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 3 | ▶ | $duration ||= '0'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$duration ||= 0
MC/DC0%0/1
$duration ||= 0
$duration
Truth table50%1/2
A
A= $duration
| |||||||||||||||||||||||||||||
| 41 | 3 | ▶ | $duration .= ".$msec" if int($msec); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 3 | $duration .= 's'; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 3 | return $duration; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 6 | ▶ | sub render_bench { | ||||||||||||||||||||||||||
|
Subroutine
render_bench: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 6 | my ($start, $end, $user, $system, $cuser, $csystem) = @_; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 6 | my $duration = render_duration($start, $end); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 6 | my $bench = sprintf( | |||||||||||||||||||||||||||
| 53 | "%s on wallclock (%5.2f usr %5.2f sys + %5.2f cusr %5.2f csys = %5.2f CPU)", | ||||||||||||||||||||||||||||
| 54 | $duration, $user, $system, $cuser, $csystem, sum($user, $system, $cuser, $csystem), | ||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||
| 56 | 6 | $bench =~ s/\s+/ /g; | |||||||||||||||||||||||||||
| 57 | 6 | $bench =~ s/(\(|\))\s+/$1/g; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 6 | return $bench; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 2 | 1; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =pod | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head1 NAME | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | Test2::Util::Times - Format timing/benchmark information. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | This modules exports tools for rendering timing data at the end of tests. | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head1 EXPORTS | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | All exports are optional. You must specify subs to import. | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =over 4 | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =item $str = render_bench($start, $end, $user, $system, $cuser, $csystem) | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =item $str = render_bench($start, time(), times()) | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | This will produce a string like one of these (Note these numbers are completely | ||||||||||||||||||||||||||||
| 87 | made up). I<Which string is used depends on the time elapsed.> | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0.12345s on wallclock (0.05 usr 0.00 sys + 0.00 cusr 0.00 csys = 0.05 CPU) | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 11.1234s on wallclock (0.05 usr 0.00 sys + 0.00 cusr 0.00 csys = 0.05 CPU) | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 01m:54.45s on wallclock (0.05 usr 0.00 sys + 0.00 cusr 0.00 csys = 0.05 CPU) | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 18h:22m:54.45s on wallclock (0.05 usr 0.00 sys + 0.00 cusr 0.00 csys = 0.05 CPU) | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 04d:18h:22m:54.45s on wallclock (0.05 usr 0.00 sys + 0.00 cusr 0.00 csys = 0.05 CPU) | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | The first 2 arguments are the C<$start> and C<$end> times in seconds (as | ||||||||||||||||||||||||||||
| 100 | returned by C<time()> or C<Time::HiRes::time()>). | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | The last 4 arguments are timing information as returned by the C<times()> | ||||||||||||||||||||||||||||
| 103 | function. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =back | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head1 SOURCE | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | The source code repository for Test2-Suite can be found at | ||||||||||||||||||||||||||||
| 110 | F<https://github.com/Test-More/test-more/>. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head1 MAINTAINERS | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =over 4 | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =item Chad Granum E<lt>exodist@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =back | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =over 4 | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =item Chad Granum E<lt>exodist@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =back | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | Copyright Chad Granum E<lt>exodist@cpan.orgE<gt>. | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | This program is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 134 | modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | See F<http://dev.perl.org/licenses/> | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =cut | ||||||||||||||||||||||||||||