Log/Timer.pm

filter: Statementstmt 100% 66 / 66 Branchbran 75.0% 6 / 8 Conditioncond 76.4% 13 / 17 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 90.6% 107 / 118 CC 14 SCAR 12.2
CC
14
Cov
93%
timer
7.0
subroutine_timer
4.2
__ANON__
3.0
CRAP 3.4
↑ Summary Guard/Timer.pm »
1 package Log::Timer;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 our $VERSION = '1.0.1'; # VERSION
7 # ABSTRACT: track nested timing information
8  
9  
10 1 our @EXPORT = our @EXPORT_OK = qw/ timer subroutine_timer /;
11  
12 1 ▶use Guard::Timer;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use List::AllUtils qw/ max /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Statistics::Descriptive;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Log::Any qw/ $log /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 1 my $name__context = {
19 "" => {
20 indent => 1,
21 },
22 };
23 1 my $message_stats = {};
24  
25  
26 5 ▶sub timer {
Subroutine timer: called CC=7 SCAR=19.5
Pod documented
27 5 my ($message, $options) = @_;
28 5 ▶ $options //= {};
Condition100%2/2
$options //= {}
l!l
23
MC/DC100%1/1
$options //= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
29  
30 5 ▶ my $log_prefix = $options->{prefix} // "";
Condition100%2/2
$options->{"prefix"} // ""
l!l
14
MC/DC100%1/1
$options->{"prefix"} // ""
$options->{"prefix"}
Truth table100%2/2
A
A= $options->{"prefix"}
Aresult
11
00
31  
32 # If you run async code, you need to distinguish the context this
33 # is running in. Human readable.
34 5 ▶ my $context_name = $options->{context} //= "";
Condition100%2/2
$options->{"context"} //= ""
l!l
14
MC/DC100%1/1
$options->{"context"} //= ""
$options->{"context"}
Truth table100%2/2
A
A= $options->{"context"}
Aresult
11
00
35  
36 # Create context at the same level we're at, assuming we're always
37 # branching off the default "process wide" context
38 my $context = $name__context->{ $context_name } //= {
39 start_indent => $name__context->{ "" }->{indent},
40 5 ▶ %{ $name__context->{ "" } },
Condition100%2/2
$name__context->{$context_name} //= {"start_indent", $name__context->{""}{"indent"}, %{$name__context->{""};}}
l!l
41
MC/DC100%1/1
$name__context->{$context_name} //= {"start_indent", $name__context->{""}{"indent"}, %{$name__context->{""};}}
$name__context->{$context_name}
Truth table100%2/2
A
A= $name__context->{$context_name}
Aresult
11
00
41 };
42  
43 5 ▶ my $indent_increase = $options->{indent_increase} // 4;
Condition50%1/2
$options->{"indent_increase"} // 4
l!l
05
MC/DC0%0/1
$options->{"indent_increase"} // 4
$options->{"indent_increase"}
Truth table50%1/2
A
A= $options->{"indent_increase"}
Aresult
11
00
44  
45 5 $context->{indent} += $indent_increase;
46  
47 5 ▶ my $context_prefix = $context_name eq ""
Branch: 1 branch
BranchTrueFalseTotal
$context_name eq '' ? : 4 1 5
48 ? ""
49 : "$context_name: ";
50  
51 return timer_guard(
52 5 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
53 5 my $duration = shift;
54 5 _collect_timing($message, $duration);
55  
56 # Un-nest indentation level
57 5 my $indent = $context->{indent} -= $indent_increase;
58  
59 5 my $indentation = " " x max(1, $indent);
60 5 $log->trace("${log_prefix}duration($duration)$indentation$context_prefix$message");
61  
62 # Clean up context if we're done with it
63 5 ▶ if(my $start_indent = $context->{start_indent}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $start_indent = $context->{"start_indent"}) 1 4 5
64 1 ▶ if( $indent == $start_indent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indent == $start_indent) 1 0 1
65 1 delete $name__context->{ $context_name };
66 }
67 }
68 },
69 5 4, # decimal points
70 );
71 }
72  
73  
74 1 ▶sub subroutine_timer {
Subroutine subroutine_timer: called CC=4 SCAR=14.3
Pod documented
75 1 my ($message, $options) = @_;
76 1 ▶ $options //= {};
Condition50%1/2
$options //= {}
l!l
01
MC/DC0%0/1
$options //= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
77  
78 1 ▶ my $depth = $options->{depth} || 1;
Condition50%1/2
$options->{'depth'} || 1
l!l
01
MC/DC0%0/1
$options->{'depth'} || 1
$options->{'depth'}
Truth table50%1/2
A
A= $options->{'depth'}
Aresult
11
00
79 1 my (undef, undef, undef, $subroutine) = caller($depth);
80 1 $subroutine =~ s/::(\w+)$/->$1/;
81 1 ▶ $message = defined($message) ? ": $message" : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $message ? : 0 1 1
82  
83 1 return timer( "$subroutine$message", $options );
84 }
85  
86 5 ▶sub _collect_timing {
Subroutine _collect_timing: called CC=2 SCAR=7.0
87 5 my ($message, $duration) = @_;
88 5 ▶ my $stats = $message_stats->{$message} ||= Statistics::Descriptive::Sparse->new();
Condition66%2/3
$message_stats->{$message} ||= "Statistics::Descriptive::Sparse"->new
l!l&&r!l&&!r
140
MC/DC0%0/2
$message_stats->{$message} ||= "Statistics::Descriptive::Sparse"->new
$message_stats->{$message} "Statistics::Descriptive::Sparse"->new
Truth table66%2/3
A ||= B
A= $message_stats->{$message}B= "Statistics::Descriptive::Sparse"->new
ABresult
1X1
011
000
89 5 $stats->add_data($duration);
90 }
91  
92  
93 2 ▶sub report_timing_stats {
Subroutine report_timing_stats: called CC=2 SCAR=6.9
Pod documented
94 2 my @what = ( "mean", "sum", "min", "max", "standard_deviation" );
95  
96 my @messages =
97 4 map { $_->{message} }
98 reverse
99 5 sort { $a->{order} <=> $b->{order} }
100 map {
101 2 my $stats = $message_stats->{$_};
102 +{
103 message => "Stats: "
104 . sprintf("%-12s", "count(" . $stats->count . "), ")
105 . join(
106 ", ",
107 4 map { sprintf("$_(%.3f)", ($stats->$_)) } @what
108 ) . " for ($_)",
109 order => $stats->sum,
110 };
111 }
112 keys %$message_stats;
113  
114 2 for my $message (@messages) {
115 4 $log->info($message);
116 }
117  
118 2 clear_timing_stats();
119 }
120  
121  
122 2 ▶sub clear_timing_stats {
Subroutine clear_timing_stats: called CC=1 SCAR=0.0
Pod documented
123 2 $message_stats = {};
124 }
125  
126 1 ▶END {
Subroutine END: called CC=1 SCAR=0.0
127 1 report_timing_stats();
128 }
129  
130 1 1;
131  
132 __END__
↑ Summary Guard/Timer.pm »