[Jifty-commit] r6804 - in plugins/Jifty-Plugin-NYTProf: lib/Jifty/Plugin lib/Jifty/Plugin/NYTProf lib/Jifty/Plugin/NYTProf/Action

Jifty commits jifty-commit at lists.jifty.org
Wed Apr 22 00:32:52 EDT 2009


Author: clkao
Date: Wed Apr 22 00:32:52 2009
New Revision: 6804

Added:
   plugins/Jifty-Plugin-NYTProf/lib/Jifty/Plugin/NYTProf/
   plugins/Jifty-Plugin-NYTProf/lib/Jifty/Plugin/NYTProf/Action/
   plugins/Jifty-Plugin-NYTProf/lib/Jifty/Plugin/NYTProf/Action/NYTProfHTML.pm
   plugins/Jifty-Plugin-NYTProf/lib/Jifty/Plugin/NYTProf/Dispatcher.pm
   plugins/Jifty-Plugin-NYTProf/lib/Jifty/Plugin/NYTProf/View.pm
Modified:
   plugins/Jifty-Plugin-NYTProf/Makefile.PL
   plugins/Jifty-Plugin-NYTProf/lib/Jifty/Plugin/NYTProf.pm

Log:
all per-request profiling and basic admin ui for viewing html output.

Modified: plugins/Jifty-Plugin-NYTProf/Makefile.PL
==============================================================================
--- plugins/Jifty-Plugin-NYTProf/Makefile.PL	(original)
+++ plugins/Jifty-Plugin-NYTProf/Makefile.PL	Wed Apr 22 00:32:52 2009
@@ -2,6 +2,7 @@
 name('Jifty-Plugin-NYTProf');
 version('0.01');
 requires('Jifty' => '0.90220');
+requires('Devel::NYTProf' => 2.10);
 
 install_share;
 

Modified: plugins/Jifty-Plugin-NYTProf/lib/Jifty/Plugin/NYTProf.pm
==============================================================================
--- plugins/Jifty-Plugin-NYTProf/lib/Jifty/Plugin/NYTProf.pm	(original)
+++ plugins/Jifty-Plugin-NYTProf/lib/Jifty/Plugin/NYTProf.pm	Wed Apr 22 00:32:52 2009
@@ -3,7 +3,28 @@
 
 package Jifty::Plugin::NYTProf;
 use base qw/Jifty::Plugin/;
-__PACKAGE__->mk_accessors(qw/first_request path/);
+use File::Path 'mkpath';
+__PACKAGE__->mk_accessors(qw/profile_request/);
+
+our @requests;
+
+sub _static_root {
+    my $self = shift;
+    my $dir = Jifty::Util->absolute_path("var/profile");
+    mkpath [$dir] unless -d $dir;
+    return $dir;
+}
+
+sub static_root {
+    my $self = shift;
+    return ($self->SUPER::static_root(), $self->_static_root);
+}
+
+sub base_root {
+    my $dir = File::Spec->catfile(__PACKAGE__->_static_root, '_profile', Jifty->app_class.'-'.$$ );
+    mkpath [$dir] unless -d $dir;
+    return $dir;
+}
 
 sub init {
     my $self = shift;
@@ -15,23 +36,22 @@
         return;
     }
 
+    return if $self->_pre_init;
+
     my %args = (split /[:=]/, $ENV{NYTPROF} || '');
     if ($args{start} and $args{start} eq "no") {
-        warn "Only profiling requests; unset NYTPROF environment variable to profile startup\n";
-        $self->path(Jifty::Util->absolute_path("var/profile"));
-        unless (-e $self->path or mkdir $self->path) {
-            warn "Can't create @{[$self->path]} for profiling: $!";
-            return;
-        }
+        $self->profile_request(1);
+    }
 
