[Bps-public-commit] jifty-plugin-profiletemplates branch, master, created. 1727739db076edc9a59f12279682d25d8e628499

Alex Vandiver alexmv at bestpractical.com
Mon May 24 16:45:13 EDT 2010


The branch, master has been created
        at  1727739db076edc9a59f12279682d25d8e628499 (commit)

- Log -----------------------------------------------------------------
commit 1727739db076edc9a59f12279682d25d8e628499
Author: Alex Vandiver <alex at chmrr.net>
Date:   Mon May 24 16:43:46 2010 -0400

    Initial import

diff --git a/Makefile.PL b/Makefile.PL
new file mode 100644
index 0000000..0c211a7
--- /dev/null
+++ b/Makefile.PL
@@ -0,0 +1,6 @@
+use inc::Module::Install;
+name('Jifty-Plugin-ProfileTemplates');
+version('0.01');
+requires('Jifty' => '1.00105');
+
+WriteAll;
diff --git a/lib/Jifty/Plugin/ProfileTemplates.pm b/lib/Jifty/Plugin/ProfileTemplates.pm
new file mode 100644
index 0000000..21ff5c0
--- /dev/null
+++ b/lib/Jifty/Plugin/ProfileTemplates.pm
@@ -0,0 +1,130 @@
+package Jifty::Plugin::ProfileTemplates;
+use strict;
+use warnings;
+use base 'Jifty::Plugin';
+use Time::HiRes qw//;
+
+sub prereq_plugins { 'RequestInspector' }
+
+my @stack;
+sub init {
+    my $self = shift;
+    return if $self->_pre_init;
+
+    Jifty::Handler->add_trigger(
+        before_render_template => sub {
+            return unless @stack;
+            my (undef, $handler, $path) = @_;
+
+            push @stack, {
+                path => $path,
+                handler => ref $handler,
+                start => Time::HiRes::time(),
+                args => { %{Jifty->web->request->arguments}, %{Jifty->web->request->template_arguments || {}} },
+            };
+            delete $stack[-1]{args}{region};
+        }
+    );
+    Jifty::Handler->add_trigger(
+        after_render_template => sub {
+            return unless @stack;
+            $stack[-1]{time} = Time::HiRes::time() - delete $stack[-1]{start};
+            push @{$stack[-2]{kids}}, pop @stack;
+        }
+    );
+}
+
+
+sub inspect_before_request {
+    @stack = ( { kids => [] } );
+}
+
+sub inspect_after_request {
+    my $self = shift;
+
+    my $ret = $stack[0];
+    $ret->{time} += $_->{time} for @{$ret->{kids}};
+    warn YAML::Dump($ret);
+
+    @stack = ();
+    return $ret;
+}
+
+sub inspect_render_summary {
+    my $self = shift;
+    my $log = shift;
+
+    return _("Total time in templates, %1", sprintf("%5.4f",$log->{time}));
+}
+
+sub inspect_render_analysis {
+    my $self = shift;
+    my $log = shift;
+    my $id = shift;
+
+    Jifty::View::Declare::Helpers::render_region(
+        name => 'templates',
+        path => '/__jifty/admin/requests/templates',
+        args => {
+            id => $id,
+        },
+    );
+}
+
+1;
+
+
+__END__
+
+=head1 NAME
+
+Jifty::Plugin::ProfileTemplates - Show timing of template rendering
+
+=head1 DESCRIPTION
+
+This plugin will log the time each template takes to render, 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:
+     - ProfileTemplates: {}
+
+=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 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/ProfileTemplates/View.pm b/lib/Jifty/Plugin/ProfileTemplates/View.pm
new file mode 100644
index 0000000..4f29b93
--- /dev/null
+++ b/lib/Jifty/Plugin/ProfileTemplates/View.pm
@@ -0,0 +1,93 @@
+package Jifty::Plugin::ProfileTemplates::View;
+use strict;
+use warnings;
+use Jifty::View::Declare -base;
+
+template '/__jifty/admin/requests/templates' => sub {
+    my $request_inspector = Jifty->find_plugin('Jifty::Plugin::RequestInspector');
+    my $id = get('id');
+
+    my $data = $request_inspector->get_plugin_data($id, "Jifty::Plugin::ProfileTemplates");
+
+    show '/__jifty/admin/requests/template', id => $id;
+};
+
+template '/__jifty/admin/requests/template' => sub {
+    my $request_inspector = Jifty->find_plugin('Jifty::Plugin::RequestInspector');
+    my $id    = get('id');
+    my @index = split /\./, (defined get('t') ? get('t') : "");
+    my $data = $request_inspector->get_plugin_data($id, "Jifty::Plugin::ProfileTemplates");
+    $data = $data->{kids}[$_] for @index;
+
+    ol {
+        for my $t (0..@{$data->{kids}}-1) {
+            my $seconds = sprintf('%.2f', $data->{kids}[$t]{time});
+            my $path = $data->{kids}[$t]{path};
+            my $label = _("(%1s) %2", $seconds, $path);
+            li {
+                my @kids = @{$data->{kids}[$t]{kids} || []};
+                if (@kids) {
+                    hyperlink(
+                        label => $label,
+                        onclick => {
+                            region    => Jifty->web->qualified_region("t_$t"),
+                            replace_with => '/__jifty/admin/requests/template',
+                            toggle    => 1,
+                            effect    => 'slideDown',
+                            arguments => {
+                                id => $id,
+                                t  => join(".", at index,$t),
+                            },
+                        },
+                    );
+                } else {
+                    outs $label;
+                }
+
+                outs " [";
+                hyperlink(
+                    label => _("Arguments"),
+                    onclick => {
+                        region    => Jifty->web->qualified_region("args_$t"),
+                        replace_with => '/__jifty/admin/requests/template_args',
+                        toggle    => 1,
+                        effect    => 'slideDown',
+                        arguments => {
+                            id => $id,
+                            t  => join(".", at index,$t),
+                        },
+                    },
+                );
+                outs "]";
+                render_region("args_$t");
+                render_region("t_$t") if @kids;
+            }
+        }
+    }
+};
+
+template '/__jifty/admin/requests/template_args' => sub {
+    my $request_inspector = Jifty->find_plugin('Jifty::Plugin::RequestInspector');
+    my $id    = get('id');
+    my @index = split /\./, get('t');
+    my $data = $request_inspector->get_plugin_data($id, "Jifty::Plugin::ProfileTemplates");
+    $data = $data->{kids}[$_] for @index;
+
+    dl {
+        for (sort keys %{$data->{args}}) {
+            dt { $_ };
+            dd { $data->{args}{$_} };
+        }
+    }
+};
+
+1;
+
+__END__
+
+=head1 NAME
+
+Jifty::Plugin::ProfileTemplates::View - View for ProfileTemplates
+
+=cut
+

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



More information about the Bps-public-commit mailing list