Fast/Profiler.pm

filter: Statementstmt 98.7% 227 / 230 Branchbran 91.8% 68 / 74 Conditioncond 70.0% 14 / 20 MC/DCmcdc 46.1% 6 / 13 Subroutinesub 96.1% 25 / 26 Podpod 61.5% 8 / 13 totaltotal 92.5% 348 / 376 CC 50 SCAR 16.5
CC
50
Cov
95%
get_stats
12.0
_get_query_type
8.0
get_detailed_stats
6.1
CRAP 5.2
« Fast/Cache.pm ↑ Summary Fast/Result.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.20';
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 4 ▶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 44 ▶ sub _color (@args) {
Subroutine _color: called CC=2 SCAR=7.9
19 2 state $impl = eval { require Term::ANSIColor; \&Term::ANSIColor::color }
20 44 ▶ || 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
200
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 44 return $impl->(@args);
22 }
23  
24 2 ▶ sub _encode_json ($data) {
Subroutine _encode_json: called CC=2 SCAR=8.5
25 2 state $impl = eval { require Cpanel::JSON::XS; \&Cpanel::JSON::XS::encode_json }
26 2 ▶ || 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
200
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 2 return $impl->($data);
28 }
29 8 field $auto_print :param :accessor = 1;
30 8 ▶ field $queries :accessor = [];
Subroutine auto_print: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $auto_print = shift() 6 2 8
31 23 ▶ field $enabled :param :accessor = 1;
Subroutine queries: called CC=2 SCAR=7.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $queries = shift() 0 23 23
32 34 ▶ field $slow_query :param :accessor = 1.0;
Subroutine enabled: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $enabled = shift() 4 30 34
33 1 ▶ field $max_queries :param :accessor = 1000;
Subroutine slow_query: called CC=2 SCAR=7.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $slow_query = shift() 0 1 1
34  
35 61 ▶ 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 1 3
36 61 ▶ return unless $enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $enabled 1 60 61
37  
38 60 my $duration = $end_time - $start_time;
39 60 ▶ my $query_info = {
Branch: 1 branch
BranchTrueFalseTotal
$duration >= $slow_query ? : 2 58 60
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 60 ▶ if ( @{$queries} >= $max_queries ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$queries;} >= $max_queries) 4 56 60
53 4 shift @{$queries};
54 }
55  
56 60 push @{$queries}, $query_info;
57  
58 60 ▶ $self->print_query($query_info) if $auto_print;
Branch: 1 branch
BranchTrueFalseTotal
if $auto_print 4 56 60
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 6 ▶ method _severity ($duration) {
Subroutine _severity: called CC=3 SCAR=11.0
66 6 ▶ return $duration >= $slow_query ? 'slow'
Branch: 2 branches
BranchTrueFalseTotal
$duration >= $slow_query ? : 1 5 6
$duration >= $slow_query / 2 ? : 0 5 5
67 : $duration >= ( $slow_query / 2 ) ? 'warn'
68 : 'ok';
69 5 }
70  
71 6 ▶ method print_query ($query_info) {
Subroutine print_query: called CC=6 SCAR=17.9
Pod documented
72 6 my $severity = $self->_severity( $query_info->{duration} );
73  
74 # json/callback mode: emit the raw record and skip text rendering.
75 6 ▶ return if $self->emit( query => { %$query_info, severity => $severity } );
Branch: 1 branch
BranchTrueFalseTotal
if $self->emit("query", {%$query_info, "severity", $severity}) 2 4 6
76  
77 4 ▶ my $duration_color =
Branch: 2 branches
BranchTrueFalseTotal
$severity eq 'slow' ? : 1 3 4
$severity eq 'warn' ? : 0 3 3
78 $severity eq 'slow' ? 'red'
79 : $severity eq 'warn' ? 'yellow'
80 : 'green';
81  
82 4 $self->out( "\n", "=" x 80, "\n" );
83 $self->out( _color('bold blue'), "Query at ", $query_info->{time},
84 4 _color('reset'), "\n" );
85  
86 $self->out( _color('bold'), "Duration: ",
87 _color($duration_color),
88 4 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 4 ( my $safe_sql = $self->_format_sql( $query_info->{sql} ) )
94 =~ s/[^\P{Cc}\n\t]//g;
95 4 $self->out( _color('bold cyan'), "SQL: ", _color('reset'), $safe_sql, "\n" );
96  
97 4 ▶ if ( @{ $query_info->{params} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$query_info->{"params"};}) 2 2 4
98 $self->out( _color('bold magenta'), "Params: ", _color('reset'),
99 2 _encode_json( $query_info->{params} ), "\n" );
100 }
101  
102 4 ▶ if ( $query_info->{duration} >= $slow_query ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query_info->{"duration"} >= $slow_query) 1 3 4
103 $self->out( _color('red'), "Stack Trace:\n", _color('reset'),
104 1 $query_info->{stack}, "\n" );
105 }
106 5 }
107  
108 4 ▶ 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 4 state $kw_re = qr/\b(SELECT|FROM|WHERE|INSERT|UPDATE|DELETE|JOIN|ON|GROUP|BY|HAVING|ORDER|LIMIT)\b/;
112 4 $sql =~ s/$kw_re/\n$1/g;
113 4 return $sql;
114 5 }
115  
116 5 ▶ method get_detailed_stats () {
Subroutine get_detailed_stats: called CC=6 SCAR=18.0
Pod documented
117 5 my %stats;
118 5 for my $query ( @{$queries} ) {
119 17 my $type = $self->_get_query_type( $query->{sql} );
120  
121 17 $stats{$type}->{count}++;
122 17 $stats{$type}->{total_time} += $query->{duration};
123 $stats{$type}->{avg_time} =
124 17 $stats{$type}->{total_time} / $stats{$type}->{count};
125  
126 17 ▶ if ( !defined $stats{$type}->{min_time}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $stats{$type}{"min_time"} or $query->{"duration"} < $stats{$type}{"min_time"}) 16 1 17
Condition66%2/3
not defined $stats{$type}{"min_time"} or $query->{"duration"} < $stats{$type}{"min_time"}
l!l&&r!l&&!r
1601
MC/DC50%1/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 table66%2/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 16 $stats{$type}->{min_time} = $query->{duration};
130 }
131  
132 17 ▶ if ( !defined $stats{$type}->{max_time}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $stats{$type}{"max_time"} or $query->{"duration"} > $stats{$type}{"max_time"}) 17 0 17
Condition66%2/3
not defined $stats{$type}{"max_time"} or $query->{"duration"} > $stats{$type}{"max_time"}
l!l&&r!l&&!r
1610
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 table66%2/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 17 $stats{$type}->{max_time} = $query->{duration};
136 }
137 }
138  
139 5 return \%stats;
140 5 }
141  
142 40 ▶ method _get_query_type ($sql) {
Subroutine _get_query_type: called CC=8 SCAR=20.8
143 40 ▶ return 'SELECT' if $sql =~ /^\s*SELECT/i;
Branch: 1 branch
BranchTrueFalseTotal
if $sql =~ /^\s*SELECT/iu 20 20 40
144 20 ▶ return 'INSERT' if $sql =~ /^\s*INSERT/i;
Branch: 1 branch
BranchTrueFalseTotal
if $sql =~ /^\s*INSERT/iu 8 12 20
145 12 ▶ return 'UPDATE' if $sql =~ /^\s*UPDATE/i;
Branch: 1 branch
BranchTrueFalseTotal
if $sql =~ /^\s*UPDATE/iu 4 8 12
146 8 ▶ return 'DELETE' if $sql =~ /^\s*DELETE/i;
Branch: 1 branch
BranchTrueFalseTotal
if $sql =~ /^\s*DELETE/iu 4 4 8
147 4 ▶ return 'CREATE' if $sql =~ /^\s*CREATE/i;
Branch: 1 branch
BranchTrueFalseTotal
if $sql =~ /^\s*CREATE/iu 1 3 4
148 3 ▶ return 'ALTER' if $sql =~ /^\s*ALTER/i;
Branch: 1 branch
BranchTrueFalseTotal
if $sql =~ /^\s*ALTER/iu 1 2 3
149 2 ▶ return 'DROP' if $sql =~ /^\s*DROP/i;
Branch: 1 branch
BranchTrueFalseTotal
if $sql =~ /^\s*DROP/iu 1 1 2
150 1 return 'OTHER';
151 5 }
152  
153 2 ▶ method print_summary () {
Subroutine print_summary: called CC=3 SCAR=11.0
Pod documented
154 2 my $stats = $self->get_detailed_stats();
155 2 ▶ return if $self->emit( summary => $stats );
Branch: 1 branch
BranchTrueFalseTotal
if $self->emit("summary", $stats) 1 1 2
156  
157  
158 1 $self->out( "\n", "=" x 80, "\n" );
159 1 $self->out( _color('bold'), "Query Statistics Summary\n", _color('reset') );
160 1 $self->out( "=" x 80, "\n" );
161  
162 1 for my $type ( sort keys %$stats ) {
163 2 $self->out( _color('bold blue'), "\n$type Queries:\n", _color('reset') );
164 2 $self->outf( " Count: %d\n", $stats->{$type}->{count} );
165 2 $self->outf( " Total Time: %.6fs\n", $stats->{$type}->{total_time} );
166 2 $self->outf( " Avg Time: %.6fs\n", $stats->{$type}->{avg_time} );
167 2 $self->outf( " Min Time: %.6fs\n", $stats->{$type}->{min_time} );
168 2 $self->outf( " Max Time: %.6fs\n", $stats->{$type}->{max_time} );
169 }
170 5 }
171  
172 2 ▶ method _get_stack_trace () {
Subroutine _get_stack_trace: called CC=3 SCAR=11.0
173 2 my $trace = "";
174 2 my $i = 0;
175  
176 2 ▶ while ( my @caller = caller( $i++ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@caller) = caller $i++) 12 2 14
177 12 ▶ next if $caller[0] =~ /^DBIx::Fast/;
Branch: 1 branch
BranchTrueFalseTotal
if $caller[0] =~ /^DBIx::Fast/u 2 10 12
178 10 $trace .= sprintf( "%s line %d\n", $caller[1], $caller[2] );
179 }
180  
181 2 return $trace;
182 5 }
183  
184 18 ▶ method get_stats () {
Subroutine get_stats: called CC=12 SCAR=24.8
Pod documented
185 18 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 18 ▶ return $stats unless @{$queries};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$queries;} 2 16 18
198  
199 16 my $first_query_time = $queries->[0]->{start};
200 16 my $last_query_time = $queries->[-1]->{end};
201  
202 16 for my $query ( @{$queries} ) {
203 23 $stats->{total_queries}++;
204 23 $stats->{total_time} += $query->{duration};
205  
206 23 ▶ if ( !defined $stats->{min_time}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $stats->{"min_time"} or $query->{"duration"} < $stats->{"min_time"}) 19 4 23
Condition100%3/3
not defined $stats->{"min_time"} or $query->{"duration"} < $stats->{"min_time"}
l!l&&r!l&&!r
1634
MC/DC100%2/2
not defined $stats->{"min_time"} or $query->{"duration"} < $stats->{"min_time"}
not defined $stats->{"min_time"} $query->{"duration"} < $stats->{"min_time"}
Truth table100%3/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 19 $stats->{min_time} = $query->{duration};
210 }
211  
212 23 ▶ if ( $query->{duration} > $stats->{max_time} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query->{"duration"} > $stats->{"max_time"}) 19 4 23
213 19 $stats->{max_time} = $query->{duration};
214 }
215  
216 23 ▶ $stats->{slow_queries}++ if $query->{duration} > $slow_query;
Branch: 1 branch
BranchTrueFalseTotal
if $query->{"duration"} > $slow_query 1 22 23
217  
218 23 my $type = $self->_get_query_type( $query->{sql} );
219 23 ▶ $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
419
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 23 my $type_stats = $stats->{query_types}->{$type};
228 23 $type_stats->{count}++;
229 23 $type_stats->{total_time} += $query->{duration};
230  
231 23 ▶ if ( !defined $type_stats->{min_time}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $type_stats->{"min_time"} or $query->{"duration"} < $type_stats->{"min_time"}) 22 1 23
Condition100%3/3
not defined $type_stats->{"min_time"} or $query->{"duration"} < $type_stats->{"min_time"}
l!l&&r!l&&!r
1931
MC/DC100%2/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 table100%3/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 22 $type_stats->{min_time} = $query->{duration};
235 }
236  
237 23 ▶ if ( $query->{duration} > $type_stats->{max_time} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query->{"duration"} > $type_stats->{"max_time"}) 20 3 23
238 20 $type_stats->{max_time} = $query->{duration};
239 }
240  
241 $type_stats->{avg_time} =
242 23 $type_stats->{total_time} / $type_stats->{count};
243 }
244  
245 16 $stats->{avg_time} = $stats->{total_time} / $stats->{total_queries};
246  
247 16 my $total_elapsed = $last_query_time - $first_query_time;
248 16 ▶ if ( $total_elapsed > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total_elapsed > 0) 16 0 16
249 $stats->{queries_per_second} =
250 16 $stats->{total_queries} / $total_elapsed;
251 }
252  
253 16 $stats->{last_query_time} = $last_query_time;
254  
255 $stats->{slow_query_percentage} =
256 16 ( $stats->{slow_queries} / $stats->{total_queries} ) * 100;
257  
258 16 return $stats;
259 5 }
260  
261 5 ▶ method print_stats () {
Subroutine print_stats: called CC=3 SCAR=11.0
Pod documented
262 5 my $stats = $self->get_stats();
263 5 ▶ return if $self->emit( stats => $stats );
Branch: 1 branch
BranchTrueFalseTotal
if $self->emit("stats", $stats) 4 1 5
264  
265  
266 1 $self->out( "\n", "=" x 80, "\n" );
267 1 $self->out( _color('bold'), "Query Statistics\n", _color('reset') );
268 1 $self->out( "=" x 80, "\n\n" );
269  
270 1 $self->outf( "Total Queries: %d\n", $stats->{total_queries} );
271 1 $self->outf( "Total Time: %.4fs\n", $stats->{total_time} );
272 1 $self->outf( "Average Time: %.4fs\n", $stats->{avg_time} );
273 1 $self->outf( "Min Time: %.4fs\n", $stats->{min_time} );
274 1 $self->outf( "Max Time: %.4fs\n", $stats->{max_time} );
275 $self->outf( "Slow Queries: %d (%.2f%%)\n",
276 $stats->{slow_queries},
277 1 $stats->{slow_query_percentage} );
278 1 $self->outf( "Queries/second: %.2f\n", $stats->{queries_per_second} );
279  
280 1 $self->out( "\nBy Query Type:\n" );
281 1 $self->out( "-" x 40, "\n" );
282  
283 1 for my $type ( sort keys %{ $stats->{query_types} } ) {
284 1 my $type_stats = $stats->{query_types}->{$type};
285 1 $self->out( _color('bold blue'), "\n$type:\n", _color('reset') );
286 1 $self->outf( " Count: %d\n", $type_stats->{count} );
287 1 $self->outf( " Total Time: %.4fs\n", $type_stats->{total_time} );
288 1 $self->outf( " Avg Time: %.4fs\n", $type_stats->{avg_time} );
289 1 $self->outf( " Min Time: %.4fs\n", $type_stats->{min_time} );
290 1 $self->outf( " Max Time: %.4fs\n", $type_stats->{max_time} );
291 }
292 5 }
293  
294 2 ▶ method get_slow_queries ($limit = 10) {
Subroutine get_slow_queries: called CC=3 SCAR=11.0
Pod documented
295 my @sorted =
296 2 sort { $b->{duration} <=> $a->{duration} } @{$queries};
297  
298 2 ▶ $limit = scalar @sorted if $limit > scalar @sorted;
Branch: 1 branch
BranchTrueFalseTotal
if $limit > scalar @sorted 1 1 2
299 2 return [ @sorted[ 0 .. ( $limit - 1 ) ] ];
300 5 }
301  
302 16 ▶ 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/Result.pm »