| 1 | package Devel::Kit::_CODE; | ||||||||||
| 2 | |||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 7 | require Devel::Symdump; | |||||||||
| 7 | 7 | require Time::HiRes; | |||||||||
| 8 | 7 | require Unix::PID::Tiny; | |||||||||
| 9 | |||||||||||
| 10 | 7 | my $pid = Unix::PID::Tiny->new(); | |||||||||
| 11 | |||||||||||
| 12 | 7 | my $countops_lazy = 0; | |||||||||
| 13 | |||||||||||
| 14 | 9 | ▶ | sub _cd { | ||||||||
|
Subroutine
_cd: called CC=7 SCAR=19.6
| |||||||||||
| 15 | 9 | my ( $code, $dump_sym ) = @_; | |||||||||
| 16 | |||||||||||
| 17 | 9 | ▶ | if ( !exists $INC{'Devel/CountOps.pm'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | 0 | require Devel::CountOps; | |||||||||
| 19 | 0 | $countops_lazy++; | |||||||||
| 20 | } | ||||||||||
| 21 | |||||||||||
| 22 | 9 | ▶ | if ( ref( $_[0] ) ne 'CODE' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 0 | return "Error: cd() requires a CODE ref\n"; | |||||||||
| 24 | } | ||||||||||
| 25 | else { | ||||||||||
| 26 | 9 | local $| = 1; | |||||||||
| 27 | 9 | Devel::Kit::d("Begin cd($code)"); | |||||||||
| 28 | 9 | my $sym_pre = Devel::Symdump->rnew('main'); | |||||||||
| 29 | 9 | my $pid_pre = $pid->pid_info_hash($$); | |||||||||
| 30 | 9 | my $start_time = [ Time::HiRes::gettimeofday() ]; | |||||||||
| 31 | 9 | my $start_ops_count = ${^_OPCODES_RUN}; | |||||||||
| 32 | 9 | eval { $code->() }; | |||||||||
| 33 | 9 | my $stop_ops_count = ${^_OPCODES_RUN}; | |||||||||
| 34 | 9 | my $err = $@; # after $stop_ops_count so we don't count the opts in that, before anything else in case an eval way down deep makes $@ wonky | |||||||||
| 35 | 9 | my $stop_time = [ Time::HiRes::gettimeofday() ]; | |||||||||
| 36 | 9 | my $pid_pst = $pid->pid_info_hash($$); | |||||||||
| 37 | 9 | my $sym_pst = Devel::Symdump->rnew('main'); | |||||||||
| 38 | 9 | Devel::Kit::o("\n"); # in case $code->() has no trailing newline | |||||||||
| 39 | 9 | Devel::Kit::d("End cd($code)"); | |||||||||
| 40 | 9 | ▶ | return "Error: “$code” failed:\n\t$err\n" if $err; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | |||||||||||
| 42 | 8 | my $res_hr = {}; | |||||||||
| 43 | |||||||||||
| 44 | 8 | $res_hr->{'seconds'} = Time::HiRes::tv_interval( $start_time, $stop_time ); | |||||||||
| 45 | 8 | ▶ | if ( $res_hr->{'seconds'} =~ m/e/i ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 2 | $res_hr->{'seconds'} = $stop_time->[0] - $start_time->[0]; | |||||||||
| 47 | 2 | $res_hr->{'seconds'} .= '.'; | |||||||||
| 48 | 2 | $res_hr->{'seconds'} .= sprintf( | |||||||||
| 49 | "%06d", | ||||||||||
| 50 | sprintf( '%.6f', $stop_time->[1] ) - sprintf( '%.6f', $start_time->[1] ) | ||||||||||
| 51 | ); | ||||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 8 | $res_hr->{'op_cnt'} = $stop_ops_count - $start_ops_count; | |||||||||
| 55 | 8 | ▶ | if ($countops_lazy) { # non-import Devel::CountOps needs done at BEGIN time to work correctly | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 0 | $res_hr->{'op_cnt'} .= ' (Add -mDevel::CountOps to command (or use Devel::CountOps (); to code) to get a more accurate count!)'; | |||||||||
| 57 | } | ||||||||||
| 58 | 8 | $res_hr->{'rss_inc_bytes'} = $pid_pst->{RSS} - $pid_pre->{RSS}; | |||||||||
| 59 | |||||||||||
| 60 | 8 | $res_hr->{'sym_diff'} = $sym_pre->diff($sym_pst); | |||||||||
| 61 | |||||||||||
| 62 | 8 | $res_hr->{'sym_add'} = () = $res_hr->{'sym_diff'} =~ m/\+ /g; | |||||||||
| 63 | 8 | $res_hr->{'sym_rem'} = () = $res_hr->{'sym_diff'} =~ m/\- /g; | |||||||||
| 64 | |||||||||||
| 65 | 8 | my $dump = ''; | |||||||||
| 66 | |||||||||||
| 67 | 8 | ▶ | if ($dump_sym) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 4 | $dump = " ---- Begin Raw Diff --\n$res_hr->{'sym_diff'}\n ---- End Raw Diff --"; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 8 | return <<"END_CODE"; | |||||||||
| 72 | $code info: | ||||||||||
| 73 | Op Count: $res_hr->{'op_cnt'} | ||||||||||
| 74 | Seconds : $res_hr->{'seconds'} | ||||||||||
| 75 | RSS Grew: $res_hr->{'rss_inc_bytes'} bytes | ||||||||||
| 76 | Symbols : | ||||||||||
| 77 | Added : $res_hr->{'sym_add'} | ||||||||||
| 78 | Removed: $res_hr->{'sym_rem'} | ||||||||||
| 79 | $dump | ||||||||||
| 80 | END_CODE | ||||||||||
| 81 | } | ||||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 7 | 1; | |||||||||