-        $self->first_request(1);
+    if ($self->profile_request) {
+        warn "Only profiling requests; unset NYTPROF environment variable to profile startup\n";
 
         Jifty::Handler->add_trigger(
             before_request => sub { $self->before_request(@_) }
         );
 
         Jifty::Handler->add_trigger(
-            before_cleanup => sub { $self->before_cleanup }
+            after_request => sub { $self->after_request(@_) }
         );
     } else {
         warn "Only profiling startup time -- set NYTPROF=start=no to profile requests\n";
@@ -43,17 +63,28 @@
 
 sub before_request {
     my $self = shift;
-    return if $self->first_request;
 
-    DB::enable_profile($self->path . "/nytprof.$$.out")
+    my $file = File::Spec->catfile( __PACKAGE__->base_root, 'nytprof-'.(1+scalar @requests).".out" );
+    warn "==> enabling profile at $file";
+    DB::enable_profile( $file );
 }
 
-sub before_cleanup {
+sub after_request {
     my $self = shift;
-    DB::disable_profile()
-          unless $self->first_request;
+    my $handler = shift;
+    my $cgi = shift;
+    DB::finish_profile();
+
+    push @requests, {
+        id => 1 + @requests,
+        url => $cgi->url(-absolute=>1,-path_info=>1),
+        time => scalar gmtime,
+    };
+    return 1;
+}
 
-    $self->first_request(0);
+sub clear_profiles {
+    @requests = ();
 }
 
 1;

Added: plugins/Jifty-Plugin-NYTProf/lib/Jifty/Plugin/NYTProf/Action/NYTProfHTML.pm
==============================================================================
--- (empty file)
+++ plugins/Jifty-Plugin-NYTProf/lib/Jifty/Plugin/NYTProf/Action/NYTProfHTML.pm	Wed Apr 22 00:32:52 2009
@@ -0,0 +1,20 @@
+package Jifty::Plugin::NYTProf::Action::NYTProfHTML;
+use strict;
+
+use Jifty::Param::Schema;
+use Jifty::Action schema {
+    param 'id';
+};
+
+sub take_action {
+    my ($self) = @_;
+    my ($self_plugin) = Jifty->find_plugin('Jifty::Plugin::NYTProf');
+    my $file = $self_plugin->base_root."/nytprof-".$self->argument_value('id');
+    return if -d "$file";
+    die unless -e "$file.out";
+    system("nytprofhtml -f $file.out -o $file");
+    # XXX: error reporting etc
+    return;
+}
+
+1;

Added: plugins/Jifty-Plugin-NYTProf/lib/Jifty/Plugin/NYTProf/Dispatcher.pm
==============================================================================
--- (empty file)
+++ plugins/Jifty-Plugin-NYTProf/lib/Jifty/Plugin/NYTProf/Dispatcher.pm	Wed Apr 22 00:32:52 2009
@@ -0,0 +1,43 @@
+package Jifty::Plugin::NYTProf::Dispatcher;
+use warnings;
+use strict;
+
+use Jifty::Dispatcher -base;
+
+# http://your.app/queries -- display full profile report
+on '/__jifty/admin/profiles' => run {
+    set 'skip_zero' => 1;
+    show "/__jifty/admin/profiles/all";
+};
+
+# http://your.app/profiles/all -- full profile report with non-query requests
+on '/__jifty/admin/profiles/all' => run {
+    set 'skip_zero' => 0;
+    show "/__jifty/admin/profiles/all";
+};
+
+# http://your.app/profiles/clear -- clear profile results
+on '/__jifty/admin/profiles/clear' => run {
+    Jifty::Plugin::NYTProf->clear_profiles;
+    set 'skip_zero' => 1;
+    redirect "/__jifty/admin/profiles";
+};
+
+=head1 NAME
+
+Jifty::Plugin::NYTProf::Dispatcher - Dispatcher for NYTProf plugin
+
+=head1 SEE ALSO
+
+L<Jifty::Plugin::NYTProf>, L<Jifty::Plugin::NYTProf::View>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2007 Best Practical Solutions
+
+This is free software and may be modified and distributed under the same terms as Perl itself.
+
+=cut
+
+1;
+

Added: plugins/Jifty-Plugin-NYTProf/lib/Jifty/Plugin/NYTProf/View.pm
==============================================================================
--- (empty file)
+++ plugins/Jifty-Plugin-NYTProf/lib/Jifty/Plugin/NYTProf/View.pm	Wed Apr 22 00:32:52 2009
@@ -0,0 +1,82 @@
+use strict;
+use warnings;
+
+package Jifty::Plugin::NYTProf::View;
+use Jifty::View::Declare -base;
+use Scalar::Util 'blessed';
+
+=head1 NAME
+
+Jifty::Plugin::NYTProf::View - Views for database queries
+
+=head1 TEMPLATES
+
+=cut
+
+template '/__jifty/admin/profiles/all' => page {
+    my $skip_zero = get 'skip_zero';
+
+    h1 { "Profiles" }
+    p {
+        a { attr { href => "/__jifty/admin/profiles/clear" }
+            "Clear profile log" }
+    }
+    hr {};
+
+    my $render = new_action( class => 'NYTProfHTML',
+                             moniker => 'nytprof_html' );
+    h3 { "All profiles" };
+    form {
+    table {
+        row {
+            th { "ID" }
+            th { "URL" }
+        };
+
+        for (@Jifty::Plugin::NYTProf::requests)
+        {
+            row {
+                cell {
+                    hyperlink( label => $_->{id},
+                               onclick => { submit => { action => $render,
+                                                        arguments => { id => $_->{id} } },
+                                            region => 'profile_output',
+                                            replace_with => '/__jifty/admin/profiles/_result',
+                                            arguments => { id => $_->{id} } },
+                               )
+                };
+                cell { $_->{url} };
+            };
+        }
+    };
+    };
+    render_region( name => 'profile_output' );
+};
+
+
+template '/__jifty/admin/profiles/_result' => sub {
+    my $id = get('id');
+
+    my $profile = '/_profile/'.Jifty->app_class."-$$/nytprof-$id/index.html" ;
+    div { { class is 'lightbox', style is 'background-color: white'; };
+          hyperlink( label => 'close',
+                     onclick => { replace_with => '/__jifty/empty' } );
+          iframe { { class is 'lightbox', src is $profile, width is '100%', height is '400', }};
+      };
+};
+
+
+=head1 SEE ALSO
+
+L<Jifty::Plugin::NYTProf>, L<Jifty::Plugin::NYTProf::Dispatcher>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2007 Best Practical Solutions
+
+This is free software and may be modified and distributed under the same terms as Perl itself.
+
+=cut
+
+1;
+


More information about the Jifty-commit mailing list