| 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::Utilscip-perl cpan Devel-NYTProf 6.15 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 | use warningsscip-perl cpan Devel-NYTProf 6.15 warnings/; |
| 34 | use strictscip-perl cpan Devel-NYTProf 6.15 strict/; |
| 35 | |
| 36 | use basescip-perl cpan Devel-NYTProf 6.15 base/ qw'Exporter'; |
| 37 | |
| 38 | use Carpscip-perl cpan Devel-NYTProf 6.15 Carp/; |
| 39 | use Cwdscip-perl cpan Devel-NYTProf 6.15 Cwd/ qw(getcwd); |
| 40 | use List::Utilscip-perl cpan Devel-NYTProf 6.15 List/Util/ qw(sum); |
| 41 | use Devel::NYTProf::Corescip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Core/; |
| 42 | |
| 43 | our $VERSIONscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/$VERSION. = '6.15'; |
| 44 | |
| 45 | our @EXPORT_OKscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/`@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 | sub get_alternation_regexscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/get_alternation_regex(). { |
| 61 | my ($strings, $suffix_regex) = @_scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/`@_`.; |
| 62 | $suffix_regex = '' unless definedscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/defined(). $suffix_regex; |
| 63 | |
| 64 | # sort longest string first |
| 65 | my @strings = sortscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/sort(). { lengthscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/length(). $bscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/$b. <=> lengthscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/length(). $ascip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/$a. } @$strings; |
| 66 | |
| 67 | # build string regex for each string |
| 68 | my $regex = joinscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/join(). "|", mapscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/map(). { quotemetascip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/quotemeta().($_scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/$_.) . $suffix_regex } @strings; |
| 69 | |
| 70 | return qr/(?:$regex)/; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | sub get_abs_paths_alternation_regexscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/get_abs_paths_alternation_regex(). { |
| 75 | my ($inc, $cwd) = @_scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/`@_`.; |
| 76 | my @inc = @$inc or croakscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/croak(). "No paths"; |
| 77 | |
| 78 | # rewrite relative directories to be absolute |
| 79 | # the logic here should match that in get_file_id() |
| 80 | my $abs_path_regex = ($^Oscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/`$^O`. eq "MSWin32") ? qr,^\w:/, : qr,^/,; |
| 81 | for (@inc) { |
| 82 | next if $_scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/$_. =~ $abs_path_regex; # already absolute |
| 83 | $_scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/$_. =~ s/^\.\///; # remove a leading './' |
| 84 | $cwd ||= getcwdscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/getcwd().(); |
| 85 | $_scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/$_. = ($_scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/$_. eq '.') ? $cwd : "$cwd/$_"; |
| 86 | } |
| 87 | |
| 88 | return get_alternation_regexscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/get_alternation_regex().(\@inc, '/?'); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | sub make_path_strip_editorscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/make_path_strip_editor(). { |
| 93 | my ($inc_ref, $anchor, $replacement) = @_scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/`@_`.; |
| 94 | $anchor = '^' if not definedscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/defined(). $anchor; |
| 95 | $replacement = '' if not definedscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/defined(). $replacement; |
| 96 | |
| 97 | my @inc = @$inc_ref; |
| 98 | |
| 99 | our %make_path_strip_editor_cachescip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/`%make_path_strip_editor_cache`.; |
| 100 | my $key = joinscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/join(). "\t", $anchor, $replacement, @inc; |
| 101 | |
| 102 | return $make_path_strip_editor_cachescip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/`%make_path_strip_editor_cache`.{$key} ||= do { |
| 103 | |
| 104 | my $inc_regex = get_abs_paths_alternation_regexscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/get_abs_paths_alternation_regex().(\@inc); |
| 105 | |
| 106 | # anchor at start, capture anchor |
| 107 | $inc_regex = qr{($anchor)$inc_regex}; |
| 108 | |
| 109 | sub { $_scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/`@_`.[0] =~ s{$inc_regex}{$1$replacement} }; |
| 110 | }; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | # edit @$paths in-place to remove specified absolute path prefixes |
| 115 | sub strip_prefix_from_pathsscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/strip_prefix_from_paths(). { |
| 116 | my ($inc_ref, $paths, $anchor, $replacement) = @_scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/`@_`.; |
| 117 | |
| 118 | croakscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/croak(). "strip_prefix_from_paths() first argument must be array ref" |
| 119 | unless refscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/ref().($inc_ref) eq 'ARRAY'; |
| 120 | return unless @{$inc_ref}; |
| 121 | |
| 122 | return if not definedscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/defined(). $paths; |
| 123 | croakscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/croak(). "strip_prefix_from_paths() second argument must be array ref" |
| 124 | unless refscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/ref().($paths) eq 'ARRAY'; |
| 125 | |
| 126 | my $editor = make_path_strip_editorscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/make_path_strip_editor().($inc_ref, $anchor, $replacement) |
| 127 | or return; |
| 128 | |
| 129 | # strip off prefix using regex, skip any empty/undef paths |
| 130 | if (UNIVERSAL::isascip-perl cpan Devel-NYTProf 6.15 UNIVERSAL/isa().($paths, 'ARRAY')) { |
| 131 | for my $path (@$paths) { |
| 132 | if (refscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/ref(). $path) { # recurse to process deeper data |
| 133 | strip_prefix_from_pathsscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/strip_prefix_from_paths().($inc_ref, $path, $anchor, $replacement); |
| 134 | } |
| 135 | elsif ($path) { |
| 136 | $editor->(scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/`->()`().$path)scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/`->()`().; |
| 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 | croakscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/croak(). "Can't strip_prefix_from_paths of $paths"; |
| 153 | } |
| 154 | |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | # eg normalize the width/precision so that the tables look good. |
| 160 | sub fmt_floatscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/fmt_float(). { |
| 161 | my ($val, $precision) = @_scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/`@_`.; |
| 162 | $precision ||= 5; |
| 163 | if ($val < 10 ** -($precision - 1) and $val > 0) { |
| 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 | $val = sprintfscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/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 | $val =~ s/e-0+/e-/; |
| 171 | } |
| 172 | elsif ($val != intscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/int().($val)) { |
| 173 | $val = sprintfscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/sprintf().("%.${precision}f", $val); |
| 174 | } |
| 175 | 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 | my $fmt_time_opt = $ENVscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/`%ENV`.{NYTPROF_FMT_TIME}; # e.g., '%f' for 'raw' times |
| 182 | |
| 183 | sub fmt_timescip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/fmt_time(). { |
| 184 | my ($sec, $width) = @_scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/`@_`.; |
| 185 | $width = '' unless definedscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/defined(). $width; |
| 186 | return undef if not definedscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/defined(). $sec; |
| 187 | return '-'.fmt_timescip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/fmt_time().(-$sec, $width) if $sec < 0; # negative value, can happen |
| 188 | return sprintfscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/sprintf(). $fmt_time_opt, $sec if $fmt_time_opt; |
| 189 | return sprintfscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/sprintf(). "%$width.0fs", 0 unless $sec; |
| 190 | return sprintfscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/sprintf(). "%$width.0fns", $sec * 1e9 if $sec < 1e-6; |
| 191 | return sprintfscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/sprintf(). "%$width.0fµs", $sec * 1e6 if $sec < 1e-3; |
| 192 | return sprintfscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/sprintf(). "%$width.*fms", 3 - lengthscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/length().(intscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/int().($sec * 1e3)), $sec * 1e3 if $sec < 1; |
| 193 | return sprintfscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/sprintf(). "%$width.*fs", 3 - lengthscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/length().(intscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/int().($sec )), $sec if $sec < 100; |
| 194 | return sprintfscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/sprintf(). "%$width.0fs", $sec; |
| 195 | } |
| 196 | |
| 197 | |
| 198 | sub fmt_incl_excl_timescip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/fmt_incl_excl_time(). { |
| 199 | my ($incl, $excl) = @_scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/`@_`.; |
| 200 | my $diff = $incl - $excl; |
| 201 | return fmt_timescip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/fmt_time().($incl) unless $diff; |
| 202 | $_scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/$_. = fmt_timescip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/fmt_time().($_scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/$_.) for $incl, $excl, $diff; |
| 203 | if ($incl =~ /(\D+)$/) { |
| 204 | # no need to repeat the unit if it's the same for all time stamps |
| 205 | my $unit = $1scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/$1.; |
| 206 | my $offset = -lengthscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/length().($unit); |
| 207 | for ($excl, $diff) { |
| 208 | if (/(\D+)$/) { |
| 209 | substrscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/substr().($_scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/$_., $offset) = "" if $1scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/$1. eq $unit |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | return sprintfscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/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 | sub calculate_median_absolute_deviationscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/calculate_median_absolute_deviation(). { |
| 221 | my $values_ref = shiftscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/shift().; |
| 222 | my ($ignore_zeros) = @_scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/`@_`.; |
| 223 | croakscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/croak(). "No array ref given" unless refscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/ref(). $values_ref eq 'ARRAY'; |
| 224 | |
| 225 | my @values = ($ignore_zeros) ? grepscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/grep(). {$_scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/$_.} @$values_ref : @$values_ref; |
| 226 | my $median_value = [sortscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/sort(). { $ascip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/$a. <=> $bscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/$b. } @values]->[@values / 2]; |
| 227 | |
| 228 | return [0, 0] if not definedscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/defined(). $median_value; # no data |
| 229 | |
| 230 | my @devi = mapscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/map(). { absscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/abs().($_scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/$_. - $median_value) } @values; |
| 231 | my $median_devi = [sortscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/sort(). { $ascip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/$a. <=> $bscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/$b. } @devi]->[@devi / 2]; |
| 232 | |
| 233 | return [$median_devi, $median_value]; |
| 234 | } |
| 235 | |
| 236 | |
| 237 | sub html_safe_filenamescip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/html_safe_filename(). { |
| 238 | my ($fname) = @_scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/`@_`.; |
| 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 | $fname =~ s{ [-/\\:\*\?"'<>|.]+ }{-}xg; |
| 243 | # remove any leading or trailing '-' chars |
| 244 | $fname =~ s{^-}{}; |
| 245 | $fname =~ s{-$}{}; |
| 246 | if($^Oscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/`$^O`. eq 'VMS'){ |
| 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 | $fname = substrscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/substr().($fname,-33); |
| 251 | } |
| 252 | return $fname; |
| 253 | } |
| 254 | |
| 255 | sub _dumperscip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/_dumper(). { |
| 256 | requirescip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/require(). Data::Dumper; |
| 257 | local $Data::Dumper::Sortkeysscip-perl cpan Devel-NYTProf 6.15 Data/Dumper/$Sortkeys. = 1; |
| 258 | local $Data::Dumper::Indentscip-perl cpan Devel-NYTProf 6.15 Data/Dumper/$Indent. = 1; |
| 259 | return Data::Dumperscip-perl cpan Devel-NYTProf 6.15 Data/Dumper/Dumper().::Dumper(@_scip-perl cpan Devel-NYTProf 6.15 Devel/NYTProf/Util/`@_`.); |
| 260 | } |
| 261 | |
| 262 | 1; |
| 263 | |
| 264 | __END__ |
| 265 | |
| 266 | =head1 SEE ALSO |
| 267 | |
| 268 | L<Devel::NYTProf> and L<Devel::NYTProf::Data> |
| 269 | |
| 270 | =head1 AUTHOR |
| 271 | |
| 272 | B<Adam Kaplan>, C<< <akaplan at nytimes.com> >> |
| 273 | B<Tim Bunce>, L<http://blog.timbunce.org> |
| 274 | B<Steve Peters>, C<< <steve at fisharerojo.org> >> |
| 275 | |
| 276 | =head1 COPYRIGHT AND LICENSE |
| 277 | |
| 278 | Copyright (c) 2008 by Tim Bunce, Ireland. |
| 279 | |
| 280 | This library is free software; you can redistribute it and/or modify |
| 281 | it under the same terms as Perl itself, either Perl version 5.8.8 or, |
| 282 | at your option, any later version of Perl 5 you may have available. |
| 283 | |
| 284 | =cut |