blib/lib/Devel/SmallProf.pm untested

filter: Statementstmt 0.0% 0 / 121 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 243 MC/DCmcdc 0.0% 0 / 84 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 496 CC 17 SCAR 42.3
CC
17
Cov
0%
END
132.0
BEGIN
30.0
DB
12.0
CRAP 68.7
1 package Devel::SmallProf; # To help the CPAN indexer to identify us
2  
3 our $VERSION = '2.02';
4  
5 package DB;
6  
7 # do not profile subs
8 BEGIN { $^P=0x122 }
9  
10 use strict;
11  
12 sub Time::HiRes::time ();
13  
14 our ($profile, $drop_zeros, $grep_format, %packages);
15  
16 my ($cdone, $done, $cstart, $start, $prevf, $prevl, $nulltime,
17 %listings, %profiles, %times, %ctimes);
18  
19 sub sub; # even if it is not used it has to be declared!
20  
21 sub DB {
22 $profile || return;
23  
24 my($pkg,$filename,$line) = caller;
25 %packages && !$packages{$pkg} && return;
26  
27 $done = Time::HiRes::time;
28 my ($u,$s,$cu,$cs) = times;
29 $cdone = $u+$s+$cu+$cs;
30  
31 # Now save the _< array for later reference. If we don't do this here,
32 # evals which do not define subroutines will disappear.
33 no strict 'refs';
34 $listings{$filename} = \@{"main::_<$filename"}
35 if defined(@{"main::_<$filename"});
36 use strict 'refs';
37  
38 my($delta);
39 $delta = $done - $start;
40 $delta = ($delta > $nulltime) ? $delta - $nulltime : 0;
41 $profiles{$filename}->[$line]++;
42 $times{$prevf}->[$prevl] += $delta;
43 $ctimes{$prevf}->[$prevl] += ($cdone - $cstart);
44 ($prevf, $prevl) = ($filename, $line);
45  
46 ($u,$s,$cu,$cs) = times;
47 $cstart = $u+$s+$cu+$cs;
48 $start = Time::HiRes::time;
49 }
50  
51  
52 use Time::HiRes;
53  
54 BEGIN {
55 $drop_zeros = 0;
56 $profile = 1;
57 $grep_format = 0;
58 if (-e '.smallprof') {
59 do '.smallprof';
60 }
61 my $env=$ENV{SMALLPROF_CONFIG}||'';
62 $drop_zeros = 1 if $env=~/z/;
63 $profile = 1 if $env=~/p/;
64 $grep_format = 1 if $env=~/g/;
65  
66 # print STDERR "drop_zeros=$DB::drop_zeros grep_format=$DB::grep_format\n";
67  
68 $prevf = '';
69 $prevl = 0;
70 my($diff,$cdiff);
71  
72 my($testDB) = sub {
73 my($pkg,$filename,$line) = caller;
74 $profile || return;
75 %packages && !$packages{$pkg} && return;
76 };
77  
78 # "Null time" compensation code
79 $nulltime = 0;
80 for (1..100) {
81 my($u,$s,$cu,$cs) = times;
82 $cstart = $u+$s+$cu+$cs;
83 $start = Time::HiRes::time;
84 &$testDB;
85 ($u,$s,$cu,$cs) = times;
86 $cdone = $u+$s+$cu+$cs;
87 $done = Time::HiRes::time;
88 $diff = $done - $start;
89 $nulltime += $diff;
90 }
91 $nulltime /= 100;
92  
93 my($u,$s,$cu,$cs) = times;
94 $cstart = $u+$s+$cu+$cs;
95 $start = Time::HiRes::time;
96 }
97  
98 END {
99 # Get time on last line executed.
100 my($u,$s,$cu,$cs) = times;
101 $cdone = $u+$s+$cu+$cs;
102 $done = Time::HiRes::time;
103 my($delta);
104 $delta = $done - $start;
105 $delta = ($delta > $nulltime) ? $delta - $nulltime : 0;
106 $times{$prevf}->[$prevl] += $delta;
107 $ctimes{$prevf}->[$prevl] += ($cdone - $cstart);
108  
109 # Now write out the results.
110 open(OUT,">smallprof.out");
111 select OUT;
112  
113 if ($grep_format) {
114 my @unsorted=();
115 for my $file (keys %profiles) {
116 my @line=@{$profiles{$file}};
117 for my $i (0..@line) {
118 my ($rfile, $ri, $eval)=
119 ($file=~/^\(eval\s*(\d+)\)\[(.*):(\d+)\]$/)
120 ? ($2, $3, "(eval $1:$i) ")
121 : ($file, $i, "");
122 $drop_zeros and !$line[$i] and next;
123 my $line=sprintf('%s:%s:%d:%d:%d: %s%s',
124 $rfile, $ri, $line[$i],
125 int($times{$file}[$i]*1000),
126 int($ctimes{$file}[$i]*1000),
127 $eval,
128 $listings{$file}[$i]||'?' );
129 chomp $line;
130 push @unsorted, [ $line, $times{$file}[$i]];
131 }
132 }
133 my @sorted=sort { $b->[1] <=> $a->[1] } @unsorted;
134 print "* file name : line number : line count : time (ms) : ctime (ms) : line source\n";
135 for (@sorted) {
136 print "$_->[0]\n";
137 }
138 }
139 else {
140 my($i,$stat,$time,$ctime,$line,$file,$page);
141 $page = 1;
142  
143 format OUT_TOP=
144 @||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
145 '================ SmallProf version '.$Devel::SmallProf::VERSION.' ================'
146 @|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Page @<<
147 "Profile of $file",$page++
148 =================================================================
149 count wall tm cpu time line
150 .
151 format OUT=
152 @######## @##.##### @##.##### @####:^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
153 $stat,$time,$ctime,$i,$line
154 .
155  
156 foreach $file (sort keys %profiles) {
157 $- = 0;
158 if (defined($listings{$file})) {
159 $i = -1;
160 foreach $line (@{$listings{$file}}) {
161 ++$i or next;
162 if (defined($line)) {
163 chomp($line);
164 } else {
165 $line = '';
166 }
167 $stat = $profiles{$file}->[$i] || 0 or !$drop_zeros or next;
168 $time = defined($times{$file}->[$i]) ?
169 $times{$file}->[$i] : 0;
170 $ctime = defined($ctimes{$file}->[$i]) ?
171 $ctimes{$file}->[$i] : 0;
172 write OUT;
173 }
174 } else {
175 $line = "The code for $file is not in the symbol table.";
176 for ($i=1; $i <= $#{$profiles{$file}}; $i++) {
177 next unless
178 ($stat = $profiles{$file}->[$i] || 0 or !$drop_zeros);
179 $time = defined($times{$file}->[$i]) ?
180 $times{$file}->[$i] : 0;
181 $ctime = defined($ctimes{$file}->[$i]) ?
182 $ctimes{$file}->[$i] : 0;
183 write OUT;
184 }
185 }
186 }
187 }
188 close OUT;
189 }
190  
191 1;
192  
193 __END__