Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

PLUGIN: (block function) timeblock

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon May 12, 2003 4:25 am    Post subject: PLUGIN: (block function) timeblock Reply with quote

Hi Hi.
    EDIT: Now works with 2.5.0 (and possibly earlier) -- thanks to messju.
    03 Nov 01: See this post for an updated and more flexible approach.

I've been running a few timing tests recently and decided that I needed a way to conveniently time arbitrary sections of template code.

Without further ado, here is block.timeblock.php:

[php:1:cd1761476c]<?php
/**
* Smarty block plugin
* -------------------------------------------------------------
* Type: block
* Name: timeblock
* Version: 1.4
* Author: boots
* Purpose: time the execution of a block
* -------------------------------------------------------------
* @param name optional if specified, adds timing info to template var array 'timeblock'
* @param title optional Title information to display at top of block
* @param info optional Additional information to display before timing result
*/
function smarty_block_timeblock( $params, $content, &$this )
{
static $block_css = "background:LightYellow; border-left:3 solid green; border-bottom:3 solid white;";
static $block_text = "Elapsed Time ";
static $title_css = "font-weight:bold; color:DarkGreen; padding-left:5;";
static $output_css = "background:Azure; margin-left:5; padding-left:2;";
static $info_css = "color:DarkGreen; padding-left:5;";

if ( !isset($content) ) {

$this->_timeblocks[] = microtime();

} else {

$_start = array_pop( $this->_timeblocks );
list( $a_micro, $a_int ) = explode( ' ', $_start );
list( $b_micro, $b_int ) = explode( ' ', microtime() );
$elapsed = ( $b_int - $a_int ) + ( $b_micro - $a_micro );


if ( !empty( $params['name'] ) ) {

$_name = '['. $params['name'] . '] ';
$this->_tpl_vars['timeblock'][$params['name']] = $elapsed;

} else {

$_name = '';

}

if ( !empty( $params['info'] ) ) {

$_block_text = $_name . $params['info'] . ' ';

} else {

$_block_text = $_name . $block_text;

}

if ( !empty($params['title'] ) ) {

$_title_text = '<div style="' . $title_css . '">'
. $_name . $params['title']
. '</div>';

} else {

$_title_text = '';

}

$output = '<div style="'.$block_css.'">'
. $_title_text
. '<div style="'.$output_css.'">'
. $content
. '</div>'
. '<div style="'.$info_css.'">'
. $_block_text . $elapsed . 's'
. '</div>'
. '</div>';

return $output;

}
}
?>[/php:1:cd1761476c]

I only expect this to be good enough for relative comparisons. There is not too much in there in terms of security and robustness--be gentle: this is meant for developers, not end-users. I hope that you're going to hate my styling choices--they are ugly and obtrusive so as to remind you to remove the timing code from your templates Wink Still, I abstracted it a little bit so that you can change it easily enough.

Example:
Code:
{timeblock title="Time Math Implementations" info="Completed In:"}
    {timeblock name="math"}
        {section name=test loop=10000}
            {math equation="x+1" x=$x assign=x}
        {/section}
    {/timeblock}
    {timeblock name="s_math"}
        {section name=test loop=10000}
            {s_math equation="x+1" x=$x assign=x}
        {/section}
    {/timeblock}
    s_math : math = {math|round:"1" equation="(x-y)/y" x=$timeblock.math y=$timeblock.s_math} : 1<br>
{/timeblock}


Not only are timeblocks nestable, but using the optional name parameter adds the timing information for the timeblock to the template array var timeblock which can be used to calculate relative measures, etc.

Suggestions are welcome.

Have fun!


Last edited by boots on Sat Nov 01, 2003 1:02 pm; edited 7 times in total
Back to top
View user's profile Send private message
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 235
Location: Purdue University

PostPosted: Mon May 12, 2003 10:54 am    Post subject: Reply with quote

Interesting, will be very useful for pseudo benchmarks
_________________
"Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed May 14, 2003 12:39 am    Post subject: Reply with quote

