blib/lib/Time/StopWatchWithMessage.pm

filter: Statementstmt 25.0% 20 / 80 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 37.5% 6 / 16 Podpod 85.7% 6 / 7 totaltotal 24.6% 32 / 130 CC 11 SCAR 19.8
CC
11
Cov
20%
_output
20.0
start
12.0
stop
6.0
CRAP 7.2
↑ Summary
1 package Time::StopWatchWithMessage;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use List::Util qw( sum max reduce );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use List::MoreUtils qw( first_index );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Time::HiRes qw( gettimeofday tv_interval );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = "0.06";
9 3 our $IS_REALTIME = 0;
10 3 our $LENGTH = 3;
11  
12 1 ▶sub new { bless [ ], shift }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
13  
14 0 ▶sub start {
Subroutine start: not called CC=3 SCAR=24.8
Pod documented
15 0 my $self = shift;
16 0 ▶ my $message = shift || __PACKAGE__ . ">>> Start watching.";
Condition0%0/2
shift() || 'Time::StopWatchWithMessage>>> Start watching.'
l!l
00
MC/DC0%0/1
shift() || 'Time::StopWatchWithMessage>>> Start watching.'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
17  
18 0 ▶ $self->stop
Branch: 1 branch
BranchTrueFalseTotal
if $self->_does_stop_need 0 0 0
19 if $self->_does_stop_need;
20  
21 0 push @{ $self }, { time => [ gettimeofday ], message => $message };
22  
23 0 return $self;
24 }
25  
26 0 ▶sub stop {
Subroutine stop: not called CC=2 SCAR=17.9
Pod documented
27 0 my $self = shift;
28 0 my $time = [ gettimeofday ];
29  
30 0 my $previous = pop @{ $self };
31 0 $previous->{time} = tv_interval( $previous->{time}, $time );
32  
33 0 push @{ $self }, $previous;
34  
35 0 ▶ if ( $IS_REALTIME ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Time::StopWatchWithMessage::IS_REALTIME) 0 0 0
36 0 warn sprintf "%s - %.${LENGTH}f[s]\n", $previous->{message}, $previous->{time};
37 }
38  
39 0 return $self;
40 }
41  
42 0 ▶sub _does_stop_need {
Subroutine _does_stop_need: not called CC=2 SCAR=17.9
43 0 my $self = shift;
44 0 ▶ return @{ $self } && ref $self->[-1]{time} eq ref [ ];
Condition0%0/3
@{$self;} && ref $self->[-1]{'time'} eq ref []
!ll&&!rl&&r
000
MC/DC0%0/2
@{$self;} && ref $self->[-1]{'time'} eq ref []
@{$self;} ref $self->[-1]{'time'} eq ref []
Truth table0%0/3
A && B
A= @{$self;}B= ref $self->[-1]{'time'} eq ref []
ABresult
0X0
100
111
45 }
46  
47 0 ▶sub collapse {
Subroutine collapse: not called CC=2 SCAR=17.9
Pod documented
48 0 my $self = shift;
49  
50 0 ▶ $self->stop
Branch: 1 branch
BranchTrueFalseTotal
if $self->_does_stop_need 0 0 0
51 if $self->_does_stop_need;
52  
53 0 ▶ my $watches_ref = reduce {
Subroutine __ANON__: not called CC=2 SCAR=17.9
54 0 ▶ my $i = first_index { $_->{message} eq $b->{message} } @{ $a };
Subroutine __ANON__: not called CC=1 SCAR=6.9
55  
56 0 ▶ if ( $i >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) { } 0 0 0
57 0 $a->[ $i ]{time} += $b->{time};
58 0 $a->[ $i ]{count}++;
59 }
60 else {
61 0 push @{ $a }, $b;
62 }
63  
64 0 $a;
65 0 } ( [ ], @{ $self } );
66  
67 0 return bless $watches_ref, ref $self;
68 }
69  
70 0 ▶sub _output {
Subroutine _output: not called CC=4 SCAR=30.0
71 0 my $self = shift;
72 0 my $FH = shift;
73  
74 0 ▶ $self->stop
Branch: 1 branch
BranchTrueFalseTotal
if $self->_does_stop_need 0 0 0
75 if $self->_does_stop_need;
76  
77 0 my $sum = sum( map { $_->{time} } @{ $self } );
78 0 my $max = max( map { $_->{time} } @{ $self } );
79 my %length = (
80 0 time => max( map { length int $_->{time} } @{ $self } ),
81 0 message => max( map { length $_->{message} } @{ $self } ),
82 );
83  
84 OUTPUT_ALL_WATCHES:
85 0 ▶ while ( defined( my $watch_ref = shift @{ $self } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $watch_ref = shift @{$self;})) 0 0 0
86 my $output = sprintf(
87 "%$length{message}s - %$length{time}.${LENGTH}f[s] / %$length{time}.${LENGTH}f[s] = %$length{time}.${LENGTH}f[%%]",
88 $watch_ref->{message},
89 $watch_ref->{time},
90 $sum,
91 0 $watch_ref->{time} / $sum * 100,
92 );
93  
94 0 ▶ if ( $watch_ref->{count} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($watch_ref->{"count"}) 0 0 0
95 0 $output = join q{; }, $output, sprintf "%d times measured.", $watch_ref->{count} + 1;
96 }
97  
98 0 print { $FH } $output, "\n";
99 }
100  
101 0 return;
102 }
103  
104 0 ▶sub output {
Subroutine output: not called CC=2 SCAR=17.9
Pod documented
105 0 my $self = shift;
106 0 ▶ my $FH = shift || *STDERR;
Condition0%0/3
shift() || *STDERR
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || *STDERR
shift() *STDERR
Truth table0%0/3
A || B
A= shift()B= *STDERR
ABresult
1X1
011
000
107  
108 0 return $self->_output( $FH );
109 }
110  
111 0 ▶sub print { shift->_output( *STDOUT ) }
Subroutine print: not called CC=1 SCAR=6.9
Pod documented
112  
113 0 ▶sub warn { shift->_output( *STDERR ) }
Subroutine warn: not called CC=1 SCAR=6.9
Pod documented
114  
115 3 1;
116  
117 __END__
↑ Summary