| 1 | package DBIx::Class::Storage::Statistics::SimpleTable; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use 5.008005; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use version; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = '0.01_01'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | DBIx::Class::Storage::Statistics::SimpleTable - DBIC statistics in a table | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | use My::DBIC::Schema; | ||||||||||||||||||||||||||||
| 18 | use DBIx::Class::Storage::Statistics::SimpleTable; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | my $dbic_schema = My::DBIC::Schema->connect; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # using DBIx::Class::Storage::Statistics::SimpleTable as single debug object | ||||||||||||||||||||||||||||
| 23 | my $profiler = DBIx::Class::Storage::Statistics::SimpleTable->new(); | ||||||||||||||||||||||||||||
| 24 | $dbic_schema->storage->debugobj( $profiler ); | ||||||||||||||||||||||||||||
| 25 | $dbic_schema->debug( 1 ); | ||||||||||||||||||||||||||||
| 26 | # ... do stuff with your schema ... | ||||||||||||||||||||||||||||
| 27 | print $profiler->report->draw; | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # using DBIx::Class::Storage::Statistics::SimpleTable with delegation | ||||||||||||||||||||||||||||
| 30 | # to the original object and FirePHP::Dispatcher support | ||||||||||||||||||||||||||||
| 31 | my $profiler = DBIx::Class::Storage::Statistics::SimpleTable | ||||||||||||||||||||||||||||
| 32 | ->new( 'FirePHP::SimpleTable' ); | ||||||||||||||||||||||||||||
| 33 | $profiler->install( $dbic_schema ); | ||||||||||||||||||||||||||||
| 34 | # ... do stuff with your schema ... | ||||||||||||||||||||||||||||
| 35 | $firephp_dispatcher->table( 'DBIC Profile' => $profiler->report ); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | B<DBIx::Class::Storage::Statistics::SimpleTable> is a | ||||||||||||||||||||||||||||
| 40 | L<DBIx::Class::Storage::Statistics> subclass that gathers | ||||||||||||||||||||||||||||
| 41 | L<DBIx::Class> profiling information in a L<Text::SimpleTable> | ||||||||||||||||||||||||||||
| 42 | class or sub-class. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | This module was created to support L<Catalyst::Plugin::FirePHP> | ||||||||||||||||||||||||||||
| 45 | but maybe some will find it useful one its own. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =cut | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1 | ▶ | use base qw/DBIx::Class::Storage::Statistics/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | use Time::HiRes qw/gettimeofday tv_interval/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 1 | ▶ | use MRO::Compat; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | 1 | ▶ | use Text::SimpleTable; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | 1 | ▶ | use Scalar::Util qw/weaken/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =head1 METHODS | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =head2 $class->new( ?$table_class ) | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | Creates a new statistics object, optionally using the | ||||||||||||||||||||||||||||
| 61 | L<Text::SimpleTable> sub-class given as argument | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 0 | my ( $class, $table_class ) = @_; | |||||||||||||||||||||||||||
| 67 | 0 | ▶ | $table_class ||= 'Text::SimpleTable'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$table_class ||= "Text::SimpleTable"
MC/DC0%0/1
$table_class ||= "Text::SimpleTable"
$table_class
Truth table0%0/2
A
A= $table_class
| |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | die "$table_class is no Text::SimpleTable" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | unless $table_class->isa('Text::SimpleTable'); | ||||||||||||||||||||||||||||
| 70 | 0 | my $self = $class->next::method(); | |||||||||||||||||||||||||||
| 71 | 0 | $self->{table_class} = $table_class; | |||||||||||||||||||||||||||
| 72 | 0 | $self->{total_time} = 0.0; | |||||||||||||||||||||||||||
| 73 | 0 | $self->{query_count} = 0; | |||||||||||||||||||||||||||
| 74 | 0 | return $self; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =head2 $self->install( $storage ) | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | Installs this statistics instance into a L<DBIx::Class::Storage> object. | ||||||||||||||||||||||||||||
| 80 | The original C<debugobj> and C<debug> state will be preserved and all | ||||||||||||||||||||||||||||
| 81 | statistic events delegated to the original object. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub install { | ||||||||||||||||||||||||||
|
Subroutine
install: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 0 | my ( $self, $storage ) = @_; | |||||||||||||||||||||||||||
| 87 | 0 | $self->{storage_object} = $storage; | |||||||||||||||||||||||||||
| 88 | 0 | weaken $self->{storage_object}; | |||||||||||||||||||||||||||
| 89 | 0 | $self->{storage_debugobj} = $storage->debugobj; | |||||||||||||||||||||||||||
| 90 | 0 | $storage->debugobj( $self ); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head2 $self->uninstall | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | Removes this statistics instance from the associated L<DBIx::Class::Storage> | ||||||||||||||||||||||||||||
| 96 | object and restores its original state. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =cut | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub uninstall { | ||||||||||||||||||||||||||
|
Subroutine
uninstall: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 102 | 0 | ▶ | return unless $self->{storage_object}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | $self->{storage_object}->debugobj( delete $self->{storage_debugobj} ); | |||||||||||||||||||||||||||
| 104 | 0 | $self->{storage_object}->debug( delete $self->{storage_debug} ); | |||||||||||||||||||||||||||
| 105 | 0 | delete $self->{storage_object}; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head1 INFORMATIONAL METHODS | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head2 $self->report | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Returns the gathered statistics as L<Text::SimpleTable> object | ||||||||||||||||||||||||||||
| 113 | (or as the sub-class choosen at object creation) | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =cut | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub report { | ||||||||||||||||||||||||||
|
Subroutine
report: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 119 | 0 | return $self->{report_table}; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head2 $self->elapsed_time | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | Returns the total time spend in L<DBIx::Class> queries. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =cut | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub elapsed_time { | ||||||||||||||||||||||||||
|
Subroutine
elapsed_time: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 130 | 0 | return $self->{total_time}; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head2 $self->query_count | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | Returns the number of queries processed. | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =cut | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | sub query_count { | ||||||||||||||||||||||||||
|
Subroutine
query_count: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 141 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 142 | 0 | return $self->{query_count}; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head1 OVERRIDDEN METHODS | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =head2 $self->query_start | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | Logs the starting time of the request | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =cut | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | sub query_start { | ||||||||||||||||||||||||||
|
Subroutine
query_start: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 154 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 155 | 0 | $self->{query_started} = [ gettimeofday() ]; | |||||||||||||||||||||||||||
| 156 | $self->{storage_debugobj}->query_start( @_ ) | ||||||||||||||||||||||||||||
| 157 | 0 | ▶ | if $self->{storage_debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head2 $self->query_end( $sql, @params ) | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Write the query string, parameters and elepsed time to the statistics table. | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =cut | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | ▶ | sub query_end { | ||||||||||||||||||||||||||
|
Subroutine
query_end: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 167 | 0 | my ( $self, $sql, @params ) = @_; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | $self->{total_time} += my $elapsed = sprintf( | ||||||||||||||||||||||||||||
| 170 | '%0.4f', tv_interval( delete $self->{query_started} ) | ||||||||||||||||||||||||||||
| 171 | 0 | ); | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | $self->{query_count} += 1; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | $self->{report_table} ||= $self->{table_class}->new( | ||||||||||||||||||||||||||||
| 176 | 0 | ▶ | [ 30, 'SQL' ], | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"report_table"} ||= $self->{"table_class"}->new([30, "SQL"], [28, "Params"], [9, "Time"])
MC/DC0%0/2
$self->{"report_table"} ||= $self->{"table_class"}->new([30, "SQL"], [28, "Params"], [9, "Time"])
$self->{"report_table"}
$self->{"table_class"}->new([30, "SQL"], [28, "Params"], [9, "Time"])
Truth table0%0/3
A ||= B
A= $self->{"report_table"}B= $self->{"table_class"}->new([30, "SQL"], [28, "Params"], [9, "Time"])
| |||||||||||||||||||||||||||||
| 177 | [ 28, 'Params' ], | ||||||||||||||||||||||||||||
| 178 | [ 9, 'Time' ], | ||||||||||||||||||||||||||||
| 179 | ); | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | ▶ | $self->report->row( $sql, join( ', ', @params ) || '', $elapsed ); | ||||||||||||||||||||||||||
|
Condition0%0/2
join(', ', @params) || ''
MC/DC0%0/1
join(', ', @params) || ''
join(', ', @params)
Truth table0%0/2
A
A= join(', ', @params)
| |||||||||||||||||||||||||||||
| 182 | $self->{storage_debugobj}->query_end( $sql, @params ) | ||||||||||||||||||||||||||||
| 183 | 0 | ▶ | if $self->{storage_debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 188 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 189 | 0 | $self->uninstall; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 1 | our $AUTOLOAD; | |||||||||||||||||||||||||||
| 193 | 0 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 194 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 0 | my $method = $AUTOLOAD; | |||||||||||||||||||||||||||
| 197 | 0 | $method =~ s/^.*://; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | croak( "Method $method not found" ) | ||||||||||||||||||||||||||||
| 200 | 0 | ▶ | unless $self and $self->{storage_debugobj}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self and $self->{"storage_debugobj"}
MC/DC0%0/2
$self and $self->{"storage_debugobj"}
$self
$self->{"storage_debugobj"}
Truth table0%0/3
A and B
A= $selfB= $self->{"storage_debugobj"}
| |||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | ▶ | return unless $self->{storage_debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 0 | ▶ | return $self->stats_object->$method(@_) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | if $self->stats_object->can( $method ); | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 0 | croak( "Method $method not found" ); | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 1 | 1; | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | __END__ | ||||||||||||||||||||||||||||