| 1 | # $Id$ | ||
| 2 | |||
| 3 | package Data::ObjectDriver::Profiler; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use base qw( Class::Accessor::Fast ); | ||
| 7 | |||
| 8 | use List::Util qw( min ); | ||
| 9 | |||
| 10 | my $simpletable; | ||
| 11 | BEGIN { | ||
| 12 | eval { | ||
| 13 | require Text::SimpleTable; | ||
| 14 | $simpletable = 1; | ||
| 15 | }; | ||
| 16 | } | ||
| 17 | |||
| 18 | __PACKAGE__->mk_accessors(qw( statistics query_log )); | ||
| 19 | |||
| 20 | sub new { | ||
| 21 | my $class = shift; | ||
| 22 | my $profiler = $class->SUPER::new(@_); | ||
| 23 | $profiler->reset; | ||
| 24 | return $profiler; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub reset { | ||
| 28 | my $profiler = shift; | ||
| 29 | $profiler->statistics({}); | ||
| 30 | $profiler->query_log([]); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub increment { | ||
| 34 | my $profiler = shift; | ||
| 35 | my($driver, $stat) = @_; | ||
| 36 | my($type) = ref($driver) =~ /([^:]+)$/; | ||
| 37 | $profiler->statistics->{join ':', $type, $stat}++; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub _normalize { | ||
| 41 | my($sql) = @_; | ||
| 42 | $sql =~ s/^\s*//; | ||
| 43 | $sql =~ s/\s*$//; | ||
| 44 | $sql =~ s/[\r\n]/ /g; | ||
| 45 | return $sql; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub record_query { | ||
| 49 | my $profiler = shift; | ||
| 50 | my($driver, $sql) = @_; | ||
| 51 | $sql = _normalize($sql); | ||
| 52 | push @{ $profiler->query_log }, $sql; | ||
| 53 | my($type) = $sql =~ /^\s*(\w+)/; | ||
| 54 | $profiler->increment($driver, 'total_queries'); | ||
| 55 | $profiler->increment($driver, 'query_' . lc($type)) if $type; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub query_frequency { | ||
| 59 | my $profiler = shift; | ||
| 60 | my $log = $profiler->query_log; | ||
| 61 | my %freq; | ||
| 62 | for my $sql (@$log) { | ||
| 63 | $freq{$sql}++; | ||
| 64 | } | ||
| 65 | return \%freq; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub total_queries { return $_[0]->statistics->{'DBI:total_queries'} || 0 } | ||
| 69 | |||
| 70 | sub report_queries_by_type { | ||
| 71 | my $profiler = shift; | ||
| 72 | return "Text::SimpleTable unavailable at startup, reports disabled." unless $simpletable; | ||
| 73 | my $stats = $profiler->statistics; | ||
| 74 | my $tbl = Text::SimpleTable->new( [ 64, 'Type' ], [ 9, 'Number' ] ); | ||
| 75 | for my $stat (keys %$stats) { | ||
| 76 | my($type) = $stat =~ /^DBI:query_(\w+)$/ | ||
| 77 | or next; | ||
| 78 | $tbl->row(uc $type, $stats->{$stat}); | ||
| 79 | } | ||
| 80 | return $tbl->draw; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub report_query_frequency { | ||
| 84 | my $profiler = shift; | ||
| 85 | return "Text::SimpleTable unavailable at startup, reports disabled." unless $simpletable; | ||
| 86 | my $freq = $profiler->query_frequency; | ||
| 87 | my $tbl = Text::SimpleTable->new( [ 64, 'Query' ], [ 9, 'Number' ] ); | ||
| 88 | my @sql = sort { ($freq->{$b} <=> $freq->{$a}) || ($a cmp $b) } keys %$freq; | ||
| 89 | for my $sql (@sql[0..min($#sql, 19)]) { | ||
| 90 | $tbl->row($sql, $freq->{$sql}); | ||
| 91 | } | ||
| 92 | return $tbl->draw; | ||
| 93 | } | ||
| 94 | |||
| 95 | 1; | ||
| 96 | __END__ |