[Bps-public-commit] jifty-plugin-timing branch, master, updated. 982d4e0ee3a30cb88965c70e81c1ae0fd82f999e

Alex M Vandiver alexmv at bestpractical.com
Wed Jan 6 12:40:50 EST 2010


The branch, master has been updated
       via  982d4e0ee3a30cb88965c70e81c1ae0fd82f999e (commit)
      from  983dfdc6452f513d534e1e9d1816132bdfbb5e57 (commit)

Summary of changes:
 .gitignore                      |   10 +++
 Makefile.PL                     |    6 ++
 lib/Jifty/Plugin/Timing.pm      |  162 +++++++++++++++++++++++++++++++++++++++
 lib/Jifty/Plugin/Timing/View.pm |   66 ++++++++++++++++
 4 files changed, 244 insertions(+), 0 deletions(-)
 create mode 100644 Makefile.PL
 create mode 100644 lib/Jifty/Plugin/Timing.pm
 create mode 100644 lib/Jifty/Plugin/Timing/View.pm

- Log -----------------------------------------------------------------
commit 982d4e0ee3a30cb88965c70e81c1ae0fd82f999e
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Wed Jan 6 12:40:43 2010 -0500

    Initial import

diff --git a/.gitignore b/.gitignore
index e69de29..bb4ab30 100644
--- a/.gitignore
+++ b/.gitignore
@@ -0,0 +1,10 @@
+MANIFEST
+MANIFEST.bak
+META.yml
+Makefile
+Makefile.old
+SIGNATURE
+blib/
+inc/
+pm_to_blib
+*.swp
diff --git a/Makefile.PL b/Makefile.PL
new file mode 100644
index 0000000..c4f376f
--- /dev/null
+++ b/Makefile.PL
@@ -0,0 +1,6 @@
+use inc::Module::Install;
+name('Jifty-Plugin-Timing');
+version('0.01');
+requires('Jifty' => '1.00105');
+
+WriteAll;
diff --git a/lib/Jifty/Plugin/Timing.pm b/lib/Jifty/Plugin/Timing.pm
new file mode 100644
index 0000000..002345d
--- /dev/null
+++ b/lib/Jifty/Plugin/Timing.pm
@@ -0,0 +1,162 @@
+package Jifty::Plugin::Timing;
+use strict;
+use warnings;
+use base 'Jifty::Plugin';
+use Time::HiRes qw//;
+
+sub prereq_plugins { 'RequestInspector' }
+
+our @hooks = qw/
+                  before_request have_request
+                  before_dispatcher_SETUP   after_dispatcher_SETUP
+                  before_dispatcher_RUN
+                  before_render_template    after_render_template
+                  after_dispatcher_RUN
+                  before_flush              after_flush
+                  before_dispatcher_CLEANUP after_dispatcher_CLEANUP
+                  before_cleanup
+                  after_request
+              /;
+
+my @request;
+
+sub init {
+    my $self = shift;
+    return if $self->_pre_init;
+
+    my $request_inspector = Jifty->find_plugin('Jifty::Plugin::RequestInspector');
+
+    for my $hook (@hooks) {
+        next if $hook eq "before_request" or $hook eq "after_request";
+        Jifty::Handler->add_trigger(
+            $hook => sub {
+                return unless @request;
+                return if Jifty->web->request and Jifty->web->request->is_subrequest;
+
+                push @request, {
+                    name => $hook,
+                    time => Time::HiRes::time(),
+                }
+            }
+        );
+    }
+}
+
+
+sub inspect_before_request {
+    warn "Leftover request! " . YAML::Dump(\@request) if @request;
+    @request = (
+        {
+            name => "before_request",
+            time => Time::HiRes::time(),
+        }
+    );
+}
+
+sub inspect_after_request {
+    my $self = shift;
+    push @request,
+        {
+            name => "after_request",
+            time => Time::HiRes::time(),
+        };
+    for ( 1 .. $#request ) {
+        $request[$_]{diff}  = $request[$_]{time} - $request[$_-1]{time};
+        $request[$_]{cumul} = $request[$_]{time} - $request[0]{time};
+    }
+    my $ret = [ @request ];
+    @request = ();
+    return $ret;
+}
+
+sub inspect_render_summary {
+    my $self = shift;
+    my $log = shift;
+
+    return _("Total time, %1", sprintf("%5.4f",$log->[-1]{time} - $log->[0]{time}));
+}
+
+sub inspect_render_analysis {
+    my $self = shift;
+    my $log = shift;
+    my $id = shift;
+
+    Jifty::View::Declare::Helpers::render_region(
+        name => 'timing',
+        path => '/__jifty/admin/requests/timing',
+        args => {
+            id => $id,
+        },
+    );
+}
+
+sub inspect_render_aggregate {
+    my $self = shift;
+    Jifty::View::Declare::Helpers::render_region(
+        name => 'timing-aggregate',
+        path => '/__jifty/admin/requests/timing_aggregate',
+    );
+}
+
+1;
+
+
+__END__
+
+=head1 NAME
+
+Jifty::Plugin::Timing - Show timing of hifty internals
+
+=head1 DESCRIPTION
+
+This plugin will log the time various parts of the jifty framework
+take to respond to a request, and generate reports.  Such reports are
+available at:
+
+    http://your.app/__jifty/admin/requests
+
+=head1 USAGE
+
+Add the following to your site_config.yml
+
+ framework:
+   Plugins:
+     - Timing: {}
+
+=head1 METHODS
+
+=head2 init
+
+Adds the necessary hooks
+
+=head2 inspect_before_request
+
+Clears the query log so we don't log any unrelated previous queries.
+
+=head2 inspect_after_request
+
+Stash the query log.
+
+=head2 inspect_render_summary
+
+Display how many queries and their total time.
+
+=head2 inspect_render_analysis
+
+Render a template with all the detailed information.
+
+=head2 inspect_render_aggregate
+
+Render a template with aggragate information.
+
+=head2 prereq_plugins
+
+This plugin depends on L<Jifty::Plugin::RequestInspector>.
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2010 Best Practical Solutions
+
+This is free software and may be modified and distributed under the same terms as Perl itself.
+
+=cut
diff --git a/lib/Jifty/Plugin/Timing/View.pm b/lib/Jifty/Plugin/Timing/View.pm
new file mode 100644
index 0000000..52a2bac
--- /dev/null
+++ b/lib/Jifty/Plugin/Timing/View.pm
@@ -0,0 +1,66 @@
+package Jifty::Plugin::Timing::View;
+use strict;
+use warnings;
+use Jifty::View::Declare -base;
+
+template '/__jifty/admin/requests/timing' => sub {
+    my $request_inspector = Jifty->find_plugin('Jifty::Plugin::RequestInspector');
+    my $id = get('id');
+
+    my $data = $request_inspector->get_plugin_data($id, "Jifty::Plugin::Timing");
+    table {
+        row {
+            th { $_ } for ("Stage", "Cumulative time", "Difference");
+        };
+        for (@{$data}) {
+            row {
+                cell { $_->{name} }
+                cell { sprintf("%5.4f",$_->{cumul} || 0) }
+                cell { sprintf("%5.4f",$_->{diff}  || 0) }
+            }
+        }
+    }
+};
+
+template '/__jifty/admin/requests/timing_aggregate' => sub {
+    my $request_inspector = Jifty->find_plugin('Jifty::Plugin::RequestInspector');
+
+    my @data = $request_inspector->get_all_plugin_data("Jifty::Plugin::Timing");
+    unless (@data) {
+        outs "No data yet.";
+        return;
+    }
+
+    my %avg;
+    for my $req (@data) {
+        for (@{$req}) {
+            $avg{$_->{name}}{diff}  += $_->{diff} || 0;
+            $avg{$_->{name}}{cumul} += $_->{cumul} || 0;
+        }
+    }
+
+    p { "Over ".(scalar @data)." requests:" }
+    table {
+        row {
+            th { $_ } for ("Stage", "Cumulative time", "Difference");
+        };
+        for (@Jifty::Plugin::Timing::hooks) {
+            row {
+                cell { $_ }
+                cell { sprintf("%5.4f",$avg{$_}{cumul}/@data) }
+                cell { sprintf("%5.4f",$avg{$_}{diff}/@data) }
+            }
+        }
+    }
+};
+
+1;
+
+__END__
+
+=head1 NAME
+
+Jifty::Plugin::Timing::View - View for Timing
+
+=cut
+

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list