lib/Catalyst/Stats.pm

filter: Statementstmt 98.9% 94 / 95 Branchbran 85.7% 36 / 42 Conditioncond 90.9% 10 / 11 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 19 / 19 Podpod 100% 7 / 7 totaltotal 95.0% 171 / 180 CC 27 SCAR 20.6
CC
27
Cov
95%
profile
16.1
__ANON__
8.0
report
3.0
CRAP 7.8
« lib/Catalyst/ClassData.pm ↑ Summary lib/Catalyst/ActionRole/QueryMatching.pm »
1 package Catalyst::Stats;
2  
3 155 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 155 ▶use Time::HiRes qw/gettimeofday tv_interval/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 155 ▶use Text::SimpleTable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 155 ▶use Catalyst::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 155 ▶use Tree::Simple qw/use_weak_refs/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 155 ▶use Tree::Simple::Visitor::FindByUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 155 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 940 ▶has enable => (is => 'rw', required => 1, default => sub{ 1 });
Subroutine __ANON__: called CC=1 SCAR=0.0
13 has tree => (
14 is => 'ro',
15 required => 1,
16 940 ▶ default => sub{ Tree::Simple->new({t => [gettimeofday]}) },
Subroutine __ANON__: called CC=1 SCAR=0.0
17 155 handles => [qw/ accept traverse /],
18 );
19 has stack => (
20 is => 'ro',
21 required => 1,
22 lazy => 1,
23 19 ▶ default => sub { [ shift->tree ] }
Subroutine __ANON__: called CC=1 SCAR=0.0
24 155 );
25  
26 76 ▶sub profile {
Subroutine profile: called CC=16 SCAR=27.8
Pod documented
27 76 my $self = shift;
28  
29 76 ▶ return unless $self->enable;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->enable 1 75 76
30  
31 75 my %params;
32 75 ▶ if (@_ <= 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ <= 1) { } 4 71 75
elsif (@_ % 2 != 0) { } 0 71 71
33 4 ▶ $params{comment} = shift || "";
Condition50%1/2
shift() || ''
l!l
40
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
34 }
35 elsif (@_ % 2 != 0) {
36 0 die "profile() requires a single comment parameter or a list of name-value pairs; found "
37 . (scalar @_) . " values: " . join(", ", @_);
38 }
39 else {
40 71 (%params) = @_;
41 71 ▶ $params{comment} ||= "";
Condition100%2/2
$params{"comment"} ||= ""
l!l
665
MC/DC100%1/1
$params{"comment"} ||= ""
$params{"comment"}
Truth table100%2/2
A
A= $params{"comment"}
Aresult
11
00
42 }
43  
44 75 my $parent;
45 my $prev;
46 75 my $t = [ gettimeofday ];
47 75 my $stack = $self->stack;
48  
49 75 ▶ if ($params{end}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"end"}) 33 42 75
50 # parent is on stack; search for matching block and splice out
51 33 ▶ for (my $i = $#{$stack}; $i > 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 37 0 37
52 37 ▶ if ($stack->[$i]->getNodeValue->{action} eq $params{end}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stack->[$i]->getNodeValue->{"action"} eq $params{"end"}) 33 4 37
53 33 my ($node) = splice(@{$stack}, $i, 1);
54 # Adjust elapsed on partner node
55 33 my $v = $node->getNodeValue;
56 33 $v->{elapsed} = tv_interval($v->{t}, $t);
57 33 return $node->getUID;
58 }
59 }
60 # if partner not found, fall through to treat as non-closing call
61 }
62 42 ▶ if ($params{parent}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"parent"}) 5 37 42
63 # parent is explicitly defined
64 5 $prev = $parent = $self->_get_uid($params{parent});
65 }
66 42 ▶ if (!$parent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent) 37 5 42
67 # Find previous node, which is either previous sibling or parent, for ref time.
68 37 ▶ $prev = $parent = $stack->[-1] or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$prev = $parent = $stack->[-1] or return undef 37 0 37
69 37 my $n = $parent->getChildCount;
70 37 ▶ $prev = $parent->getChild($n - 1) if $n > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n > 0 10 27 37
71 }
72  
73 my $node = Tree::Simple->new({
74 action => $params{begin} || "",
75 t => $t,
76 elapsed => tv_interval($prev->getNodeValue->{t}, $t),
77 comment => $params{comment},
78 42 ▶ });
Condition100%2/2
$params{'begin'} || ''
l!l
348
MC/DC100%1/1
$params{'begin'} || ''
$params{'begin'}
Truth table100%2/2
A
A= $params{'begin'}
Aresult
11
00
79 42 ▶ $node->setUID($params{uid}) if $params{uid};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"uid"} 30 12 42
80  
81 42 $parent->addChild($node);
82 42 ▶ push(@{$stack}, $node) if $params{begin};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"begin"} 34 8 42
83  
84 42 return $node->getUID;
85 }
86  
87 1 ▶sub created {
Subroutine created: called CC=1 SCAR=0.0
Pod documented
88 1 return @{ shift->{tree}->getNodeValue->{t} };
89 }
90  
91 16 ▶sub elapsed {
Subroutine elapsed: called CC=1 SCAR=0.0
Pod documented
92 16 return tv_interval(shift->{tree}->getNodeValue->{t});
93 }
94  
95 16 ▶sub report {
Subroutine report: called CC=3 SCAR=11.0
Pod documented
96 16 my $self = shift;
97  
98 16 my $t;
99 my @results;
100  
101 16 ▶ if (!wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
unless (wantarray) 15 1 16
102 15 $t = Text::SimpleTable->new(
103 [ Catalyst::Utils::term_width() - 9 - 13, 'Action' ],
104 [ 9, 'Time' ],
105 );
106 }
107  
108 39 ▶ $self->traverse(sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
109 39 my $action = shift;
110 39 my $stat = $action->getNodeValue;
111 my @r = ( $action->getDepth,
112 ($stat->{action} || "") .
113 ($stat->{action} && $stat->{comment} ? " " : "") . ($stat->{comment} ? '- ' . $stat->{comment} : ""),
114 $stat->{elapsed},
115 39 ▶ $stat->{action} ? 1 : 0,
Branch: 3 branches
BranchTrueFalseTotal
$stat->{'action'} && $stat->{'comment'} ? : 1 38 39
$stat->{'comment'} ? : 9 30 39
$stat->{'action'} ? : 31 8 39
Condition100%5/5
$stat->{'action'} || ''
l!l
318
$stat->{"action"} and $stat->{"comment"}
!ll&&!rl&&r
8301
MC/DC100%1/1
$stat->{'action'} || ''
$stat->{'action'}
MC/DC100%2/2
$stat->{"action"} and $stat->{"comment"}
$stat->{"action"} $stat->{"comment"}
Truth table100%2/2
A
A= $stat->{'action'}
Aresult
11
00
Truth table100%3/3
A and B
A= $stat->{"action"}B= $stat->{"comment"}
ABresult
0X0
100
111
116 );
117 # Trim down any times >= 10 to avoid ugly Text::Simple line wrapping
118 39 my $elapsed = substr(sprintf("%f", $stat->{elapsed}), 0, 8) . "s";
119 39 ▶ if ($t) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t) { } 28 11 39
120 28 ▶ $t->row( ( q{ } x $r[0] ) . $r[1],
Branch: 1 branch
BranchTrueFalseTotal
defined $r[2] ? : 28 0 28
121 defined $r[2] ? $elapsed : '??');
122 }
123 else {
124 11 push @results, \@r;
125 }
126 16 });
127 16 ▶ return wantarray ? @results : $t->draw;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 15 16
128 }
129  
130 5 ▶sub _get_uid {
Subroutine _get_uid: called CC=1 SCAR=0.0
131 5 my ($self, $uid) = @_;
132  
133 5 my $visitor = Tree::Simple::Visitor::FindByUID->new;
134 5 $visitor->searchForUID($uid);
135 5 $self->accept($visitor);
136 5 return $visitor->getResult;
137 }
138  
139 1 ▶sub addChild {
Subroutine addChild: called CC=2 SCAR=7.0
Pod documented
140 1 my $self = shift;
141 1 my $node = $_[ 0 ];
142  
143 1 my $stat = $node->getNodeValue;
144  
145 # do we need to fake $stat->{ t } ?
146 1 ▶ if( $stat->{ elapsed } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stat->{"elapsed"}) 1 0 1
147 # remove the "s" from elapsed time
148 1 $stat->{ elapsed } =~ s{s$}{};
149 }
150  
151 1 $self->tree->addChild( @_ );
152 }
153  
154 1 ▶sub setNodeValue {
Subroutine setNodeValue: called CC=2 SCAR=7.0
Pod documented
155 1 my $self = shift;
156 1 my $stat = $_[ 0 ];
157  
158 # do we need to fake $stat->{ t } ?
159 1 ▶ if( $stat->{ elapsed } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stat->{"elapsed"}) 1 0 1
160 # remove the "s" from elapsed time
161 1 $stat->{ elapsed } =~ s{s$}{};
162 }
163  
164 1 $self->tree->setNodeValue( @_ );
165 }
166  
167 1 ▶sub getNodeValue {
Subroutine getNodeValue: called CC=1 SCAR=0.0
Pod documented
168 1 my $self = shift;
169 1 $self->tree->getNodeValue( @_ )->{ t };
170 }
171  
172 155 __PACKAGE__->meta->make_immutable();
173  
174 155 1;
175  
176 __END__
« lib/Catalyst/ClassData.pm ↑ Summary lib/Catalyst/ActionRole/QueryMatching.pm »