<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jon Schutz Technical Notes and Recommendations &#187; Catalyst MVC Framework</title>
	<atom:link href="http://notes.jschutz.net/topics/catalyst-mvc-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://notes.jschutz.net</link>
	<description>Useful snippets technical info and recommendations</description>
	<lastBuildDate>Wed, 02 Mar 2011 12:01:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Adding Action Timings to your Catalyst Output</title>
		<link>http://notes.jschutz.net/2008/04/adding-action-timings-to-your-catalyst-output/</link>
		<comments>http://notes.jschutz.net/2008/04/adding-action-timings-to-your-catalyst-output/#comments</comments>
		<pubDate>Sat, 19 Apr 2008 14:22:27 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Catalyst MVC Framework]]></category>
		<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://notes.jschutz.net/17/perl/adding-action-timings-to-your-catalyst-output</guid>
		<description><![CDATA[About a year ago, onemogin wrote an article on adding action timings to the HTML output of a Catalyst app.  To do so, it was necessary to access $c-&#62;stats, which at the time was an internal object (that is, there was no published API for it) and therefore subject to change.  As of [...]]]></description>
			<content:encoded><![CDATA[<p>About a year ago, onemogin wrote an article on <a href="http://www.onemogin.com/blog/559-adding-action-timings-to-your-output.html" target="_blank">adding action timings to the HTML output of a Catalyst app</a>.  To do so, it was necessary to access $c-&gt;stats, which at the time was an internal object (that is, there was no published API for it) and therefore subject to change.  As of Catalyst-Runtime 5.7012, $c-&gt;stats has a defined interface and returns a Catalyst::Stats object (or your own class, if you provide one) rather than the Tree::Simple object that it used to.</p>
<p>It&#8217;s easy to fix your code to work with 5.7012.  Onemogin&#8217;s code in the end() method looked like this:</p>
<pre><span style="color: #b1b100">  my</span> <span style="color: #0000ff">$tree</span> = <span style="color: #0000ff">$c</span>-&gt;<span style="color: #006600">stats</span><span style="color: #66cc66">(</span><span style="color: #66cc66">)</span>;

<span style="color: #b1b100">  my</span> <span style="color: #0000ff">$dvisit</span> = <span style="color: #000000; font-weight: bold">new</span> Tree::<span style="color: #006600">Simple</span>::<span style="color: #006600">Visitor</span><span style="color: #66cc66">(</span><span style="color: #66cc66">)</span>;
<span style="color: #0000ff">  $tree</span>-&gt;<span style="color: #006600">accept</span><span style="color: #66cc66">(</span><span style="color: #0000ff">$dvisit</span><span style="color: #66cc66">)</span>;
<span style="color: #0000ff">  $c</span>-&gt;<span style="color: #006600">stash</span>-&gt;<span style="color: #66cc66">{</span><span style="color: #ff0000">'action_stats'</span><span style="color: #66cc66">}</span> = <span style="color: #0000ff">$dvisit</span>-&gt;<span style="color: #006600">getResults</span><span style="color: #66cc66">(</span><span style="color: #66cc66">)</span>;</pre>
<p>which needs to become this:</p>
<pre>  my @report = $c-&gt;stats-&gt;report;
  $c-&gt;stash-&gt;{action_stats}= \@report;</pre>
<p>and your template will also need to change; here&#8217;s an example:</p>
<p><pre> 
 &lt;div id="stats"&gt;
 &lt;table border="0" cellspacing="0" cellpadding="0"&gt;
 [% space = '&amp;nbsp;&amp;nbsp;' %]
 &lt;tr&gt;&lt;th&gt;Action&lt;/th&gt;&lt;th&gt;Time&lt;/th&gt;&lt;/tr&gt;
 [% FOREACH r=action_stats %]
 &lt;tr&gt;&lt;td class="description"&gt;[% space.repeat(r.0) %][% r.1 | html %]&lt;/td&gt;
&lt;td class="elapsed"&gt;[% UNLESS r.3 %]+[% END %][% r.2 %]s&lt;/td&gt;&lt;/tr&gt;
 [% END %]
 &lt;/table&gt;
 &lt;/div&gt;
</pre></p>
<p>to produce an end result such as:</p>
<p><style type="text/css">
#stats { 
       font-family: arial,helvetica,sans-serif; 
       font-size: 100%;
       background: #E0E0E0;
       margin: 0;
       padding: 0;
       border: solid 1px #F00000; 
       width: 400px; 
       }

#stats table {
       width: 100%;
       border: 0;
}

#stats th {
       color: #FFFFFF;
       font-size: 120%;
       font-weight: bold;
       background: #D47878;
       border: 0; 
       }
#stats td {
       color: #000000;
       border: 0;
       line-height: 120%;
       padding-left: 5%;
       }
#stats td.elapsed {
       background: #F8F8F8;
       padding-left: 5%;
}
</style>
<div  id="stats">
<table border="0" cellspacing="0" cellpadding="0">

<tr><th>Action</th><th>Time</th></tr>

<tr><td class="description">/default</td><td class="elapsed">0.005895s</td></tr>

<tr><td class="description">&nbsp;&nbsp;-&gt; /look_left</td><td class="elapsed">0.00091s</td></tr>

<tr><td class="description">&nbsp;&nbsp;&nbsp;&nbsp;- starting critical bit</td><td class="elapsed">+0.000479s</td></tr>

<tr><td class="description">&nbsp;&nbsp;&nbsp;&nbsp;- critical bit complete</td><td class="elapsed">+0.000208s</td></tr>

<tr><td class="description">&nbsp;&nbsp;-&gt; /look_right</td><td class="elapsed">0.000587s</td></tr>

<tr><td class="description">&nbsp;&nbsp;-&gt; /look_left</td><td class="elapsed">0.000799s</td></tr>

<tr><td class="description">&nbsp;&nbsp;&nbsp;&nbsp;- starting critical bit</td><td class="elapsed">+0.000441s</td></tr>

<tr><td class="description">&nbsp;&nbsp;&nbsp;&nbsp;- critical bit complete</td><td class="elapsed">+0.000169s</td></tr>

<tr><td class="description">&nbsp;&nbsp;-&gt; /cross_over</td><td class="elapsed">0.001766s</td></tr>

<tr><td class="description">/end</td><td class="elapsed">0.000462s</td></tr>

</table>
</div>
</p>
<p>Here&#8217;s the bit of controller code that generated the example:</p>
<p><pre>
sub default : Private {
    my ( $self, $c ) = @_;

    $c->forward('look_left');
    $c->forward('look_right');
    $c->forward('look_left');
    $c->forward('cross_over');
}

sub look_left : Private {
    my ( $self, $c ) = @_;
    for (1 .. 100) {};
    $c->stats->profile("starting critical bit");
    for (1 .. 100) {};
    $c->stats->profile("critical bit complete");
}

sub look_right : Private {
    for (1 .. 1000) {};
}
sub cross_over : Private {
    for (1 .. 10000) {};
}

sub end : ActionClass('RenderView') {
    my ( $self, $c ) = @_;
    my @report = $c->stats->report;
    $c->stash->{action_stats}= \@report;
}

</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://notes.jschutz.net/2008/04/adding-action-timings-to-your-catalyst-output/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

