QueryLog/Analyzer.pm

filter: Statementstmt 97.9% 48 / 49 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 5 / 5 totaltotal 96.7% 60 / 62 CC 8 SCAR 9.9
CC
8
Cov
96%
get_totaled_queries
4.0
get_totaled_queries_by_bucket
3.0
get_sorted_queries
2.0
CRAP 2.7
« QueryLog.pm ↑ Summary QueryLog/Query.pm »
1 package DBIx::Class::QueryLog::Analyzer;
2 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 4 has querylog => (
5 is => 'rw',
6 isa => 'DBIx::Class::QueryLog'
7 );
8  
9 =head1 NAME
10  
11 DBIx::Class::QueryLog::Analyzer - Query Analysis
12  
13 =head1 SYNOPSIS
14  
15 Analyzes the results of a QueryLog. Create an Analyzer and pass it the
16 QueryLog:
17  
18 my $schema = ... # Get your schema!
19 my $ql = DBIx::Class::QueryLog->new;
20 $schema->storage->debugobj($ql);
21 $schema->storage->debug(1);
22 ... # do some stuff!
23 my $ana = DBIx::Class::QueryLog::Analyzer->new({ querylog => $ql });
24 my @queries = $ana->get_sorted_queries;
25 # or...
26 my $totaled = $ana->get_totaled_queries;
27  
28  
29 =head1 METHODS
30  
31 =head2 new
32  
33 Create a new DBIx::Class::QueryLog::Analyzer
34  
35 =head2 get_sorted_queries
36  
37 Returns an arrayref of all Query objects, sorted by elapsed time (descending).
38  
39 =cut
40  
41 sub get_sorted_queries {
42 1 ▶ my ($self) = @_;
Subroutine get_sorted_queries: called CC=2 SCAR=6.9
Pod documented
43  
44 1 my @queries;
45  
46 1 foreach my $l (@{ $self->querylog->log }) {
47 2 push(@queries, @{ $l->get_sorted_queries });
48 }
49 1 return [ reverse sort { $a->time_elapsed <=> $b->time_elapsed } @queries ];
50 }
51  
52 =head2 get_fastest_query_executions($sql_statement)
53  
54 Returns an arrayref of Query objects representing in order of the fastest
55 executions of a given statement. Accepts either SQL or a
56 DBIx::Class::QueryLog::Query object. If given SQL, it must match the executed
57 SQL, including placeholders.
58  
59 $ana->get_slowest_query_executions("SELECT foo FROM bar WHERE gorch = ?");
60  
61 =cut
62 sub get_fastest_query_executions {
63 3 ▶ my ($self, $sql) = @_;
Subroutine get_fastest_query_executions: called CC=2 SCAR=6.9
Pod documented
64  
65 3 my @queries;
66 3 foreach my $l (@{ $self->querylog->log }) {
67 12 push(@queries, @{ $l->get_sorted_queries($sql) });
68 }
69  
70 3 return [ sort { $a->time_elapsed <=> $b->time_elapsed } @queries ];
71 }
72  
73 =head2 get_slowest_query_executions($sql_statement)
74  
75 Opposite of I<get_fastest_query_executions>. Same arguments.
76  
77 =cut
78  
79 sub get_slowest_query_executions {
80 2 ▶ my ($self, $sql) = @_;
Subroutine get_slowest_query_executions: called CC=1 SCAR=0.0
Pod documented
81  
82 2 return [ reverse @{ $self->get_fastest_query_executions($sql) } ];
83 }
84  
85 =head2 get_totaled_queries
86  
87 Returns hashref of the queries executed, with same-SQL combined and totaled.
88 So if the same query is executed multiple times, it will be combined into
89 a single entry. The structure is:
90  
91 $var = {
92 'SQL that was EXECUTED' => {
93 count => 2,
94 time_elapsed => 1931,
95 queries => [
96 DBIx::Class::QueryLog...,
97 DBIx::Class::QueryLog...
98 ]
99 }
100 }
101  
102 This is useful for when you've fine-tuned individually slow queries and need
103 to isolate which queries are executed a lot, so that you can determine which
104 to focus on next.
105  
106 To sort it you'll want to use something like this (sorry for the long line,
107 blame perl...):
108  
109 my $analyzed = $ana->get_totaled_queries;
110 my @keys = reverse sort {
111 $analyzed->{$a}->{'time_elapsed'} <=> $analyzed->{$b}->{'time_elapsed'}
112 } keys(%{ $analyzed });
113  
114 So one could sort by count or time_elapsed.
115  
116 =cut
117  
118 sub get_totaled_queries {
119 2 ▶ my ($self, $honor_buckets) = @_;
Subroutine get_totaled_queries: called CC=4 SCAR=14.0
Pod documented
120  
121 2 my %totaled;
122 2 foreach my $l (@{ $self->querylog->log }) {
123 4 foreach my $q (@{ $l->queries }) {
124 6 ▶ if($honor_buckets) {
Branch: 1 branch
BranchTrueFalseTotal
if ($honor_buckets) { } 0 6 6
125 0 return $self->get_totaled_queries_by_bucket;
126 } else {
127 6 $totaled{$q->sql}->{count}++;
128 6 $totaled{$q->sql}->{time_elapsed} += $q->time_elapsed;
129 6 push(@{ $totaled{$q->sql}->{queries} }, $q);
130 }
131 }
132 }
133 2 return \%totaled;
134 }
135  
136 =head2 get_totaled_queries_by_bucket
137  
138 Same as get_totaled_queries, but breaks the totaled queries up by bucket:
139  
140 $var = {
141 'bucket1' => {
142 'SQL that was EXECUTED' => {
143 count => 2,
144 time_elapsed => 1931,
145 queries => [
146 DBIx::Class::QueryLog...,
147 DBIx::Class::QueryLog...
148 ]
149 }
150 }
151 'bucket2' => { ... }
152 }
153  
154 It is otherwise identical to get_totaled_queries
155  
156 =cut
157  
158 sub get_totaled_queries_by_bucket {
159 1 ▶ my ($self) = @_;
Subroutine get_totaled_queries_by_bucket: called CC=3 SCAR=11.0
Pod documented
160  
161 1 my %totaled;
162 1 foreach my $l (@{ $self->querylog->log }) {
163 2 foreach my $q (@{ $l->queries }) {
164 2 $totaled{$q->bucket}->{$q->sql}->{count}++;
165 2 $totaled{$q->bucket}->{$q->sql}->{time_elapsed} += $q->time_elapsed;
166 2 push(@{ $totaled{$q->bucket}->{$q->sql}->{queries} }, $q);
167 }
168 }
169 1 return \%totaled;
170 }
171  
172 =head1 AUTHOR
173  
174 Cory G Watson C<< <gphat at cpan.org> >>
175  
176 =head1 ACKNOWLEDGEMENTS
177  
178 =head1 COPYRIGHT & LICENSE
179  
180 Copyright 2009 Cory G Watson, all rights reserved.
181  
182 This program is free software; you can redistribute it and/or modify it
183 under the same terms as Perl itself.
184  
185 =cut
186  
187 4 __PACKAGE__->meta->make_immutable;
188  
189 4 1;
« QueryLog.pm ↑ Summary QueryLog/Query.pm »