blib/lib/Telemetry/Any.pm

filter: Statementstmt 89.4% 68 / 76 Branchbran 81.2% 13 / 16 Conditioncond 72.7% 8 / 11 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 100% 12 / 12 Podpod 40.0% 2 / 5 totaltotal 84.2% 107 / 127 CC 15 SCAR 15.6
CC
15
Cov
86%
report
9.0
import
5.4
detailed
3.0
CRAP 4.8
↑ Summary Build »
1 package Telemetry::Any;
2 2 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use base 'Devel::Timer';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = "0.06";
11  
12 2 my $telemetry = __PACKAGE__->new();
13  
14 sub import {
15 2 ▶ my ( $class, $var ) = @_;
Subroutine import: called CC=3 SCAR=16.9
16  
17 2 ▶ return if !defined $var;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $var 2 0 2
18  
19 0 my $saw_var;
20 0 ▶ if ( $var =~ /^\$(\w+)/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var =~ /^\$(\w+)/x) { } 0 0 0
21 0 $saw_var = $1;
22 }
23 else {
24 0 croak('Аrgument must be a variable');
25 }
26  
27 0 my $caller = caller();
28  
29 2 ▶ no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
30 0 my $varname = "${caller}::${saw_var}";
31 0 *$varname = \$telemetry;
32  
33 0 return;
34 }
35  
36 ## calculate total time (start time vs last time)
37 sub total_time {
38 34 ▶ my ($self) = @_;
Subroutine total_time: called CC=1 SCAR=0.0
Pod undocumented
39  
40 34 return Time::HiRes::tv_interval( $self->{times}->[0], $self->{times}->[ $self->{count} - 1 ] );
41 }
42  
43 sub report {
44 6 ▶ my ( $self, %args ) = @_;
Subroutine report: called CC=9 SCAR=22.0
Pod documented
45  
46 6 ▶ my @records = $args{collapse} ? $self->collapsed(%args) : $self->detailed(%args);
Branch: 1 branch
BranchTrueFalseTotal
$args{'collapse'} ? : 3 3 6
47  
48 6 my $report;
49  
50 6 ▶ if ( defined $args{format} && $args{format} eq 'table' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"format"} and $args{"format"} eq "table") 2 4 6
Condition66%2/3
defined $args{"format"} and $args{"format"} eq "table"
!ll&&!rl&&r
402
MC/DC50%1/2
defined $args{"format"} and $args{"format"} eq "table"
defined $args{"format"} $args{"format"} eq "table"
Truth table66%2/3
A and B
A= defined $args{"format"}B= $args{"format"} eq "table"
ABresult
0X0
100
111
51 2 $report .= ref($self) . ' Report -- Total time: ' . sprintf( '%.4f', $self->total_time() ) . " secs\n";
52 }
53  
54 6 ▶ if ( $args{collapse} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'collapse'}) { } 3 3 6
55 3 ▶ if ( defined $args{format} && $args{format} eq 'table' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"format"} and $args{"format"} eq "table") 1 2 3
Condition66%2/3
defined $args{"format"} and $args{"format"} eq "table"
!ll&&!rl&&r
201
MC/DC50%1/2
defined $args{"format"} and $args{"format"} eq "table"
defined $args{"format"} $args{"format"} eq "table"
Truth table66%2/3
A and B
A= defined $args{"format"}B= $args{"format"} eq "table"
ABresult
0X0
100
111
56 1 $report .= "Count Time Percent\n";
57 1 $report .= "----------------------------------------------\n";
58 }
59  
60 $report .= join "\n",
61 3 map { sprintf( '%8s %.4f %5.2f%% %s', $_->{count}, $_->{time}, $_->{percent}, $_->{label}, ) } @records;
62 }
63 else {
64 3 ▶ if ( defined $args{format} && $args{format} eq 'table' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"format"} and $args{"format"} eq "table") 1 2 3
Condition66%2/3
defined $args{"format"} and $args{"format"} eq "table"
!ll&&!rl&&r
201
MC/DC50%1/2
defined $args{"format"} and $args{"format"} eq "table"
defined $args{"format"} $args{"format"} eq "table"
Truth table66%2/3
A and B
A= defined $args{"format"}B= $args{"format"} eq "table"
ABresult
0X0
100
111
65 1 $report .= "Interval Time Percent\n";
66 1 $report .= "----------------------------------------------\n";
67 }
68  
69 $report .= join "\n", map {
70 3 sprintf(
71 '%02d -> %02d %.4f %5.2f%% %s',
72 $_->{interval} - 1,
73 $_->{interval}, $_->{time}, $_->{percent}, $_->{label},
74 )
75 20 } @records;
76 }
77  
78 6 return $report;
79 }
80  
81 sub detailed {
82 3 ▶ my ( $self, %args ) = @_;
Subroutine detailed: called CC=3 SCAR=11.0
Pod undocumented
83  
84 ## sort interval structure based on value
85  
86 3 @{ $self->{intervals} } = sort { $b->{value} <=> $a->{value} } @{ $self->{intervals} };
87  
88 ##
89 ## report of each time space between marks
90 ##
91  
92 3 my @records;
93  
94 3 for my $i ( @{ $self->{intervals} } ) {
95 ## skip first time (to make an interval,
96 ## compare the current time with the previous one)
97  
98 23 ▶ next if ( $i->{index} == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $i->{"index"} == 0 3 20 23
99  
100 my $record = { ## no critic (NamingConventions::ProhibitAmbiguousNames
101 interval => $i->{index},
102 time => sprintf( '%.6f', $i->{value} ),
103 percent => sprintf( '%.2f', $i->{value} / $self->total_time() * 100 ),
104 20 label => sprintf( '%s -> %s', $self->{label}->{ $i->{index} - 1 }, $self->{label}->{ $i->{index} } ),
105 };
106  
107 20 push @records, $record;
108 }
109  
110 3 return @records;
111 }
112  
113 sub collapsed {
114 3 ▶ my ( $self, %args ) = @_;
Subroutine collapsed: called CC=3 SCAR=11.0
Pod undocumented
115  
116 3 $self->_calculate_collapsed;
117  
118 3 my $c = $self->{collapsed};
119 3 ▶ my $sort_by = $args{sort_by} || 'time';
Condition100%2/2
$args{'sort_by'} || 'time'
l!l
12
MC/DC100%1/1
$args{'sort_by'} || 'time'
$args{'sort_by'}
Truth table100%2/2
A
A= $args{'sort_by'}
Aresult
11
00
120  
121 3 my @labels = sort { $c->{$b}->{$sort_by} <=> $c->{$a}->{$sort_by} } keys %$c;
122  
123 3 my @records;
124  
125 3 foreach my $label (@labels) {
126  
127 my $record = { ## no critic (NamingConventions::ProhibitAmbiguousNames
128 count => $c->{$label}->{count},
129 time => sprintf( '%.6f', $c->{$label}->{time} ),
130 12 percent => sprintf( '%.2f', $c->{$label}->{time} / $self->total_time() * 100 ),
131 label => $label,
132 };
133  
134 12 push @records, $record;
135 }
136  
137 3 return @records;
138 }
139  
140 sub reset { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
141 1 ▶ my ($self) = @_;
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
142  
143 1 %{$self} = (
144 times => [],
145 count => 0,
146 label => {},
147 );
148  
149 1 return $self;
150 }
151  
152 2 1;
153 __END__
154  
155 =encoding utf-8
156  
157 =head1 NAME
158  
159 Telemetry::Any - It's new $module
160  
161 =head1 SYNOPSIS
162  
163 use Telemetry::Any;
164  
165 =head1 DESCRIPTION
166  
167 Telemetry::Any is ...
168  
169 =head1 LICENSE
170  
171 Copyright (C) Mikhail Ivanov.
172  
173 This library is free software; you can redistribute it and/or modify
174 it under the same terms as Perl itself.
175  
176 =head1 AUTHOR
177  
178 Mikhail Ivanov E<lt>m.ivanych@gmail.comE<gt>
179  
180 =cut
181  
↑ Summary Build »