| 1 | package Devel::SimpleProfiler; | ||||||||||||||||||||
| 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 | |||||||||||||||||||||
| 6 | 1 | ▶ | use Aspect; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | use Time::HiRes; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 1 | ▶ | use File::Temp qw/ tempfile /; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 1 | ▶ | use vars qw($VERSION); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 1 | $VERSION = '1.0'; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 1 | our( @stack, %calltimes, %callers, %calls, $tmpFile, $re ); | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | =head1 NAME | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | Devel::SimpleProfiler - quick and dirty perl code profiler | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | =head1 SYNPOSIS | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | use Devel::SimpleProfiler; | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | Devel::SimpleProfiler::init( "/tmp/tmpfile", qr/RegexToMatchSubNames/ ); | ||||||||||||||||||||
| 27 | Devel::SimpleProfiler::start; | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | .... | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | if( ! fork ) { | ||||||||||||||||||||
| 32 | # must restart for child process | ||||||||||||||||||||
| 33 | Devel::SimpleProfiler::start; | ||||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | .... | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | Devel::SimpleProfiler::analyze('total'); | ||||||||||||||||||||
| 39 | exit; | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | # ---- PRINTS OUT (and sorts by total) ----- | ||||||||||||||||||||
| 42 | performance stats ( all times are in ms) | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | sub | # calls | total t | mean t | avg t | max t | min t | ||||||||||||||||||||
| 45 | -----------------+---------+---------+--------+-------+-------+------ | ||||||||||||||||||||
| 46 | main::test_suite | 1 | 2922 | 2922 | 2922 | 2922 | 2922 | ||||||||||||||||||||
| 47 | OtherThing::fun | 27 | 152 | 1 | 5 | 63 | 0 | ||||||||||||||||||||
| 48 | SomeObj::new | 3 | 26 | 8 | 8 | 8 | 8 | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | .... | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | =head1 DESCRIPTION | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | This is meant to be a simple way to get a performance benchmark for | ||||||||||||||||||||
| 55 | perl subs. It uses the fantastic Aspect module written by | ||||||||||||||||||||
| 56 | Adam Kennedy, Marcel Gruenauer and Ran Eilam to monkey patch select | ||||||||||||||||||||
| 57 | perl subs and gather statistics about them. | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =head1 METHODS | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | =head2 init | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | init takes two arguments : a temp file to use and a regular expression | ||||||||||||||||||||
| 64 | to find subs to measure. By default, the file is /tmp/foo and the | ||||||||||||||||||||
| 65 | regex is qr/^main:/; | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | init should be called once for a run. | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | =cut | ||||||||||||||||||||
| 70 | 0 | ▶ | sub init { | ||||||||||||||||||
|
Subroutine
init: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 71 | 0 | ( $tmpFile, $re ) = @_; | |||||||||||||||||||
| 72 | 0 | ▶ | $tmpFile ||= '/tmp/foo'; | ||||||||||||||||||
|
Condition0%0/2
$Devel::SimpleProfiler::tmpFile ||= "/tmp/foo"
MC/DC0%0/1
$Devel::SimpleProfiler::tmpFile ||= "/tmp/foo"
$Devel::SimpleProfiler::tmpFile
Truth table0%0/2
A
A= $Devel::SimpleProfiler::tmpFile
| |||||||||||||||||||||
| 73 | 0 | ▶ | $re ||= qr/^main:/; | ||||||||||||||||||
|
Condition0%0/2
$Devel::SimpleProfiler::re ||= qr/^main:/
MC/DC0%0/1
$Devel::SimpleProfiler::re ||= qr/^main:/
$Devel::SimpleProfiler::re
Truth table0%0/2
A
A= $Devel::SimpleProfiler::re
| |||||||||||||||||||||
| 74 | 0 | unlink $tmpFile; | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | =head2 analyze | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | analyze simply outputs the data collected from the profiler so far in | ||||||||||||||||||||
| 80 | a table with the columns | ||||||||||||||||||||
| 81 | * sub name | ||||||||||||||||||||
| 82 | * total number of calls | ||||||||||||||||||||
| 83 | * total time in ms | ||||||||||||||||||||
| 84 | * mean time in ms | ||||||||||||||||||||
| 85 | * average time in ms | ||||||||||||||||||||
| 86 | * max time in ms | ||||||||||||||||||||
| 87 | * min time in ms | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | This can be called as many times as desired. It takes an optional | ||||||||||||||||||||
| 90 | argument to sort by, which can be one of : | ||||||||||||||||||||
| 91 | 'calls', 'total', 'mean', 'avg', 'max', 'min' | ||||||||||||||||||||
| 92 | The default sorting is by average. | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | =cut | ||||||||||||||||||||
| 95 | 0 | ▶ | sub analyze { | ||||||||||||||||||
|
Subroutine
analyze: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 96 | 0 | my $sort = shift; | |||||||||||||||||||
| 97 | 0 | my( %funtimes, %funcalls, %funcalled ); | |||||||||||||||||||
| 98 | 0 | open( IN, "<$tmpFile" ); | |||||||||||||||||||
| 99 | 0 | ▶ | while( <IN> ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | 0 | chomp; | |||||||||||||||||||
| 101 | 0 | my( $fun, $time, $stack ) = split /\|/, $_; | |||||||||||||||||||
| 102 | 0 | push @{$funtimes{ $fun }}, $time; | |||||||||||||||||||
| 103 | 0 | my( @stack ) = split ',', $stack; | |||||||||||||||||||
| 104 | 0 | for my $call ( @stack ) { | |||||||||||||||||||
| 105 | 0 | $funcalls{$call}{$fun}++; | |||||||||||||||||||
| 106 | 0 | $funcalled{$fun}{$call}++ | |||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | 0 | _analyze( \%funtimes, \%funcalled, \%funcalls, $sort ); | |||||||||||||||||||
| 110 | } #analyze | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 2 | ▶ | sub _analyze { | ||||||||||||||||||
|
Subroutine
_analyze: called CC=6 SCAR=17.9
| |||||||||||||||||||||
| 113 | 2 | my( $calltimes, $callers, $calls, $sort ) = @_; | |||||||||||||||||||
| 114 | 2 | ▶ | $sort ||= 'avg'; | ||||||||||||||||||
|
Condition50%1/2
$sort ||= "avg"
MC/DC0%0/1
$sort ||= "avg"
$sort
Truth table50%1/2
A
A= $sort
| |||||||||||||||||||||
| 115 | 2 | my %stats; | |||||||||||||||||||
| 116 | 2 | my $longsub = 0; | |||||||||||||||||||
| 117 | 2 | for my $subr ( keys %$calltimes ) { | |||||||||||||||||||
| 118 | 6 | ▶ | if( length( $subr ) > $longsub ) { $longsub = length( $subr ) }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 119 | 6 | my @times = sort { $a <=> $b } @{$calltimes->{$subr}}; | |||||||||||||||||||
| 120 | 6 | my $calls = scalar( @times ); | |||||||||||||||||||
| 121 | 6 | my $tottime = 0; | |||||||||||||||||||
| 122 | 6 | map { $tottime += $_ } @times; | |||||||||||||||||||
| 123 | 6 | ▶ | $stats{$subr} = { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 124 | calls => $calls, | ||||||||||||||||||||
| 125 | total => $tottime, | ||||||||||||||||||||
| 126 | mean => $times[ int( @times/2 ) ], | ||||||||||||||||||||
| 127 | avg => $calls ? int($tottime / $calls) : '?', | ||||||||||||||||||||
| 128 | max => $times[$#times], | ||||||||||||||||||||
| 129 | min => $times[0], | ||||||||||||||||||||
| 130 | }; | ||||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | 2 | my( @titles ) = ( 'sub', '# calls', 'total t', 'mean t', 'avg t', 'max t', 'min t' ); | |||||||||||||||||||
| 133 | 2 | my $minwidth = 7; | |||||||||||||||||||
| 134 | 2 | my $buf = "\n performance stats ( all times are in ms)\n\n"; | |||||||||||||||||||
| 135 | 2 | $buf .= sprintf( "%*s | ", $longsub, "sub" ). join( " | ", map { sprintf( "%*s", $minwidth, $_ ) } @titles[1..$#titles] ) ."\n"; | |||||||||||||||||||
| 136 | 2 | $buf .= '-' x $longsub . '--+-' . join( "-+-", map { '-' x $minwidth } @titles[1..$#titles] )."\n"; | |||||||||||||||||||
| 137 | # for my $subr (sort { $stats{$b}{total} <=> $stats{$a}{total} } keys %stats) { | ||||||||||||||||||||
| 138 | 2 | for my $subr (sort { $stats{$b}{$sort} <=> $stats{$a}{$sort} } keys %stats) { | |||||||||||||||||||
| 139 | $buf .= join( " | ", sprintf( "%*s ", $longsub, $subr ), | ||||||||||||||||||||
| 140 | 6 | map { sprintf( "%*d", $minwidth, $stats{$subr}{$_} ) } | |||||||||||||||||||
| 141 | qw( calls total mean avg max min ) )."\n"; | ||||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | 2 | if( 0 ) { | |||||||||||||||||||
| 144 | $buf .= "Who Calls What\n"; | ||||||||||||||||||||
| 145 | for my $subr (sort { $stats{$a}->{total} <=> $stats{$b}->{total} } keys %stats) { | ||||||||||||||||||||
| 146 | my $calls = [sort { $calls->{$subr}{$b} <=> $calls->{$subr}{$a} } keys %{$calls->{$subr}||{}}]; | ||||||||||||||||||||
| 147 | my $called_by = [sort { $callers->{$subr}{$b} <=> $callers->{$subr}{$a} } keys %{$callers->{$subr}||{}}]; | ||||||||||||||||||||
| 148 | $buf .= " $subr\n" . | ||||||||||||||||||||
| 149 | " Called by :" . ( @$called_by ? "\n\t" . join( "\n\t", map { "$_ $callers->{$subr}{$_}" } @$called_by ) : '<not called>' ) . "\n" . | ||||||||||||||||||||
| 150 | " Calls :" . ( @$calls ? "\n\t" . join( "\n\t", map { "$_ $calls->{$subr}{$_}" } @$calls ) : '<does not make calls>' ) . "\n"; | ||||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | $buf .= "\n\n"; | ||||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | 2 | $buf; | |||||||||||||||||||
| 155 | } #_analyze | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | =head2 start | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | This is called to start or continue the data collection process. It takes | ||||||||||||||||||||
| 160 | an option regex parameter in case something different is desired than the | ||||||||||||||||||||
| 161 | one given at init. This must be called to continue the profiling in a | ||||||||||||||||||||
| 162 | child thread if one is forked. | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | =cut | ||||||||||||||||||||
| 165 | 0 | ▶ | sub start { | ||||||||||||||||||
|
Subroutine
start: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 166 | 0 | ▶ | my $re = shift || $re; | ||||||||||||||||||
|
Condition0%0/3
shift() || $Devel::SimpleProfiler::re
MC/DC0%0/2
shift() || $Devel::SimpleProfiler::re
shift()
$Devel::SimpleProfiler::re
Truth table0%0/3
A || B
A= shift()B= $Devel::SimpleProfiler::re
| |||||||||||||||||||||
| 167 | 0 | my $count = 0; | |||||||||||||||||||
| 168 | 0 | ▶ | around { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 169 | 0 | my $subname = $_->{sub_name}; | |||||||||||||||||||
| 170 | 0 | my $start = [Time::HiRes::gettimeofday]; # returns [ seconds, microseconds ] | |||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | 0 | push @stack, $subname; | |||||||||||||||||||
| 173 | 0 | $_->proceed; | |||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | 0 | pop @stack; | |||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | 0 | map { $callers{$subname}{$_}++; $calls{$_}{$subname}++ } @stack; | |||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | # tv_interval returns floating point seconds, convert to ms | ||||||||||||||||||||
| 180 | 0 | push @{$calltimes{$subname}}, 1_000 * Time::HiRes::tv_interval( $start ); | |||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | 0 | my $line = "$subname|" . (1_000 * Time::HiRes::tv_interval( $start ) ) . "|" . join(",", @stack ); | |||||||||||||||||||
| 183 | 0 | ++$count; | |||||||||||||||||||
| 184 | 0 | open( OUT, ">>$tmpFile" ); | |||||||||||||||||||
| 185 | 0 | print OUT "$line\n"; | |||||||||||||||||||
| 186 | 0 | close OUT; | |||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 0 | } call $re; | |||||||||||||||||||
| 189 | } #start | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | 1 | 1; | |||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | __END__ | ||||||||||||||||||||