[Bast-commits] r4743 - in DBIx-Class-QueryLog/1.0/trunk: . lib/DBIx/Class/QueryLog t

gphat at dev.catalyst.perl.org gphat at dev.catalyst.perl.org
Thu Aug 7 18:13:09 BST 2008


Author: gphat
Date: 2008-08-07 18:13:09 +0100 (Thu, 07 Aug 2008)
New Revision: 4743

Added:
   DBIx-Class-QueryLog/1.0/trunk/t/04-fastslow.t
Modified:
   DBIx-Class-QueryLog/1.0/trunk/Changes
   DBIx-Class-QueryLog/1.0/trunk/lib/DBIx/Class/QueryLog/Analyzer.pm
   DBIx-Class-QueryLog/1.0/trunk/lib/DBIx/Class/QueryLog/Query.pm
   DBIx-Class-QueryLog/1.0/trunk/lib/DBIx/Class/QueryLog/Transaction.pm
   DBIx-Class-QueryLog/1.0/trunk/t/03-buckets.t
Log:
Add get_(fast|slow)est_query_executions


Modified: DBIx-Class-QueryLog/1.0/trunk/Changes
===================================================================
--- DBIx-Class-QueryLog/1.0/trunk/Changes	2008-08-07 15:33:24 UTC (rev 4742)
+++ DBIx-Class-QueryLog/1.0/trunk/Changes	2008-08-07 17:13:09 UTC (rev 4743)
@@ -1,5 +1,8 @@
 Revision history for DBIx-Class-QueryLog
 
+1.1.1
+    - Add get_(fast|slow)est_query_executions to Analyzer
+
 1.1.0
     - Add buckets to QueryLog
     - Transaction complete wasn't using add_to_log

Modified: DBIx-Class-QueryLog/1.0/trunk/lib/DBIx/Class/QueryLog/Analyzer.pm
===================================================================
--- DBIx-Class-QueryLog/1.0/trunk/lib/DBIx/Class/QueryLog/Analyzer.pm	2008-08-07 15:33:24 UTC (rev 4742)
+++ DBIx-Class-QueryLog/1.0/trunk/lib/DBIx/Class/QueryLog/Analyzer.pm	2008-08-07 17:13:09 UTC (rev 4743)
@@ -58,8 +58,41 @@
     return [ reverse sort { $a->time_elapsed <=> $b->time_elapsed } @queries ];
 }
 
-=head2 get_totaled_queries($honor_buckets)
+=head2 get_fastest_query_executions($sql_statement)
 
+Returns an arrayref of Query objects representing in order of the fastest
+executions of a given statement.  Accepts either SQL or a
+DBIx::Class::QueryLog::Query object.  If given SQL, it must match the executed
+SQL, including placeholders.
+
+  $ana->get_slowest_query_executions("SELECT foo FROM bar WHERE gorch = ?");
+
+=cut
+sub get_fastest_query_executions {
+    my ($self, $sql) = @_;
+
+    my @queries;
+    foreach my $l (@{ $self->querylog->log }) {
+        push(@queries, @{ $l->get_sorted_queries($sql) });
+    }
+
+    return [ sort { $a->time_elapsed <=> $b->time_elapsed } @queries ];
+}
+
+=head2 get_slowest_query_executions($sql_statement)
+
+Opposite of I<get_fastest_query_executions>.  Same arguments.
+
+=cut
+
+sub get_slowest_query_executions {
+    my ($self, $sql) = @_;
+
+    return [ reverse @{ $self->get_fastest_query_executions($sql) } ];
+}
+
+=head2 get_totaled_queries
+
 Returns hashref of the queries executed, with same-SQL combined and totaled.
 So if the same query is executed multiple times, it will be combined into
 a single entry.  The structure is:
@@ -75,23 +108,6 @@
         }
     }
 
