| 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
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 517 | ▶ | my $self = bless {}, ref $class || $class; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 61 | ▶ | $self->_debugfh || $self->_build_debugfh; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $debug_env and $debug_env =~ /=(.+)$/
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 =~ /=(.+)$/
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | or die("Cannot open trace file $file: $!\n"); | ||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | open ($fh, '>&STDERR') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 55 | my $message = "$string: ".join(', ', @bind)."\n"; | |||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | 55 | ▶ | if(defined($self->callback)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
@bind and not $self->silence and not defined $self->callback
@bind and not $self->silence
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||