RTM/Scope.pm untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 109 CC 10 SCAR 20.1
CC
10
Cov
0%
new
12.0
stop
12.0
auto_key
12.0
CRAP 7.4
1 ##############################################################################
2 #
3 # Time::RTM::Scope
4 # 2024 (c) Vladi Belperchinov-Shabanski "Cade" <cade@noxrun.com>
5 #
6 # This is internal module, see Time::RTM for external API
7 #
8 # DISTRIBUTED UNDER GPLv2
9 #
10 ##############################################################################
11 package Time::RTM::Scope;
12 use strict;
13 use Time::HiRes;
14 use Carp;
15  
16 our $VERSION = '1.15';
17  
18 ##############################################################################
19  
20 sub new
21 {
22 my $class = shift;
23 my $rtm = shift; # controller bject
24 my @keys = @_;
25
26 carp( "second argument is expected to be Time::RTM" ) unless ref( $rtm ) eq 'Time::RTM';
27  
28 push @keys, '*' if @keys == 0;
29
30 $class = ref( $class ) || $class;
31 my $self = {
32 'RTM' => $rtm,
33 'KEYS' => \@keys,
34 };
35 bless $self, $class;
36
37 $self->restart();
38
39 return $self;
40 }
41  
42 sub restart
43 {
44 my $self = shift;
45
46 $self->{ 'START' } = Time::HiRes::time();
47 }
48  
49 # TODO: pause() cont()
50 sub split_str
51 {
52 my $self = shift;
53
54 return "n/a/n" unless $self->{ 'START' };
55 return $self->{ 'START' } . "/" . Time::HiRes::time() . "/" . (Time::HiRes::time() - $self->{ 'START' });
56 }
57  
58 sub split
59 {
60 my $self = shift;
61
62 return undef unless $self->{ 'START' };
63 return Time::HiRes::time() - $self->{ 'START' };
64 }
65  
66 sub stop
67 {
68 my $self = shift;
69  
70 carp( "scope timer is not started, use start() first" ) if $self->{ 'START' } == 0;
71  
72 my $et = Time::HiRes::time();
73 my $st = $self->{ 'START' };
74 my $dt = $et - $st;
75
76 my $rtm = $self->__rtm();
77
78 my $keys = $self->{ 'KEYS' };
79  
80 for my $key ( @$keys )
81 {
82 $key = $self->auto_key() if $key =~ /^\*?$/;
83 $rtm->__add_dt( $key, $st, $dt, $et );
84 }
85  
86 delete $self->{ 'START' };
87 }
88  
89 # rename/set new keys before finish
90 sub set_keys
91 {
92 my $self = shift;
93  
94 $self->{ 'KEYS' } = [ @_ ];
95 }
96  
97  
98 sub DESTROY
99 {
100 my $self = shift;
101  
102 $self->stop() if $self->{ 'START' };
103 }
104  
105 sub auto_key
106 {
107 my $self = shift;
108  
109 my @key;
110 my $i = 0;
111 my $se = 1; # skip eval
112 while ( my ( $pack, $file, $line, $subname, $hasargs, $wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash ) = caller($i++) )
113 {
114 next if $subname =~ /^Time::RTM::/; # skip self
115 next if $subname eq '(eval)' and $se;
116 $se = 0;
117 push @key, "$subname";
118 }
119  
120 push @key, 'main::';
121
122 my $key = join '/', reverse @key;
123
124 return $key;
125 }
126  
127 ### INTERNAL #################################################################
128  
129 sub __rtm
130 {
131 my $self = shift;
132
133 return $self->{ 'RTM' };
134 }
135  
136 ##############################################################################
137  
138 =pod
139  
140 =head1 NAME
141  
142 Time::RTM::Scope is used internally by Time::RTM.
143 See Time::RTM manual.
144  
145 =head1 AUTHOR
146  
147 Vladi Belperchinov-Shabanski "Cade"
148  
149 <cade@noxrun.com> <cade@cpan.org>
150  
151 http://cade.noxrun.com
152  
153 =cut
154  
155 ##############################################################################
156 1;
157 ##############################################################################