Quote:
will be very useful for pseudo benchmarks


Exactly -- good to determine the relative performance of otherwise equal techniques. Also handy for plugin developers to test the performance of code in-place.

Never-the-less, the timing code makes no attempt to level the playing field for separate timing blocks (ie. the environment is not "flushed"), so a if you are timing two sequential blocks that each iterate the same array (for example), the first one is *likely* to run a little bit slower since the second one will have the benefit of having at least portions of the array already in 'optimized'. That's a poor way of describing it, but the point is, fairer tests require that you vary the execution order as well as separated test cases.

Still, I've already used this to discover some very interesting things about my code, my plugins and Smarty in general. I hope that others find it useful as well.
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri May 23, 2003 3:34 pm    Post subject: Reply with quote

@boots: i only skimmed over it, but it seems you use $repeat only to determine the start- or end-call of the block.

if you change "if ($repeat)" to "if (!isset($content))" then your plugin should also work with 2.5.0 stable and older versions of smarty.

just a thought
greetings
messju
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri May 23, 2003 5:47 pm    Post subject: Reply with quote

messju wrote:
if you change "if ($repeat)" to "if (!isset($content))" then your plugin should also work with 2.5.0 stable and older versions of smarty.


@messju:

Good point. I think my original intention was to have a built-in looping mechanism, but I decided against that and forgot that I wasn't tied to using $repeat anymore.

I updated the code to reflect your suggestion.

Thanks for the tip!
Back to top
View user's profile Send private message
Frank
Smarty Rookie


Joined: 18 Apr 2003
Posts: 6

PostPosted: Mon May 26, 2003 12:54 pm    Post subject: Reply with quote

Yo Boots: I keep getting this error Sad

Fatal error: Smarty error: [in plaintext/message.tpl line 11]: [plugin] unknown tag - 's_math' (core.load_plugins.php, line 117) in /usr/local/smarty/libs/Smarty.class.php on line 1678

I'm running the latest CVS Version (updated 2 minutes ago)
Back to top
View user's profile Send private message
aloner
Smarty Rookie


Joined: 24 Apr 2003
Posts: 24

PostPosted: Mon May 26, 2003 1:24 pm    Post subject: Reply with quote

's_math' is a plugin for (hopefully) faster math in Smarty. You should install it if you want to run the provided template.

It's in 'plugins' forum.
_________________
Your ad here.
Back to top
View user's profile Send private message
ShoAn
Smarty Rookie


Joined: 04 May 2004
Posts: 5

PostPosted: Tue May 04, 2004 3:14 pm    Post subject: Reply with quote

hmm, i get this error:

Fatal error: Using $this when not in object context in D:\httpd\test\includes\smarty\plugins\block.timeblock.php on line 25

can somebody help?
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Tue May 04, 2004 3:30 pm    Post subject: Reply with quote

this is one of php5's great new OO-features everybody is so excited about Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website
ShoAn
Smarty Rookie


Joined: 04 May 2004
Posts: 5

PostPosted: Tue May 04, 2004 4:57 pm    Post subject: Reply with quote

hehe, right. i have php5 Wink

but what can i do?

at any time php5 is standart ... so sombody have to fix this problem as soon as possible, i think

i would do it, if i could do it Wink
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Tue May 04, 2004 5:05 pm    Post subject: Reply with quote

ShoAn wrote:
i would do it, if i could do it Wink


just take the plugin and replace all occurences of "$this" with "$smarty". that should help.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
ShoAn
Smarty Rookie


Joined: 04 May 2004
Posts: 5

PostPosted: Tue May 04, 2004 5:43 pm    Post subject: Reply with quote

ui, i think it works, but with ca. 1000x "Warning: Smarty error: math: parameter x is empty in D:\httpd\test\includes\smarty\Smarty.class.php on line 1102" Shocked and the page was loading about five seconds Crying or Very sad

what could it be?
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP