Fast/Profiler.pm

filter: Statementstmt 71.3% 164 / 230 Branchbran 41.8% 31 / 74 Conditioncond 50.0% 10 / 20 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 80.7% 21 / 26 Podpod 61.5% 8 / 13 totaltotal 62.5% 235 / 376 CC 50 SCAR 22.5
CC
50
Cov
63%
_get_query_type
37.1
get_stats
12.2
_get_stack_trace
12.0
CRAP 9.5
« Fast/Connector.pm ↑ Summary Fast/Schema.pm »
1 package DBIx::Fast::Profiler;
2  
3 3 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Object::Pad 0.807;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = '0.21';
7 3 ▶use Time::HiRes 'time';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶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 2 ▶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 9 ▶ || 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 1 ▶ || 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 6 field $auto_print :param :accessor = 1;
30 6 ▶ field $queries :accessor = [];
Subroutine auto_print: called CC=2 SCAR=7.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $auto_print = shift() 6 0 6
31 4 ▶ field $enabled :param :accessor = 1;
Subroutine queries: called CC=2 SCAR=7.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $queries = shift() 0 4 4
32 5 ▶ field $slow_query :param :accessor = 1.0;
Subroutine enabled: called CC=2 SCAR=8.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $enabled = shift() 0 5 5
33 0 ▶ field $max_queries :param :accessor = 1000;
Subroutine slow_query: not called CC=2 SCAR=14.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $slow_query = shift() 0 0 0
34  
35 5 ▶ method add_query ($sql, $params, $start_time, $end_time) {
Subroutine add_query: called CC=5 SCAR=17.1
Subroutine max_queries: not called CC=2 SCAR=7.3
Pod documented
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $max_queries = shift() 0 0 0
36 5 ▶ return unless $enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $enabled 0 5 5
37  
38 5 my $duration = $end_time - $start_time;
39 5 ▶ my $query_info = {
Branch: 1 branch
BranchTrueFalseTotal
$duration >= $slow_query ? : 0 5 5
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 5 ▶ if ( @{$queries} >= $max_queries ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$queries;} >= $max_queries) 0 5 5
53 0 shift @{$queries};
54 }
55  
56 5 push @{$queries}, $query_info;
57  
58 5 ▶ $self->print_query($query_info) if $auto_print;
Branch: 1 branch
BranchTrueFalseTotal
if $auto_print 3 2 5
59 3 }
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 3 }
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 3 }
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 3 }
115  
116 2 ▶ method get_detailed_stats () {
Subroutine get_detailed_stats: called CC=6 SCAR=18.7
Pod documented
117 2 my %stats;
118 2 for my $query ( @{$queries} ) {
119 2 my $type = $self->_get_query_type( $query->{sql} );
120  
121 2 $stats{$type}->{count}++;
122 2 $stats{$type}->{total_time} += $query->{duration};
123 $stats{$type}->{avg_time} =
124 2 $stats{$type}->{total_time} / $stats{$type}->{count};
125  
126 2 ▶ if ( !defined $stats{$type}->{min_time}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $stats{$type}{"min_time"} or $query->{"duration"} < $stats{$type}{"min_time"}) 2 0 2
Condition33%1/3
not defined $stats{$type}{"min_time"} or $query->{"duration"} < $stats{$type}{"min_time"}
l!l&&r!l&&!r
200
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 2 $stats{$type}->{min_time} = $query->{duration};
130 }
131  
132 2 ▶ if ( !defined $stats{$type}->{max_time}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $stats{$type}{"max_time"} or $query->{"duration"} > $stats{$type}{"max_time"}) 2 0 2
Condition33%1/3
not defined $stats{$type}{"max_time"} or $query->{"duration"} > $stats{$type}{"max_time"}
l!l&&r!l&&!r
200
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 2 $stats{$type}->{max_time} = $query->{duration};
136 }
137 }
138  
139 2 return \%stats;
140 3 }
141  
142 7 ▶ method _get_query_type ($sql) {
Subroutine _get_query_type: called CC=8 SCAR=36.1
143 7 ▶ return 'SELECT' if $sql =~ /^\s*SELECT/i;
Branch: 1 branch
BranchTrueFalseTotal
if $sql =~ /^\s*SELECT/iu 7 0 7
144 0 ▶ return 'INSERT' if $sql =~ /^\s*INSERT/i;
Branch: 1 branch
BranchTrueFalseTotal
if $sql =~ /^\s*INSERT/iu 0 0 0
145 0 ▶ return 'UPDATE' if $sql =~ /^\s*UPDATE/i;
Branch: 1 branch
BranchTrueFalseTotal
if $sql =~ /^\s*UPDATE/iu 0 0 0
146 0 ▶ return 'DELETE' if $sql =~ /^\s*DELETE/i;
Branch: 1 branch
BranchTrueFalseTotal
if $sql =~ /^\s*DELETE/iu 0 0 0
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 3 }
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 3 }
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 3 }
183  
184 5 ▶ method get_stats () {
Subroutine get_stats: called CC=12 SCAR=25.0
Pod documented
185 5 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 5 ▶ return $stats unless @{$queries};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$queries;} 1 4 5
198  
199 4 my $first_query_time = $queries->[0]->{start};
200 4 my $last_query_time = $queries->[-1]->{end};
201  
202 4 for my $query ( @{$queries} ) {
203 5 $stats->{total_queries}++;
204 5 $stats->{total_time} += $query->{duration};
205  
206 5 ▶ if ( !defined $stats->{min_time}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $stats->{"min_time"} or $query->{"duration"} < $stats->{"min_time"}) 5 0 5
Condition66%2/3
not defined $stats->{"min_time"} or $query->{"duration"} < $stats->{"min_time"}
l!l&&r!l&&!r
410
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 5 $stats->{min_time} = $query->{duration};
210 }
211  
212 5 ▶ if ( $query->{duration} > $stats->{max_time} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query->{"duration"} > $stats->{"max_time"}) 4 1 5
213 4 $stats->{max_time} = $query->{duration};
214 }
215  
216 5 ▶ $stats->{slow_queries}++ if $query->{duration} > $slow_query;
Branch: 1 branch
BranchTrueFalseTotal
if $query->{"duration"} > $slow_query 0 5 5
217  
218 5 my $type = $self->_get_query_type( $query->{sql} );
219 5 ▶ $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
14
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 5 my $type_stats = $stats->{query_types}->{$type};
228 5 $type_stats->{count}++;
229 5 $type_stats->{total_time} += $query->{duration};
230  
231 5 ▶ if ( !defined $type_stats->{min_time}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $type_stats->{"min_time"} or $query->{"duration"} < $type_stats->{"min_time"}) 5 0 5
Condition66%2/3
not defined $type_stats->{"min_time"} or $query->{"duration"} < $type_stats->{"min_time"}
l!l&&r!l&&!r
410
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 5 $type_stats->{min_time} = $query->{duration};
235 }
236  
237 5 ▶ if ( $query->{duration} > $type_stats->{max_time} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query->{"duration"} > $type_stats->{"max_time"}) 4 1 5
238 4 $type_stats->{max_time} = $query->{duration};
239 }
240  
241 $type_stats->{avg_time} =
242 5 $type_stats->{total_time} / $type_stats->{count};
243 }
244  
245 4 $stats->{avg_time} = $stats->{total_time} / $stats->{total_queries};
246  
247 4 my $total_elapsed = $last_query_time - $first_query_time;
248 4 ▶ if ( $total_elapsed > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total_elapsed > 0) 4 0 4
249 $stats->{queries_per_second} =
250 4 $stats->{total_queries} / $total_elapsed;
251 }
252  
253 4 $stats->{last_query_time} = $last_query_time;
254  
255 $stats->{slow_query_percentage} =
256 4 ( $stats->{slow_queries} / $stats->{total_queries} ) * 100;
257  
258 4 return $stats;
259 3 }
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 3 }
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 3 }
301  
302 2 ▶ method clear () { $queries = [] }
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
303 }
304  
305 3 1;
306  
307 __END__
« Fast/Connector.pm ↑ Summary Fast/Schema.pm »