Profiler/Scope.pm untested

filter: Statementstmt 0.0% 0 / 47 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 86 CC 8 SCAR 20.9
CC
8
Cov
0%
new
12.0
stop
12.0
auto_key
12.0
CRAP 8.1
1 ##############################################################################
2 #
3 # Time::Profiler::Scope
4 # Vladi Belperchinov-Shabanski "Cade" <cade@biscom.net> <cade@datamax.bg>
5 #
6 # This is internal module, see Time::Profiler for external API
7 #
8 # DISTRIBUTED UNDER GPLv2
9 #
10 ##############################################################################
11 package Time::Profiler::Scope;
12 use strict;
13 use Time::HR;
14 use Carp;
15  
16 ##############################################################################
17  
18 sub new
19 {
20 my $class = shift;
21 my $profiler = shift;
22 my @keys = @_;
23
24 carp( "second argument is expected to be Time::Profiler" ) unless ref( $profiler ) eq 'Time::Profiler';
25  
26 push @keys, '*' if @keys == 0;
27
28 $class = ref( $class ) || $class;
29 my $self = {
30 'PROFILER' => $profiler,
31 'KEYS' => \@keys,
32 };
33 bless $self, $class;
34 return $self;
35 }
36  
37 sub start
38 {
39 my $self = shift;
40
41 $self->{ 'START' } = gethrtime();
42 }
43  
44 sub stop
45 {
46 my $self = shift;
47  
48 carp( "scope timer is not started, use start() first" ) if $self->{ 'START' } == 0;
49  
50 my $dt = gethrtime() - $self->{ 'START' };
51
52 my $pr = $self->__pr();
53
54 my $keys = $self->{ 'KEYS' };
55  
56 for my $key ( @$keys )
57 {
58 $key = $self->auto_key() if $key =~ /^\*?$/;
59 $pr->__add_dt( $key, $dt );
60 }
61  
62 delete $self->{ 'START' };
63 }
64  
65 sub DESTROY
66 {
67 my $self = shift;
68  
69 $self->stop() if $self->{ 'START' };
70 }
71  
72 sub auto_key
73 {
74 my $self = shift;
75  
76 my @key;
77 my $i = 0;
78 my $se = 1; # skip eval
79 while ( my ( $pack, $file, $line, $subname, $hasargs, $wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash ) = caller($i++) )
80 {
81 next if $subname =~ /^Time::Profiler::/; # skip self
82 next if $subname eq '(eval)' and $se;
83 $se = 0;
84 push @key, "$subname";
85 }
86  
87 push @key, 'main::';
88
89 my $key = join '/', reverse @key;
90
91 return $key;
92 }
93  
94 ### INTERNAL #################################################################
95  
96 sub __pr
97 {
98 my $self = shift;
99
100 return $self->{ 'PROFILER' }
101 }
102  
103 ##############################################################################
104  
105 =pod
106  
107 =head1 NAME
108  
109 Time::Profiler::Scope is used internally by Time::Profiler.
110 See Time::Profiler manual.
111  
112 =head1 AUTHOR
113  
114 Vladi Belperchinov-Shabanski "Cade"
115  
116 <cade@biscom.net> <cade@datamax.bg> <cade@cpan.org>
117  
118 http://cade.datamax.bg
119  
120 =cut
121  
122 ##############################################################################
123 1;
124 ##############################################################################