blib/lib/Devel/NYTProf/Util.pm

filter: Statementstmt 97.4% 116 / 119 Branchbran 80.8% 55 / 68 Conditioncond 72.7% 8 / 11 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 100% 18 / 18 Podpod 0.0% 0 / 9 totaltotal 85.7% 199 / 232 CC 43 SCAR 18.6
CC
43
Cov
90%
strip_prefix_from_paths
10.8
fmt_time
10.3
fmt_incl_excl_time
7.1
CRAP 6.4
« blib/lib/Devel/NYTProf/SubInfo.pm ↑ Summary blib/script/nytprofcalls »
1 # vim: ts=8 sw=4 expandtab:
2 ##########################################################
3 # This script is part of the Devel::NYTProf distribution
4 #
5 # Copyright, contact and other information can be found
6 # at the bottom of this file, or by going to:
7 # http://metacpan.org/release/Devel-NYTProf/
8 #
9 ###########################################################
10 package Devel::NYTProf::Util;
11  
12 =head1 NAME
13  
14 Devel::NYTProf::Util - general utility functions for L<Devel::NYTProf>
15  
16 =head1 SYNOPSIS
17  
18 use Devel::NYTProf::Util qw(strip_prefix_from_paths);
19  
20 =head1 DESCRIPTION
21  
22 Contains general utility functions for L<Devel::NYTProf>
23  
24 B<Note:> The documentation for this module is currently incomplete and out of date.
25  
26 =head1 FUNCTIONS
27  
28 =encoding ISO8859-1
29  
30 =cut
31  
32  
33 701 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 701 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 701 ▶use base qw'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 701 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 701 ▶use Cwd qw(getcwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 701 ▶use List::Util qw(sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
41 701 ▶use Devel::NYTProf::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 701 our $VERSION = '6.16';
44  
45 701 our @EXPORT_OK = qw(
46 fmt_float
47 fmt_time
48 fmt_incl_excl_time
49 make_path_strip_editor
50 strip_prefix_from_paths
51 calculate_median_absolute_deviation
52 get_alternation_regex
53 get_abs_paths_alternation_regex
54 html_safe_filename
55 trace_level
56 _dumper
57 );
58  
59  
60 340 ▶sub get_alternation_regex {
Subroutine get_alternation_regex: called CC=2 SCAR=6.9
Pod undocumented
61 340 my ($strings, $suffix_regex) = @_;
62 340 ▶ $suffix_regex = '' unless defined $suffix_regex;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $suffix_regex 1 339 340
63  
64 # sort longest string first
65 340 my @strings = sort { length $b <=> length $a } @$strings;
66  
67 # build string regex for each string
68 340 my $regex = join "|", map { quotemeta($_) . $suffix_regex } @strings;
69  
70 340 return qr/(?:$regex)/;
71 }
72  
73  
74 340 ▶sub get_abs_paths_alternation_regex {
Subroutine get_abs_paths_alternation_regex: called CC=7 SCAR=19.5
Pod undocumented
75 340 my ($inc, $cwd) = @_;
76 340 ▶ my @inc = @$inc or croak "No paths";
Branch: 1 branch
BranchTrueFalseTotal
my(@inc) = @$inc or croak("No paths") 339 1 340
77  
78 # rewrite relative directories to be absolute
79 # the logic here should match that in get_file_id()
80 339 ▶ my $abs_path_regex = ($^O eq "MSWin32") ? qr,^\w:/, : qr,^/,;
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 339 339
81 339 for (@inc) {
82 5857 ▶ next if $_ =~ $abs_path_regex; # already absolute
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /$abs_path_regex/ 4209 1648 5857
83 1648 $_ =~ s/^\.\///; # remove a leading './'
84 1648 ▶ $cwd ||= getcwd();
Condition66%2/3
$cwd ||= getcwd()
l!l&&r!l&&!r
13103380
MC/DC0%0/2
$cwd ||= getcwd()
$cwd getcwd()
Truth table66%2/3
A ||= B
A= $cwdB= getcwd()
ABresult
1X1
011
000
85 1648 ▶ $_ = ($_ eq '.') ? $cwd : "$cwd/$_";
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '.' ? : 640 1008 1648
86 }
87  
88 339 return get_alternation_regex(\@inc, '/?');
89 }
90  
91  
92 25691 ▶sub make_path_strip_editor {
Subroutine make_path_strip_editor: called CC=4 SCAR=13.9
Pod undocumented
93 25691 my ($inc_ref, $anchor, $replacement) = @_;
94 25691 ▶ $anchor = '^' if not defined $anchor;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $anchor 1 25690 25691
95 25691 ▶ $replacement = '' if not defined $replacement;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $replacement 25691 0 25691
96  
97 25691 my @inc = @$inc_ref;
98  
99 25691 our %make_path_strip_editor_cache;
100 25691 my $key = join "\t", $anchor, $replacement, @inc;
101  
102 25691 ▶ return $make_path_strip_editor_cache{$key} ||= do {
Condition66%2/3
$Devel::NYTProf::Util::make_path_strip_editor_cache{$key} ||= do { my $inc_regex = get_abs_paths_alternation_regex(\@inc); $inc_regex = qr/($anchor)$inc_regex/; sub { $_[0] =~ s/$inc_regex/$1$replacement/; } }
l!l&&r!l&&!r
253543370
MC/DC0%0/2
$Devel::NYTProf::Util::make_path_strip_editor_cache{$key} ||= do { my $inc_regex = get_abs_paths_alternation_regex(\@inc); $inc_regex = qr/($anchor)$inc_regex/; sub { $_[0] =~ s/$inc_regex/$1$replacement/; } }
$Devel::NYTProf::Util::make_path_strip_editor_cache{$key} do { my $inc_regex = get_abs_paths_alternation_regex(\@inc); $inc_regex = qr/($anchor)$inc_regex/; sub { $_[0] =~ s/$inc_regex/$1$replacement/; } }
Truth table66%2/3
A ||= B
A= $Devel::NYTProf::Util::make_path_strip_editor_cache{$key}B= do { my $inc_regex = get_abs_paths_alternation_regex(\@inc); $inc_regex = qr/($anchor)$inc_regex/; sub { $_[0] =~ s/$inc_regex/$1$replacement/; } }
ABresult
1X1
011
000
103  
104 337 my $inc_regex = get_abs_paths_alternation_regex(\@inc);
105  
106 # anchor at start, capture anchor
107 337 $inc_regex = qr{($anchor)$inc_regex};
108  
109 337 ▶ sub { $_[0] =~ s{$inc_regex}{$1$replacement} };
Subroutine __ANON__: called CC=1 SCAR=0.0
110 };
111 }
112  
113  
114 # edit @$paths in-place to remove specified absolute path prefixes
115 25695 ▶sub strip_prefix_from_paths {
Subroutine strip_prefix_from_paths: called CC=10 SCAR=23.8
Pod undocumented
116 25695 my ($inc_ref, $paths, $anchor, $replacement) = @_;
117  
118 25695 ▶ croak "strip_prefix_from_paths() first argument must be array ref"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $inc_ref eq "ARRAY" 1 25694 25695
119 unless ref($inc_ref) eq 'ARRAY';
120 25694 ▶ return unless @{$inc_ref};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$inc_ref;} 1 25693 25694
121  
122 25693 ▶ return if not defined $paths;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $paths 1 25692 25693
123 25692 ▶ croak "strip_prefix_from_paths() second argument must be array ref"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $paths eq "ARRAY" 1 25691 25692
124 unless ref($paths) eq 'ARRAY';
125  
126 25691 ▶ my $editor = make_path_strip_editor($inc_ref, $anchor, $replacement)
Branch: 1 branch
BranchTrueFalseTotal
my $editor = make_path_strip_editor($inc_ref, $anchor, $replacement) or return 25691 0 25691
127 or return;
128  
129 # strip off prefix using regex, skip any empty/undef paths
130 25691 ▶ if (UNIVERSAL::isa($paths, 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($paths, 'ARRAY')) { } 25691 0 25691
131 25691 for my $path (@$paths) {
132 25691 ▶ if (ref $path) { # recurse to process deeper data
Branch: 2 branches
BranchTrueFalseTotal
if (ref $path) { } 0 25691 25691
elsif ($path) { } 25691 0 25691
133 0 strip_prefix_from_paths($inc_ref, $path, $anchor, $replacement);
134 }
135 elsif ($path) {
136 25691 $editor->($path);
137 }
138 }
139 }
140 # There are no instances in test suite where $paths is a hash ref
141 # elsif (UNIVERSAL::isa($paths, 'HASH')) {
142 # for my $orig (keys %$paths) {
143 # $editor->(my $new = $orig)
144 # or next;
145 # my $value = delete $paths->{$orig};
146 # warn "Stripping prefix from $orig overwrites existing $new"
147 # if defined $paths->{$new};
148 # $paths->{$new} = $value;
149 # }
150 # }
151 else {
152 0 croak "Can't strip_prefix_from_paths of $paths";
153 }
154  
155 25691 return;
156 }
157  
158  
159 # eg normalize the width/precision so that the tables look good.
160 17 ▶sub fmt_float {
Subroutine fmt_float: called CC=5 SCAR=16.1
Pod undocumented
161 17 my ($val, $precision) = @_;
162 17 ▶ $precision ||= 5;
Condition100%2/2
$precision ||= 5
l!l
89
MC/DC100%1/1
$precision ||= 5
$precision
Truth table100%2/2
A
A= $precision
Aresult
11
00
163 17 ▶ if ($val < 10 ** -($precision - 1) and $val > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($val < 10 ** (-($precision - 1)) and $val > 0) { } 1 16 17
elsif ($val != int $val) { } 14 2 16
Condition66%2/3
$val < 10 ** (-($precision - 1)) and $val > 0
!ll&&!rl&&r
1601
MC/DC50%1/2
$val < 10 ** (-($precision - 1)) and $val > 0
$val < 10 ** (-($precision - 1)) $val > 0
Truth table66%2/3
A and B
A= $val < 10 ** (-($precision - 1))B= $val > 0
ABresult
0X0
100
111
164 # Give the same width as a larger value formatted with the %f below.
165 # This gives us 2 digits of precision for $precision == 5
166 1 $val = sprintf("%." . ($precision - 4) . "e", $val);
167 # But our exponents will always be e-05 to e-09, never e-10 or smaller
168 # so remove the leading zero to make these small numbers stand out less
169 # on the table.
170 1 $val =~ s/e-0+/e-/;
171 }
172 elsif ($val != int($val)) {
173 14 $val = sprintf("%.${precision}f", $val);
174 }
175 17 return $val;
176 }
177  
178  
179 # XXX undocumented hack that may become to an option one day
180 # Useful for making the time data more easily parseable
181 701 my $fmt_time_opt = $ENV{NYTPROF_FMT_TIME}; # e.g., '%f' for 'raw' times
182  
183 28 ▶sub fmt_time {
Subroutine fmt_time: called CC=10 SCAR=23.3
Pod undocumented
184 28 my ($sec, $width) = @_;
185 28 ▶ $width = '' unless defined $width;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $width 28 0 28
186 28 ▶ return undef if not defined $sec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sec 0 28 28
187 28 ▶ return '-'.fmt_time(-$sec, $width) if $sec < 0; # negative value, can happen
Branch: 1 branch
BranchTrueFalseTotal
if $sec < 0 0 28 28
188 28 ▶ return sprintf $fmt_time_opt, $sec if $fmt_time_opt;
Branch: 1 branch
BranchTrueFalseTotal
if $fmt_time_opt 0 28 28
189 28 ▶ return sprintf "%$width.0fs", 0 unless $sec;
Branch: 1 branch
BranchTrueFalseTotal
unless $sec 1 27 28
190 27 ▶ return sprintf "%$width.0fns", $sec * 1e9 if $sec < 1e-6;
Branch: 1 branch
BranchTrueFalseTotal
if $sec < "1e-06" 4 23 27
191 23 ▶ return sprintf "%$width.0fµs", $sec * 1e6 if $sec < 1e-3;
Branch: 1 branch
BranchTrueFalseTotal
if $sec < "0.001" 7 16 23
192 16 ▶ return sprintf "%$width.*fms", 3 - length(int($sec * 1e3)), $sec * 1e3 if $sec < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $sec < 1 6 10 16
193 10 ▶ return sprintf "%$width.*fs", 3 - length(int($sec )), $sec if $sec < 100;
Branch: 1 branch
BranchTrueFalseTotal
if $sec < 100 8 2 10
194 2 return sprintf "%$width.0fs", $sec;
195 }
196  
197  
198 5 ▶sub fmt_incl_excl_time {
Subroutine fmt_incl_excl_time: called CC=7 SCAR=19.5
Pod undocumented
199 5 my ($incl, $excl) = @_;
200 5 my $diff = $incl - $excl;
201 5 ▶ return fmt_time($incl) unless $diff;
Branch: 1 branch
BranchTrueFalseTotal
unless $diff 1 4 5
202 4 $_ = fmt_time($_) for $incl, $excl, $diff;
203 4 ▶ if ($incl =~ /(\D+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($incl =~ /(\D+)$/) 4 0 4
204 # no need to repeat the unit if it's the same for all time stamps
205 4 my $unit = $1;
206 4 my $offset = -length($unit);
207 4 for ($excl, $diff) {
208 8 ▶ if (/(\D+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/(\D+)$/) 8 0 8
209 8 ▶ substr($_, $offset) = "" if $1 eq $unit
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq $unit 6 2 8
210 }
211 }
212 }
213 4 return sprintf "%s (%s+%s)", $incl, $excl, $diff;
214 }
215  
216  
217 ## Given a ref to an array of numeric values
218 ## returns median distance from the median value, and the median value.
219 ## See http://en.wikipedia.org/wiki/Median_absolute_deviation
220 82020 ▶sub calculate_median_absolute_deviation {
Subroutine calculate_median_absolute_deviation: called CC=4 SCAR=13.9
Pod undocumented
221 82020 my $values_ref = shift;
222 82020 my ($ignore_zeros) = @_;
223 82020 ▶ croak "No array ref given" unless ref $values_ref eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $values_ref eq "ARRAY" 1 82019 82020
224  
225 82019 ▶ my @values = ($ignore_zeros) ? grep {$_} @$values_ref : @$values_ref;
Branch: 1 branch
BranchTrueFalseTotal
$ignore_zeros ? : 1 82018 82019
226 82019 my $median_value = [sort { $a <=> $b } @values]->[@values / 2];
227  
228 82019 ▶ return [0, 0] if not defined $median_value; # no data
Branch: 1 branch
BranchTrueFalseTotal
unless defined $median_value 8841 73178 82019
229  
230 73178 my @devi = map { abs($_ - $median_value) } @values;
231 73178 my $median_devi = [sort { $a <=> $b } @devi]->[@devi / 2];
232  
233 73178 return [$median_devi, $median_value];
234 }
235  
236  
237 16720 ▶sub html_safe_filename {
Subroutine html_safe_filename: called CC=2 SCAR=7.1
Pod undocumented
238 16720 my ($fname) = @_;
239 # replace / and \ with html safe '-', we also do a bunch of other
240 # chars, especially ':' for Windows, to make the namer simpler and safer
241 # also remove dots to keep VMS happy
242 16720 $fname =~ s{ [-/\\:\*\?"'<>|.]+ }{-}xg;
243 # remove any leading or trailing '-' chars
244 16720 $fname =~ s{^-}{};
245 16720 $fname =~ s{-$}{};
246 16720 ▶ if($^O eq 'VMS'){
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "VMS") 0 16720 16720
247 # ODS-2 is limited to 39.39 chars (39 filename, 39 extension)
248 # Reader.pm appends -LEVEL onto html safe filename so must
249 # subtract 1 + max length of (sub block line), so 6.
250 0 $fname = substr($fname,-33);
251 }
252 16720 return $fname;
253 }
254  
255 2 ▶sub _dumper {
Subroutine _dumper: called CC=1 SCAR=0.0
256 2 require Data::Dumper;
257 2 local $Data::Dumper::Sortkeys = 1;
258 2 local $Data::Dumper::Indent = 1;
259 2 return Data::Dumper::Dumper(@_);
260 }
261  
262 701 1;
263  
264 __END__
« blib/lib/Devel/NYTProf/SubInfo.pm ↑ Summary blib/script/nytprofcalls »