-If you pass a true value then this method will break out by bucket.  The
-structure becomes:
-
-$var = {
-    'bucket1' => {
-        'SQL that was EXECUTED' => {
-            count           => 2,
-            time_elapsed    => 1931,
-            queries         => [
-                DBIx::Class::QueryLog...,
-                DBIx::Class::QueryLog...
-            ]
-        }
-    }
-    'bucket2' => { ... }
-}
-
 This is useful for when you've fine-tuned individually slow queries and need
 to isolate which queries are executed a lot, so that you can determine which
 to focus on next.
@@ -115,9 +131,7 @@
     foreach my $l (@{ $self->querylog->log }) {
         foreach my $q (@{ $l->queries }) {
             if($honor_buckets) {
-                $totaled{$q->bucket}->{$q->sql}->{count}++;
-                $totaled{$q->bucket}->{$q->sql}->{time_elapsed} += $q->time_elapsed;
-                push(@{ $totaled{$q->bucket}->{$q->sql}->{queries} }, $q);
+                return $self->get_totaled_queries_by_bucket;
             } else {
                 $totaled{$q->sql}->{count}++;
                 $totaled{$q->sql}->{time_elapsed} += $q->time_elapsed;
@@ -128,6 +142,42 @@
     return \%totaled;
 }
 
+=head2 get_totaled_queries_by_bucket
+
+Same as get_totaled_queries, but breaks the totaled queries up by bucket:
+
+$var = {
+    'bucket1' => {
+        'SQL that was EXECUTED' => {
+            count           => 2,
+            time_elapsed    => 1931,
+            queries         => [
+                DBIx::Class::QueryLog...,
+                DBIx::Class::QueryLog...
+            ]
+        }
+    }
+    'bucket2' => { ... }
+}
+
+It is otherwise identical to get_totaled_queries
+
+=cut
+
+sub get_totaled_queries_by_bucket {
+    my ($self) = @_;
+
+    my %totaled;
+    foreach my $l (@{ $self->querylog->log }) {
+        foreach my $q (@{ $l->queries }) {
+            $totaled{$q->bucket}->{$q->sql}->{count}++;
+            $totaled{$q->bucket}->{$q->sql}->{time_elapsed} += $q->time_elapsed;
+            push(@{ $totaled{$q->bucket}->{$q->sql}->{queries} }, $q);
+        }
+    }
+    return \%totaled;
+}
+
 =head1 AUTHOR
 
 Cory 'G' Watson C<< <gphat at cpan.org> >>

Modified: DBIx-Class-QueryLog/1.0/trunk/lib/DBIx/Class/QueryLog/Query.pm
===================================================================
--- DBIx-Class-QueryLog/1.0/trunk/lib/DBIx/Class/QueryLog/Query.pm	2008-08-07 15:33:24 UTC (rev 4742)
+++ DBIx-Class-QueryLog/1.0/trunk/lib/DBIx/Class/QueryLog/Query.pm	2008-08-07 17:13:09 UTC (rev 4743)
@@ -76,8 +76,16 @@
 
 =cut
 sub get_sorted_queries {
-    my $self = shift;
+    my ($self, $sql) = @_;
 
+    if(defined($sql)) {
+        if($self->sql eq $sql) {
+            return [ $self ];
+        } else {
+            return [  ];
+        }
+    }
+
     return [ $self ];
 }
 

Modified: DBIx-Class-QueryLog/1.0/trunk/lib/DBIx/Class/QueryLog/Transaction.pm
===================================================================
--- DBIx-Class-QueryLog/1.0/trunk/lib/DBIx/Class/QueryLog/Transaction.pm	2008-08-07 15:33:24 UTC (rev 4742)
+++ DBIx-Class-QueryLog/1.0/trunk/lib/DBIx/Class/QueryLog/Transaction.pm	2008-08-07 17:13:09 UTC (rev 4743)
@@ -96,15 +96,26 @@
     return scalar(@{ $self->queries });
 }
 
-=head2 get_sorted_queries
+=head2 get_sorted_queries([ $sql ])
 
-Returns all the queries in this Transaction, sorted by elapsed time. (descending)
+Returns all the queries in this Transaction, sorted by elapsed time.
+(descending).
 
+If given an argument of an SQL statement, only queries matching that statement
+will be considered.
+
 =cut
 sub get_sorted_queries {
-    my $self = shift;
+    my ($self, $sql) = @_;
 
-    return [ reverse sort { $a->time_elapsed <=> $b->time_elapsed } @{ $self->queries } ];
+    my @qs;
+    if($sql) {
+        @qs = grep({ $_->sql eq $sql } @{ $self->queries });
+    } else {
+        @qs = @{ $self->queries };
+    }
+
+    return [ reverse sort { $a->time_elapsed <=> $b->time_elapsed } @qs ];
 }
 
 =head1 AUTHOR

Modified: DBIx-Class-QueryLog/1.0/trunk/t/03-buckets.t
===================================================================
--- DBIx-Class-QueryLog/1.0/trunk/t/03-buckets.t	2008-08-07 15:33:24 UTC (rev 4742)
+++ DBIx-Class-QueryLog/1.0/trunk/t/03-buckets.t	2008-08-07 17:13:09 UTC (rev 4743)
@@ -27,5 +27,5 @@
 my $ana = DBIx::Class::QueryLog::Analyzer->new({
     querylog => $ql
 });
-my $total = $ana->get_totaled_queries(1);
+my $total = $ana->get_totaled_queries_by_bucket;
 cmp_ok(scalar(keys(%{ $total })), '==', 2, '2 buckets');
\ No newline at end of file

Added: DBIx-Class-QueryLog/1.0/trunk/t/04-fastslow.t
===================================================================
--- DBIx-Class-QueryLog/1.0/trunk/t/04-fastslow.t	                        (rev 0)
+++ DBIx-Class-QueryLog/1.0/trunk/t/04-fastslow.t	2008-08-07 17:13:09 UTC (rev 4743)
@@ -0,0 +1,44 @@
+#!perl
+use strict;
+use warnings;
+use Test::More tests => 9;
+
+use DBIx::Class::QueryLog;
+use DBIx::Class::QueryLog::Analyzer;
+use DBIx::Class::QueryLog::Query;
+use DBIx::Class::QueryLog::Transaction;
+
+my $ql = DBIx::Class::QueryLog->new;
+$ql->query_start('SELECT * from foo', 'fast');
+$ql->query_end('SELECT * from foo', 'fast');
+
+$ql->query_start('SELECT * from foo2', 'fast');
+$ql->query_end('SELECT * from foo2', 'fast');
+
+$ql->query_start('SELECT * from foo', 'slow');
+sleep(3);
+$ql->query_end('SELECT * from foo', 'slow');
+
+$ql->txn_begin;
+$ql->query_start('SELECT * from foo', 'medium');
+sleep(2);
+$ql->query_end('SELECT * from foo', 'medium');
+$ql->txn_commit;
+
+my $ana = DBIx::Class::QueryLog::Analyzer->new({
+    querylog => $ql
+});
+my $slow = $ana->get_slowest_query_executions('SELECT * from foo');
+cmp_ok(scalar(@{ $slow }), '==', 3, '3 executions found');
+cmp_ok($slow->[0]->params->[0], 'eq', 'slow', 'slow executions 0');
+cmp_ok($slow->[1]->params->[0], 'eq', 'medium', 'slow executions 1');
+cmp_ok($slow->[2]->params->[0], 'eq', 'fast', 'slow executions 2');
+
+my $other = $ana->get_slowest_query_executions('SELECT * from foo2');
+cmp_ok(scalar(@{ $other }), '==', 1, '1 executions found');
+
+my $fast = $ana->get_fastest_query_executions('SELECT * from foo');
+cmp_ok(scalar(@{ $fast }), '==', 3, '3 executions found');
+cmp_ok($fast->[2]->params->[0], 'eq', 'slow', 'fast executions 2');
+cmp_ok($fast->[1]->params->[0], 'eq', 'medium', 'fast executions 1');
+cmp_ok($fast->[0]->params->[0], 'eq', 'fast', 'fast executions 0');




More information about the Bast-commits mailing list