Fast/Profiler.pm

filter: Statementstmt 74.3% 171 / 230 Branchbran 55.4% 41 / 74 Conditioncond 50.0% 10 / 20 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 84.6% 22 / 26 Podpod 61.5% 8 / 13 totaltotal 67.2% 253 / 376 CC 50 SCAR 19.2
CC
50
Cov
69%
_get_query_type
12.8
get_stats
12.2
_get_stack_trace
12.0
CRAP 6.8
« Fast/Cache.pm ↑ Summary Fast/Cached.pm »
1 package DBIx::Fast::Profiler;
2  
3 5 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Object::Pad 0.807;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 our $VERSION = '0.22';
7 5 ▶use Time::HiRes 'time';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use DBIx::Fast::Output;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 # Cpanel::JSON::XS and Term::ANSIColor are optional (recommends): the _color /
10 # _encode_json helpers below load them on demand and fall back to colorless
11 # output and core JSON::PP when absent.
12  
13 5 ▶class DBIx::Fast::Profiler :isa(DBIx::Fast::Base) :does(DBIx::Fast::Output) {
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # Display helpers are runtime RECOMMENDS: text output must degrade
16 # gracefully on a bare install - core JSON::PP for params, colorless
17 # output without Term::ANSIColor. Resolved once, lazily.
18 9 ▶ sub _color (@args) {
Subroutine _color: called CC=2 SCAR=7.9
19 1 state $impl = eval { require Term::ANSIColor; \&Term::ANSIColor::color }
20 0 ▶ || sub { '' };
Subroutine __ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
eval { do { require Term::ANSIColor; \&Term::ANSIColor::color } } || sub { ''; }
l!l&&r!l&&!r
100
MC/DC0%0/2
eval { do { require Term::ANSIColor; \&Term::ANSIColor::color } } || sub { ''; }
eval { do { require Term::ANSIColor; \&Term::ANSIColor::color } } sub { ''; }
Truth table33%1/3
A || B
A= eval { do { require Term::ANSIColor; \&Term::ANSIColor::color } }B= sub { ''; }
ABresult
1X1
011
000
21 9 return $impl->(@args);
22 }
23  
24 1 ▶ sub _encode_json ($data) {
Subroutine _encode_json: called CC=2 SCAR=8.5
25 1 state $impl = eval { require Cpanel::JSON::XS; \&Cpanel::JSON::XS::encode_json }
26 0 ▶ || do { require JSON::PP; \&JSON::PP::encode_json };
Condition33%1/3
eval { do { require Cpanel::JSON::XS; \&Cpanel::JSON::XS::encode_json } } || do { require JSON::PP; \&JSON::PP::encode_json }
l!l&&r!l&&!r
100
MC/DC0%0/2
eval { do { require Cpanel::JSON::XS; \&Cpanel::JSON::XS::encode_json } } || do { require JSON::PP; \&JSON::PP::encode_json }
eval { do { require Cpanel::JSON::XS; \&Cpanel::JSON::XS::encode_json } } do { require JSON::PP; \&JSON::PP::encode_json }
Truth table33%1/3
A || B
A= eval { do { require Cpanel::JSON::XS; \&Cpanel::JSON::XS::encode_json } }B= do { require JSON::PP; \&JSON::PP::encode_json }
ABresult
1X1
011
000
27 1 return $impl->($data);
28 }
29 7 field $auto_print :param :accessor = 1;
30 7 ▶ field $queries :accessor = [];
Subroutine auto_print: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $auto_print = shift() 6 1 7
31 16 ▶ field $enabled :param :accessor = 1;
Subroutine queries: called CC=2 SCAR=7.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $queries = shift() 0 16 16
32 0 ▶ field $slow_query :param :accessor = 1.0;
Subroutine enabled: called CC=2 SCAR=7.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $enabled = shift() 2 31 33
33 0 ▶ field $max_queries :param :accessor = 1000;
Subroutine slow_query: not called CC=2 SCAR=10.5
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $slow_query = shift() 0 0 0
34  
35 30 ▶ method add_query ($sql, $params, $start_time, $end_time) {
Subroutine add_query: called CC=5 SCAR=16.1
Subroutine max_queries: called CC=2 SCAR=6.9
Pod documented
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $max_queries = shift() 2 0 2
36 30 ▶ return unless $enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $enabled 0 30 30
37  
38 30 my $duration = $end_time - $start_time;
39 30 ▶ my $query_info = {
Branch: 1 branch
BranchTrueFalseTotal
$duration >= $slow_query ? : 0 30 30
40 sql => $sql,
41 params => $params,
42 start => $start_time,
43 end => $end_time,
44 duration => $duration,
45 # The stack is only ever shown for slow queries (print_query), so
46 # only pay for the caller() walk then; '' (defined) otherwise.
47 stack => ( $duration >= $slow_query )
48 ? $self->_get_stack_trace() : '',
49 time => scalar( localtime($start_time) )
50 };
51  
52 30 ▶ if ( @{$queries} >= $max_queries ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$queries;} >= $max_queries) 2 28 30
53 2 shift @{$queries};
54 }
55  
56 30 push @{$queries}, $query_info;
57  
58 30 ▶ $self->print_query($query_info) if $auto_print;
Branch: 1 branch
BranchTrueFalseTotal
if $auto_print 3 27 30
59 5 }
60  
61 # Envelope identity for DBIx::Fast::Output events.
62 7 ▶ method _output_meta () { { source => 'tracker' } }
Subroutine _output_meta: called CC=1 SCAR=0.0
63  
64 # slow/warn/ok mirror the red/yellow/green duration buckets.
65 3 ▶ method _severity ($duration) {
Subroutine _severity: called CC=3 SCAR=11.3
66 3 ▶ return $duration >= $slow_query ? 'slow'
Branch: 2 branches
BranchTrueFalseTotal
$duration >= $slow_query ? : 0 3 3
$duration >= $slow_query / 2 ? : 0 3 3
67 : $duration >= ( $slow_query / 2 ) ? 'warn'
68 : 'ok';
69 5 }
70  
71 3 ▶ method print_query ($query_info) {
Subroutine print_query: called CC=6 SCAR=18.3
Pod documented
72 3 my $severity = $self->_severity( $query_info->{duration} );
73  
74 # json/callback mode: emit the raw record and skip text rendering.
75 3 ▶ return if $self->emit( query => { %$query_info, severity => $severity } );
Branch: 1 branch
BranchTrueFalseTotal
if $self->emit("query", {%$query_info, "severity", $severity}) 2 1 3
76  
77 1 ▶ my $duration_color =
Branch: 2 branches
BranchTrueFalseTotal
$severity eq 'slow' ? : 0 1 1
$severity eq 'warn' ? : 0 1 1
78 $severity eq 'slow' ? 'red'
79 : $severity eq 'warn' ? 'yellow'
80 : 'green';
81  
82 1 $self->out( "\n", "=" x 80, "\n" );
83 $self->out( _color('bold blue'), "Query at ", $query_info->{time},
84 1 _color('reset'), "\n" );
85  
86 $self->out( _color('bold'), "Duration: ",
87 _color($duration_color),
88 1 sprintf( "%.6fs", $query_info->{duration} ),
89 _color('reset'), "\n" );
90  
91 # Strip control characters (except \n/\t) before printing: SQL text can
92 # embed ANSI escapes that would manipulate the reader's terminal.
93 1 ( my $safe_sql = $self->_format_sql( $query_info->{sql} ) )
94 =~ s/[^\P{Cc}\n\t]//g;
95 1 $self->out( _color('bold cyan'), "SQL: ", _color('reset'), $safe_sql, "\n" );
96  
97 1 ▶ if ( @{ $query_info->{params} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$query_info->{"params"};}) 1 0 1
98 $self->out( _color('bold magenta'), "Params: ", _color('reset'),
99 1 _encode_json( $query_info->{params} ), "\n" );
100 }
101  
102 1 ▶ if ( $query_info->{duration} >= $slow_query ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query_info->{"duration"} >= $slow_query) 0 1 1
103 $self->out( _color('red'), "Stack Trace:\n", _color('reset'),
104 0 $query_info->{stack}, "\n" );
105 }
106 5 }
107  
108 1 ▶ method _format_sql ($sql) {
Subroutine _format_sql: called CC=1 SCAR=0.0
109 # Single precompiled regex with alternation - avoids recompiling
110 # one regex per keyword on every call.
111 1 state $kw_re = qr/\b(SELECT|FROM|WHERE|INSERT|UPDATE|DELETE|JOIN|ON|GROUP|BY|HAVING|ORDER|LIMIT)\b/;
112 1 $sql =~ s/$kw_re/\n$1/g;
113 1 return $sql;
114 5 }
115  
116 3 ▶ method get_detailed_stats () {
Subroutine get_detailed_stats: called CC=6 SCAR=18.7
Pod documented
117 3 my %stats;
118 3 for my $query ( @{$queries} ) {
119 6 my $type = $self->_get_query_type( $query->{sql} );
120  
121 6 $stats{$type}->{count}++;
122 6 $stats{$type}->{total_time} += $query->{duration};
123 $stats{$type}->{avg_time} =
124 6 $stats{$type}->{total_time} / $stats{$type}->{count};
125  
126 6 ▶ if ( !defined $stats{$type}->{min_time}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $stats{$type}{"min_time"} or $query->{"duration"} < $stats{$type}{"min_time"}) 6 0 6
Condition33%1/3
not defined $stats{$type}{"min_time"} or $query->{"duration"} < $stats{$type}{"min_time"}
l!l&&r!l&&!r
600
MC/DC0%0/2
not defined $stats{$type}{"min_time"} or $query->{"duration"} < $stats{$type}{"min_time"}
not defined $stats{$type}{"min_time"} $query->{"duration"} < $stats{$type}{"min_time"}
Truth table33%1/3
not(A) or B
A= not defined $stats{$type}{"min_time"}B= $query->{"duration"} < $stats{$type}{"min_time"}
ABresult
1X1
011
000
127 || $query->{duration} < $stats{$type}->{min_time} )
128 {
129 6 $stats{$type}->{min_time} = $query->{duration};
130 }
131  
132 6 ▶ if ( !defined $stats{$type}->{max_time}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $stats{$type}{"max_time"} or $query->{"duration"} > $stats{$type}{"max_time"}) 6 0 6
Condition33%1/3
not defined $stats{$type}{"max_time"} or $query->{"duration"} > $stats{$type}{"max_time"}
l!l&&r!l&&!r
600
MC/DC0%0/2
not defined $stats{$type}{"max_time"} or $query->{"duration"} > $stats{$type}{"max_time"}
not defined $stats{$type}{"max_time"} $query->{"duration"} > $stats{$type}{"max_time"}
Truth table33%1/3
not(A) or B
A= not defined $stats{$type}{"max_time"}B= $query->{"duration"} > $stats{$type}{"max_time"}
ABresult
1X1
011
000
133 || $query->{duration} > $stats{$type}->{max_time} )
134 {
135 6 $stats{$type}->{max_time} = $query->{duration};
136 }
137 }
138  
139 3 return \%stats;
140 5 }
141  
142 22 ▶ method _get_query_type ($sql) {
Subroutine _get_query_type: called CC=8 SCAR=25.5
143 22 ▶ return 'SELECT' if $sql =~ /^\s*SELECT/i;
Branch: 1 branch
BranchTrueFalseTotal
if $sql =~ /^\s*SELECT/iu 13 9 22
144 9 ▶ return 'INSERT' if $sql =~ /^\s*INSERT/i;
Branch: 1 branch
BranchTrueFalseTotal
if $sql =~ /^\s*INSERT/iu 5 4 9
145 4 ▶ return 'UPDATE' if $sql =~ /^\s*UPDATE/i;
Branch: 1 branch
BranchTrueFalseTotal
if $sql =~ /^\s*UPDATE/iu 2 2 4
146 2 ▶ return 'DELETE' if $sql =~ /^\s*DELETE/i;
Branch: 1 branch
BranchTrueFalseTotal
if $sql =~ /^\s*DELETE/iu 2 0 2
147 0 ▶ return 'CREATE' if $sql =~ /^\s*CREATE/i;
Branch: 1 branch
BranchTrueFalseTotal
if $sql =~ /^\s*CREATE/iu 0 0 0
148 0 ▶ return 'ALTER' if $sql =~ /^\s*ALTER/i;
Branch: 1 branch
BranchTrueFalseTotal
if $sql =~ /^\s*ALTER/iu 0 0 0
149 0 ▶ return 'DROP' if $sql =~ /^\s*DROP/i;
Branch: 1 branch
BranchTrueFalseTotal
if $sql =~ /^\s*DROP/iu 0 0 0
150 0 return 'OTHER';
151 5 }
152  
153 1 ▶ method print_summary () {
Subroutine print_summary: called CC=3 SCAR=16.9
Pod documented
154 1 my $stats = $self->get_detailed_stats();
155 1 ▶ return if $self->emit( summary => $stats );
Branch: 1 branch
BranchTrueFalseTotal
if $self->emit("summary", $stats) 1 0 1
156  
157  
158 0 $self->out( "\n", "=" x 80, "\n" );
159 0 $self->out( _color('bold'), "Query Statistics Summary\n", _color('reset') );
160 0 $self->out( "=" x 80, "\n" );
161  
162 0 for my $type ( sort keys %$stats ) {
163 0 $self->out( _color('bold blue'), "\n$type Queries:\n", _color('reset') );
164 0 $self->outf( " Count: %d\n", $stats->{$type}->{count} );
165 0 $self->outf( " Total Time: %.6fs\n", $stats->{$type}->{total_time} );
166 0 $self->outf( " Avg Time: %.6fs\n", $stats->{$type}->{avg_time} );
167 0 $self->outf( " Min Time: %.6fs\n", $stats->{$type}->{min_time} );
168 0 $self->outf( " Max Time: %.6fs\n", $stats->{$type}->{max_time} );
169 }
170 5 }
171  
172 0 ▶ method _get_stack_trace () {
Subroutine _get_stack_trace: not called CC=3 SCAR=24.8
173 0 my $trace = "";
174 0 my $i = 0;
175  
176 0 ▶ while ( my @caller = caller( $i++ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@caller) = caller $i++) 0 0 0
177 0 ▶ next if $caller[0] =~ /^DBIx::Fast/;
Branch: 1 branch
BranchTrueFalseTotal
if $caller[0] =~ /^DBIx::Fast/u 0 0 0
178 0 $trace .= sprintf( "%s line %d\n", $caller[1], $caller[2] );
179 }
180  
181 0 return $trace;
182 5 }
183  
184 14 ▶ method get_stats () {
Subroutine get_stats: called CC=12 SCAR=25.0
Pod documented
185 14 my $stats = {
186 total_queries => 0,
187 total_time => 0,
188 avg_time => 0,
189 min_time => undef,
190 max_time => 0,
191 slow_queries => 0,
192 query_types => {},
193 last_query_time => 0,
194 queries_per_second => 0
195 };
196  
197 14 ▶ return $stats unless @{$queries};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$queries;} 1 13 14
198  
199 13 my $first_query_time = $queries->[0]->{start};
200 13 my $last_query_time = $queries->[-1]->{end};
201  
202 13 for my $query ( @{$queries} ) {
203 16 $stats->{total_queries}++;
204 16 $stats->{total_time} += $query->{duration};
205  
206 16 ▶ if ( !defined $stats->{min_time}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $stats->{"min_time"} or $query->{"duration"} < $stats->{"min_time"}) 16 0 16
Condition66%2/3
not defined $stats->{"min_time"} or $query->{"duration"} < $stats->{"min_time"}
l!l&&r!l&&!r
1330
MC/DC0%0/2
not defined $stats->{"min_time"} or $query->{"duration"} < $stats->{"min_time"}
not defined $stats->{"min_time"} $query->{"duration"} < $stats->{"min_time"}
Truth table66%2/3
not(A) or B
A= not defined $stats->{"min_time"}B= $query->{"duration"} < $stats->{"min_time"}
ABresult
1X1
011
000
207 || $query->{duration} < $stats->{min_time} )
208 {
209 16 $stats->{min_time} = $query->{duration};
210 }
211  
212 16 ▶ if ( $query->{duration} > $stats->{max_time} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query->{"duration"} > $stats->{"max_time"}) 13 3 16
213 13 $stats->{max_time} = $query->{duration};
214 }
215  
216 16 ▶ $stats->{slow_queries}++ if $query->{duration} > $slow_query;
Branch: 1 branch
BranchTrueFalseTotal
if $query->{"duration"} > $slow_query 0 16 16
217  
218 16 my $type = $self->_get_query_type( $query->{sql} );
219 16 ▶ $stats->{query_types}->{$type} ||= {
Condition100%2/2
$stats->{"query_types"}{$type} ||= {"count", 0, "total_time", 0, "min_time", undef, "max_time", 0, "avg_time", 0}
l!l
313
MC/DC100%1/1
$stats->{"query_types"}{$type} ||= {"count", 0, "total_time", 0, "min_time", undef, "max_time", 0, "avg_time", 0}
$stats->{"query_types"}{$type}
Truth table100%2/2
A
A= $stats->{"query_types"}{$type}
Aresult
11
00
220 count => 0,
221 total_time => 0,
222 min_time => undef,
223 max_time => 0,
224 avg_time => 0
225 };
226  
227 16 my $type_stats = $stats->{query_types}->{$type};
228 16 $type_stats->{count}++;
229 16 $type_stats->{total_time} += $query->{duration};
230  
231 16 ▶ if ( !defined $type_stats->{min_time}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $type_stats->{"min_time"} or $query->{"duration"} < $type_stats->{"min_time"}) 16 0 16
Condition66%2/3
not defined $type_stats->{"min_time"} or $query->{"duration"} < $type_stats->{"min_time"}
l!l&&r!l&&!r
1330
MC/DC0%0/2
not defined $type_stats->{"min_time"} or $query->{"duration"} < $type_stats->{"min_time"}
not defined $type_stats->{"min_time"} $query->{"duration"} < $type_stats->{"min_time"}
Truth table66%2/3
not(A) or B
A= not defined $type_stats->{"min_time"}B= $query->{"duration"} < $type_stats->{"min_time"}
ABresult
1X1
011
000
232 || $query->{duration} < $type_stats->{min_time} )
233 {
234 16 $type_stats->{min_time} = $query->{duration};
235 }
236  
237 16 ▶ if ( $query->{duration} > $type_stats->{max_time} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query->{"duration"} > $type_stats->{"max_time"}) 13 3 16
238 13 $type_stats->{max_time} = $query->{duration};
239 }
240  
241 $type_stats->{avg_time} =
242 16 $type_stats->{total_time} / $type_stats->{count};
243 }
244  
245 13 $stats->{avg_time} = $stats->{total_time} / $stats->{total_queries};
246  
247 13 my $total_elapsed = $last_query_time - $first_query_time;
248 13 ▶ if ( $total_elapsed > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total_elapsed > 0) 13 0 13
249 $stats->{queries_per_second} =
250 13 $stats->{total_queries} / $total_elapsed;
251 }
252  
253 13 $stats->{last_query_time} = $last_query_time;
254  
255 $stats->{slow_query_percentage} =
256 13 ( $stats->{slow_queries} / $stats->{total_queries} ) * 100;
257  
258 13 return $stats;
259 5 }
260  
261 4 ▶ method print_stats () {
Subroutine print_stats: called CC=3 SCAR=20.0
Pod documented
262 4 my $stats = $self->get_stats();
263 4 ▶ return if $self->emit( stats => $stats );
Branch: 1 branch
BranchTrueFalseTotal
if $self->emit("stats", $stats) 4 0 4
264  
265  
266 0 $self->out( "\n", "=" x 80, "\n" );
267 0 $self->out( _color('bold'), "Query Statistics\n", _color('reset') );
268 0 $self->out( "=" x 80, "\n\n" );
269  
270 0 $self->outf( "Total Queries: %d\n", $stats->{total_queries} );
271 0 $self->outf( "Total Time: %.4fs\n", $stats->{total_time} );
272 0 $self->outf( "Average Time: %.4fs\n", $stats->{avg_time} );
273 0 $self->outf( "Min Time: %.4fs\n", $stats->{min_time} );
274 0 $self->outf( "Max Time: %.4fs\n", $stats->{max_time} );
275 $self->outf( "Slow Queries: %d (%.2f%%)\n",
276 $stats->{slow_queries},
277 0 $stats->{slow_query_percentage} );
278 0 $self->outf( "Queries/second: %.2f\n", $stats->{queries_per_second} );
279  
280 0 $self->out( "\nBy Query Type:\n" );
281 0 $self->out( "-" x 40, "\n" );
282  
283 0 for my $type ( sort keys %{ $stats->{query_types} } ) {
284 0 my $type_stats = $stats->{query_types}->{$type};
285 0 $self->out( _color('bold blue'), "\n$type:\n", _color('reset') );
286 0 $self->outf( " Count: %d\n", $type_stats->{count} );
287 0 $self->outf( " Total Time: %.4fs\n", $type_stats->{total_time} );
288 0 $self->outf( " Avg Time: %.4fs\n", $type_stats->{avg_time} );
289 0 $self->outf( " Min Time: %.4fs\n", $type_stats->{min_time} );
290 0 $self->outf( " Max Time: %.4fs\n", $type_stats->{max_time} );
291 }
292 5 }
293  
294 0 ▶ method get_slow_queries ($limit = 10) {
Subroutine get_slow_queries: not called CC=3 SCAR=24.8
Pod documented
295 my @sorted =
296 0 sort { $b->{duration} <=> $a->{duration} } @{$queries};
297  
298 0 ▶ $limit = scalar @sorted if $limit > scalar @sorted;
Branch: 1 branch
BranchTrueFalseTotal
if $limit > scalar @sorted 0 0 0
299 0 return [ @sorted[ 0 .. ( $limit - 1 ) ] ];
300 5 }
301  
302 15 ▶ method clear () { $queries = [] }
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
303 }
304  
305 5 1;
306  
307 __END__
« Fast/Cache.pm ↑ Summary Fast/Cached.pm »