From f2ce1eff857c498de8feaadc3b2e373cea9fa760 Mon Sep 17 00:00:00 2001 From: Graham Knop Date: Wed, 14 Apr 2010 14:51:52 -0500 Subject: [PATCH] convert performance indicators to a Plack::Middleware::Debug panel --- lib/WebGUI/Asset/Wobject/Dashboard.pm | 1 - lib/WebGUI/Asset/Wobject/Layout.pm | 7 +- lib/WebGUI/Content/Asset.pm | 23 +------ lib/WebGUI/Macro/AssetProxy.pm | 7 +- lib/WebGUI/Middleware/Debug/Performance.pm | 77 ++++++++++++++++++++++ lib/WebGUI/Middleware/Session.pm | 2 + lib/WebGUI/Session/ErrorHandler.pm | 12 ++-- 7 files changed, 98 insertions(+), 31 deletions(-) create mode 100644 lib/WebGUI/Middleware/Debug/Performance.pm diff --git a/lib/WebGUI/Asset/Wobject/Dashboard.pm b/lib/WebGUI/Asset/Wobject/Dashboard.pm index aa8113a49..4c3f09d3c 100644 --- a/lib/WebGUI/Asset/Wobject/Dashboard.pm +++ b/lib/WebGUI/Asset/Wobject/Dashboard.pm @@ -313,7 +313,6 @@ sub view { my @found; my $newStuff; - my $showPerformance = $self->session->errorHandler->canShowPerformanceIndicators(); foreach my $position (@positions) { my @assets = split(",",$position); foreach my $asset (@assets) { diff --git a/lib/WebGUI/Asset/Wobject/Layout.pm b/lib/WebGUI/Asset/Wobject/Layout.pm index 2e26e8710..6ee065de9 100644 --- a/lib/WebGUI/Asset/Wobject/Layout.pm +++ b/lib/WebGUI/Asset/Wobject/Layout.pm @@ -327,7 +327,7 @@ Show performance indicators for the Layout and all children if enabled. sub view { my $self = shift; my $session = $self->session; - my $showPerformance = $session->errorHandler->canShowPerformanceIndicators; + my $perfLog = $session->log->performanceLogger; my @parts = split $self->{_viewSplitter}, $self->processTemplate($self->{_viewVars}, undef, $self->{_viewTemplate}); my $output = ""; @@ -342,9 +342,10 @@ sub view { my ($assetId, $outputPart) = split '~~', $part, 2; my $asset = $self->{_viewPlaceholder}{$assetId}; if (defined $asset) { - my $t = [Time::HiRes::gettimeofday()] if ($showPerformance); + my $t = $perfLog ? [Time::HiRes::gettimeofday()] : undef; my $assetOutput = $asset->view; - $assetOutput .= "Asset:".Time::HiRes::tv_interval($t) if ($showPerformance); + $perfLog->({ asset => $asset, 'time' => Time::HiRes::tv_interval($t), type => 'Layout' }) + if $perfLog; if ($self->{_viewPrintOverride}) { $session->output->print($assetOutput); } else { diff --git a/lib/WebGUI/Content/Asset.pm b/lib/WebGUI/Content/Asset.pm index 425d4f2db..616000ea6 100644 --- a/lib/WebGUI/Content/Asset.pm +++ b/lib/WebGUI/Content/Asset.pm @@ -83,28 +83,11 @@ sub handler { my ($session) = @_; my ($errorHandler, $http, $var, $asset, $request, $config) = $session->quick(qw(errorHandler http var asset request config)); my $output = ""; - if ($errorHandler->canShowPerformanceIndicators) { #show performance indicators if required + if (my $perfLog = $errorHandler->performanceLogger) { #show performance indicators if required my $t = [Time::HiRes::gettimeofday()]; $output = page($session); - $t = Time::HiRes::tv_interval($t) ; - if ($output =~ /<\/title>/) { - $output =~ s/<\/title>/ : ${t} seconds<\/title>/i; - } - else { - # Kludge. - my $mimeType = $http->getMimeType(); - if ($mimeType eq 'text/css') { - $session->output->print("\n/* Page generated in $t seconds. */\n"); - } - elsif ($mimeType =~ m{text/html}) { - $session->output->print("\nPage generated in $t seconds.\n"); - } - else { - # Don't apply to content when we don't know how - # to modify it semi-safely. - } - } - } + $perfLog->({ time => Time::HiRes::tv_interval($t), type => 'Page'}); + } else { my $asset = getAsset($session, getRequestedAssetUrl($session)); diff --git a/lib/WebGUI/Macro/AssetProxy.pm b/lib/WebGUI/Macro/AssetProxy.pm index bcbe2c03b..169bfe5d2 100644 --- a/lib/WebGUI/Macro/AssetProxy.pm +++ b/lib/WebGUI/Macro/AssetProxy.pm @@ -43,7 +43,8 @@ Defaults to 'url'. But if you want to use an assetId as the first parameter, the #------------------------------------------------------------------- sub process { my ($session, $identifier, $type) = @_; - my $t = ($session->errorHandler->canShowPerformanceIndicators()) ? [Time::HiRes::gettimeofday()] : undef; + my $perfLog = $session->log->performanceLogger; + my $t = $perfLog ? [Time::HiRes::gettimeofday()] : undef; my $asset; if ($type eq 'assetId') { $asset = eval { WebGUI::Asset->newById($session, $identifier); }; @@ -79,8 +80,8 @@ sub process { $asset->toggleToolbar; $asset->prepareView; my $output = $asset->view; - $output .= "AssetProxy:" . Time::HiRes::tv_interval($t) - if $t; + $perfLog->({ asset => $asset, time => Time::HiRes::tv_interval($t), type => 'Proxy'}) + if $perfLog; return $output; } return ''; diff --git a/lib/WebGUI/Middleware/Debug/Performance.pm b/lib/WebGUI/Middleware/Debug/Performance.pm new file mode 100644 index 000000000..f8739a1f7 --- /dev/null +++ b/lib/WebGUI/Middleware/Debug/Performance.pm @@ -0,0 +1,77 @@ +package WebGUI::Middleware::Debug::Performance; +use 5.008; +use strict; +use warnings; +use parent qw(Plack::Middleware::Debug::Base); +our $VERSION = '0.07'; + +sub panel_name { 'Asset Performance' } + +sub run { + my ($self, $env, $panel) = @_; + + my $perf_log = []; + $env->{'webgui.perf.logger'} = sub { + my $args = shift; + my $asset = $args->{asset}; + my $log_data = { + 'time' => $args->{time}, + 'type' => $args->{type}, + 'message' => $args->{message}, + $asset ? ( + 'viewUrl' => $asset->getUrl, + 'editUrl' => $asset->getUrl('func=edit'), + 'assetTitle' => $asset->title, + ) : (), + }; + push @$perf_log, $log_data; + }; + + return sub { + my $res = shift; + + $panel->nav_subtitle(scalar @$perf_log . ' events'); + if (@$perf_log) { + $panel->content($self->render_log($perf_log)); + } + }; +} + +my $log_template = __PACKAGE__->build_template(<<'EOTMPL'); + + + + + + + + + +% my $i; +% for my $event ( @{ $_[0]->{list} } ) { + + + + + +% } + +
TimeTypeItem
<%= $event->{time} %><%= $event->{type} %> +% if ($event->{message}) { + <%= $event->{message} %> +% } +% if ($event->{assetTitle}) { + View + Edit + <%= $event->{assetTitle} %> +% } +
+EOTMPL + +sub render_log { + my ($self, $events) = @_; + $self->render($log_template, { list => $events }); +} + +1; + diff --git a/lib/WebGUI/Middleware/Session.pm b/lib/WebGUI/Middleware/Session.pm index b05c28d00..76444a5e5 100644 --- a/lib/WebGUI/Middleware/Session.pm +++ b/lib/WebGUI/Middleware/Session.pm @@ -7,6 +7,7 @@ use WebGUI::Utility (); use Try::Tiny; use Plack::Middleware::StackTrace; use Plack::Middleware::Debug; +use WebGUI::Middleware::Debug::Performance; use WebGUI::Middleware::HTTPExceptions; use Plack::Middleware::ErrorDocument; use Plack::Middleware::SimpleLogger; @@ -81,6 +82,7 @@ sub call { [ 'MySQLTrace', skip_packages => qr/\AWebGUI::SQL(?:\z|::)/ ], 'Response', 'Logger', + sub { WebGUI::Middleware::Debug::Performance->wrap($_[0]) }, ], ); } diff --git a/lib/WebGUI/Session/ErrorHandler.pm b/lib/WebGUI/Session/ErrorHandler.pm index 0cbb41093..1f0796785 100644 --- a/lib/WebGUI/Session/ErrorHandler.pm +++ b/lib/WebGUI/Session/ErrorHandler.pm @@ -21,6 +21,7 @@ use JSON; use HTML::Entities qw(encode_entities); use Log::Log4perl; use WebGUI::Exception; +use Sub::Uplevel; =head1 NAME @@ -84,10 +85,13 @@ Returns true if the user meets the conditions to see performance indicators and =cut -sub canShowPerformanceIndicators { - my $self = shift; - return 0 unless $self->session->setting->get("showPerformanceIndicators"); - return $self->canShowBasedOnIP('debugIp'); +sub performanceLogger { + my $self = shift; + my $request = $self->session->request; + return + unless $request; + my $logger = $request->env->{'webgui.perf.logger'}; + return $logger; }