blib/lib/DBIO/Storage/Statistics.pm

filter: Statementstmt 79.0% 79 / 100 Branchbran 50.0% 19 / 38 Conditioncond 53.3% 8 / 15 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 72.7% 16 / 22 Podpod 100% 12 / 12 totaltotal 69.3% 136 / 196 CC 26 SCAR 14.7
CC
26
Cov
69%
_build_debugfh
7.2
new
6.1
txn_begin
6.0
CRAP 4.3
« blib/lib/DBIO/Schema/Type.pm ↑ Summary blib/lib/DBIO/Replicated/DebugProxy.pm »
1 package DBIO::Storage::Statistics;
2 # ABSTRACT: SQL Statistics
3  
4 283 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 283 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 283 our $VERSION = '0.900007';
8  
9 283 ▶use DBIO::Util qw(sigwarn_silencer qsub);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 283 ▶use Fcntl qw(O_WRONLY O_APPEND O_CREAT);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 283 ▶use IO::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 283 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 283 ▶use base 'DBIO::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 283 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 283 __PACKAGE__->mk_group_accessors(simple => qw(
18 _defaulted_to_stderr
19 silence
20 callback
21 _query_start_time
22 last_query_elapsed
23 total_elapsed
24 query_count
25 ));
26  
27  
28 517 ▶sub new {
Subroutine new: called CC=5 SCAR=18.1
Pod documented
29 517 my $class = shift;
30 0 ▶ my %args = @_ == 1 && ref $_[0] eq 'HASH' ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'HASH' ? : 0 517 517
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
51700
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
31 517 ▶ my $self = bless {}, ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
05170
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
32 517 $self->{$_} = $args{$_} for grep { exists $args{$_} }
33 qw(_debugfh _defaulted_to_stderr silence callback);
34 517 $self->{total_elapsed} = 0;
35 517 $self->{query_count} = 0;
36 517 return $self;
37 }
38  
39 # FIXME - there ought to be a way to fold this into _debugfh itself
40 # having the undef re-trigger the builder (or better yet a default
41 # which can be folded in as a qsub)
42 78 ▶sub debugfh {
Subroutine debugfh: called CC=3 SCAR=11.0
Pod documented
43 78 my $self = shift;
44  
45 78 ▶ return $self->_debugfh(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 17 61 78
46 61 ▶ $self->_debugfh || $self->_build_debugfh;
Branch: 1 branch
BranchTrueFalseTotal
$self->_debugfh or $self->_build_debugfh 56 5 61
47 }
48  
49 78 ▶sub _debugfh {
Subroutine _debugfh: called CC=2 SCAR=6.9
50 78 my $self = shift;
51 78 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 17 61 78
52 17 $self->{_debugfh} = $_[0];
53 17 $self->_defaulted_to_stderr(undef);
54 17 return $_[0];
55 }
56 61 return $self->{_debugfh};
57 }
58  
59 5 ▶sub _build_debugfh {
Subroutine _build_debugfh: called CC=5 SCAR=19.7
60 5 my $fh;
61  
62 5 my $debug_env = $ENV{DBIO_TRACE};
63  
64 5 ▶ if (defined($debug_env) and ($debug_env =~ /=(.+)$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $debug_env and $debug_env =~ /=(.+)$/) { } 5 0 5
Condition33%1/3
defined $debug_env and $debug_env =~ /=(.+)$/
!ll&&!rl&&r
005
MC/DC0%0/2
defined $debug_env and $debug_env =~ /=(.+)$/
defined $debug_env $debug_env =~ /=(.+)$/
Truth table33%1/3
A and B
A= defined $debug_envB= $debug_env =~ /=(.+)$/
ABresult
0X0
100
111
65 # A trace file is opened for append and created for its owner alone (0600)
66 5 my $file = $1;
67 5 ▶ sysopen ($fh, $file, O_WRONLY | O_APPEND | O_CREAT, 0600)
Branch: 1 branch
BranchTrueFalseTotal
sysopen $fh, $file, 1089, 384 or die "Cannot open trace file ${file}: $!\n" 5 0 5
68 or die("Cannot open trace file $file: $!\n");
69 }
70 else {
71 0 ▶ open ($fh, '>&STDERR')
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">&STDERR" or die "Duplication of STDERR for debug output failed (perhaps your STDERR is closed?): $!\n" 0 0 0
72 or die("Duplication of STDERR for debug output failed (perhaps your STDERR is closed?): $!\n");
73 0 $_[0]->_defaulted_to_stderr(1);
74 }
75  
76 5 $fh->autoflush(1);
77  
78 5 $fh;
79 }
80  
81  
82 59 ▶sub print {
Subroutine print: called CC=3 SCAR=11.3
Pod documented
83 59 my ($self, $msg) = @_;
84  
85 59 ▶ return if $self->silence;
Branch: 1 branch
BranchTrueFalseTotal
if $self->silence 0 59 59
86  
87 59 my $fh = $self->debugfh;
88  
89 # not using 'no warnings' here because all of this can change at runtime
90 59 ▶ local $SIG{__WARN__} = sigwarn_silencer(qr/^Wide character in print/)
Branch: 1 branch
BranchTrueFalseTotal
if $self->_defaulted_to_stderr 0 59 59
91 if $self->_defaulted_to_stderr;
92  
93 59 $fh->print($msg);
94 }
95  
96 0 ▶sub txn_begin {
Subroutine txn_begin: not called CC=2 SCAR=17.9
Pod documented
97 0 my $self = shift;
98  
99 0 ▶ return if $self->callback;
Branch: 1 branch
BranchTrueFalseTotal
if $self->callback 0 0 0
100  
101 0 $self->print("BEGIN WORK\n");
102 }
103  
104 0 ▶sub txn_rollback {
Subroutine txn_rollback: not called CC=2 SCAR=17.9
Pod documented
105 0 my $self = shift;
106  
107 0 ▶ return if $self->callback;
Branch: 1 branch
BranchTrueFalseTotal
if $self->callback 0 0 0
108  
109 0 $self->print("ROLLBACK\n");
110 }
111  
112 0 ▶sub txn_commit {
Subroutine txn_commit: not called CC=2 SCAR=17.9
Pod documented
113 0 my $self = shift;
114  
115 0 ▶ return if $self->callback;
Branch: 1 branch
BranchTrueFalseTotal
if $self->callback 0 0 0
116  
117 0 $self->print("COMMIT\n");
118 }
119  
120 0 ▶sub svp_begin {
Subroutine svp_begin: not called CC=2 SCAR=17.9
Pod documented
121 0 my ($self, $name) = @_;
122  
123 0 ▶ return if $self->callback;
Branch: 1 branch
BranchTrueFalseTotal
if $self->callback 0 0 0
124  
125 0 $self->print("SAVEPOINT $name\n");
126 }
127  
128 0 ▶sub svp_release {
Subroutine svp_release: not called CC=2 SCAR=17.9
Pod documented
129 0 my ($self, $name) = @_;
130  
131 0 ▶ return if $self->callback;
Branch: 1 branch
BranchTrueFalseTotal
if $self->callback 0 0 0
132  
133 0 $self->print("RELEASE SAVEPOINT $name\n");
134 }
135  
136 0 ▶sub svp_rollback {
Subroutine svp_rollback: not called CC=2 SCAR=17.9
Pod documented
137 0 my ($self, $name) = @_;
138  
139 0 ▶ return if $self->callback;
Branch: 1 branch
BranchTrueFalseTotal
if $self->callback 0 0 0
140  
141 0 $self->print("ROLLBACK TO SAVEPOINT $name\n");
142 }
143  
144 6924 ▶sub query_start {
Subroutine query_start: called CC=3 SCAR=11.0
Pod documented
145 6924 my ($self, $string, @bind) = @_;
146  
147 6924 $self->_query_start_time(Time::HiRes::time());
148  
149 # @bind is only populated when debug output is enabled
150 6924 ▶ return unless @bind;
Branch: 1 branch
BranchTrueFalseTotal
unless @bind 6869 55 6924
151  
152 55 my $message = "$string: ".join(', ', @bind)."\n";
153  
154 55 ▶ if(defined($self->callback)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->callback) 11 44 55
155 11 $string =~ m/^(\w+)/;
156 11 $self->callback->($1, $message);
157 11 return;
158 }
159  
160 44 $self->print($message);
161 }
162  
163  
164 6886 ▶sub query_end {
Subroutine query_end: called CC=5 SCAR=16.1
Pod documented
165 6886 my ($self, $string, @bind) = @_;
166  
167 6886 my $start = $self->_query_start_time;
168 6886 ▶ return unless defined $start;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $start 7 6879 6886
169  
170 6879 my $elapsed = Time::HiRes::time() - $start;
171 6879 $self->last_query_elapsed($elapsed);
172 6879 $self->{total_elapsed} += $elapsed;
173 6879 $self->{query_count}++;
174 6879 $self->_query_start_time(undef);
175  
176 # only print elapsed when debug output is active
177 6879 ▶ if (@bind && !$self->silence && !defined($self->callback)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bind and not $self->silence and not defined $self->callback) 7 6872 6879
Condition83%5/6
@bind and not $self->silence and not defined $self->callback
!ll&&!rl&&r
686577
@bind and not $self->silence
!ll&&!rl&&r
6865014
MC/DC66%2/3
@bind and not $self->silence and not defined $self->callback
@bind not $self->silence not defined $self->callback
Truth table75%3/4
A and not(B) and not(C)
A= @bindB= not $self->silenceC= not defined $self->callback
ABCresult
0XX0
10X0
1100
1111
178 7 $self->print(sprintf(" Elapsed: %.6fs\n", $elapsed));
179 }
180 }
181  
182  
183 1 ▶sub reset_stats {
Subroutine reset_stats: called CC=1 SCAR=0.0
Pod documented
184 1 my $self = shift;
185 1 $self->{query_count} = 0;
186 1 $self->{total_elapsed} = 0;
187 1 $self->{last_query_elapsed} = undef;
188 }
189  
190  
191 283 1;
192  
193 __END__
« blib/lib/DBIO/Schema/Type.pm ↑ Summary blib/lib/DBIO/Replicated/DebugProxy.pm »