Jump Start - Simple performance measurement

Now we build a plugin skeleton to measure the time lapsed to execute the script. This maybe useful for plugin performance measurement. Therefore we introduce a switch (DEBUG_MODE_ACTIVE) at the beginning to use the function only for such purpose. DokuWiki delivers a global config parameter, which may be used instead (→config:allowdebug). To keep the focus on the main things, we reduced the simple script code to just stopping the script execution. For performance measurement issues it is necessary that the controlling function do not cause add significant delays. The proposed get_execution_time function is very small and simple to follow this rule.

Info

     DokuWiki has two different Cache methods. It differs between XHTML and Instructions. To put only ~~NOCACHE~~
     on a page will cause re-rendering of XHTML but ignores the Instruction cache.

     Therefore this simple method of perormance measuring has to be initialized within the render method. The time consumption
     caused by handler method will be ignored. That leads into improved measure of time your plugin code causes in the event
     your handler function does not call much others. For further details about caching please refer to Caching.


     It is also possible to use a debug switching based on syntax line parameter of your plugin instead of globally turning on
     the DW debugging or using a constant within the script (e.g. you could use dbg=on as first parameter → to check
     this look on code line 29). Consider to turn all used const DEBUG_MODE_ACTIVE into var $DEBUG_MODE_ACTIVE and
     delete code line 10.













The php-Code

Search the following code for DEBUG_MODE_ACTIVE and consider

  • style of output is stored to the style.css file to keep focused on the main things
  • get_execution_time can be stored to a separate php-file, which then has to be included into your syntax.php
  • you can trigger get_execution_time multiple times and receive the time passed since its first call
syntax.php
*/ if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); require_once(DOKU_PLUGIN.'syntax.php'); define('DEBUG_MODE_ACTIVE',TRUE); //to show some information if set to true /******************************************************************************/ /* All DokuWiki plugins to extend the parser/rendering mechanism * * need to inherit from this class */ class syntax_plugin_xm2stuff extends DokuWiki_Syntax_Plugin { function getInfo() { return confToHash(dirname(__FILE__).'/plugin.info.txt'); } function getType() { return 'substition';} // Returns the type of syntax this plugin defines function getPType(){ return 'block';} // Defines how this syntax is handled regarding paragraphs function getSort() { return 999;} // Returns a number used to determine in which order modes are add function connectTo($mode){ $this->Lexer->addSpecialPattern('\{\[^}]*\>\}',$mode,'plugin_xm2stuff'); } function handle($match, $state, $pos, &$handler) { $match = substr($match,strlen('{'),-2); $params = explode(',',$match); return $params; } /******************************************************************************/ /* Create output */ function render($mode, &$renderer, $params) { // if(stripos($params[0],'dbg=on') !== false) $DEBUG_MODE_ACTIVE = TRUE; // set initial timestamp before doing anything else // to be done within renderer method else cache handling of instructions // would ignore this if called e.g. within handle method if(DEBUG_MODE_ACTIVE != false) { $this->get_execution_time(); } // replace following line by your real plugin code usleep(2000000); // the script will sleep for the next 2 seconds // call the function $this->get_execution_time() and output if DEBUG mode // put the styled output of time measurement below all if(DEBUG_MODE_ACTIVE != false) { $output .= 'passed time after usleep
'.NL. '(Script execution 1: '.$this->get_execution_time().' seconds)

'.NL; } /********************************************************************/ /* finally, when all the work was done, handover output to renderer */ /********************************************************************/ $renderer->doc .= $output; } /******************************************************************************/ /* get execution time in seconds at current point of call * * @return float Execution time at this point of call * * source: http://www.php.net/microtime */ function get_execution_time() { static $microtime_start = null; if($microtime_start === null) { $microtime_start = microtime(true); return 0.0; } return microtime(true) - $microtime_start; } }
  1. <?php
  2. /***************************************************************************************
  3. *  xm2stuff Plugin: Plugin Dev Jumpstart - skeleton for simple performance measurement *
  4. * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)                             *
  5. * @author     Taggic <taggic@t-online.de>                                              */
  6. if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
  7. if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
  8. require_once(DOKU_PLUGIN.'syntax.php');  
  9.  
  10. define('DEBUG_MODE_ACTIVE',TRUE);         //to show some information if set to true
  11.    
  12. /******************************************************************************/
  13. /* All DokuWiki plugins to extend the parser/rendering mechanism               *
  14.  * need to inherit from this class                                             */
  15. class syntax_plugin_xm2stuff extends DokuWiki_Syntax_Plugin
  16. {   function getInfo() { return confToHash(dirname(__FILE__).'/plugin.info.txt'); }
  17.     function getType() { return 'substition';} // Returns the type of syntax this plugin defines
  18.     function getPType(){ return 'block';}      // Defines how this syntax is handled regarding paragraphs
  19.     function getSort() { return 999;}         // Returns a number used to determine in which order modes are add
  20.     function connectTo($mode){
  21.              $this->Lexer->addSpecialPattern('\{\<xm2stuff>[^}]*\>\}',$mode,'plugin_xm2stuff'); }
  22.     function handle($match, $state, $pos, &$handler) {        
  23.       $match  = substr($match,strlen('{<xm2stuff>'),-2);
  24.       $params = explode(',',$match); return $params; }
  25.  
  26. /******************************************************************************/
  27. /* Create output                                                              */
  28. function render($mode, &$renderer, $params) {
  29. //     if(stripos($params[0],'dbg=on') !== false) $DEBUG_MODE_ACTIVE = TRUE;
  30.     // set initial timestamp before doing anything else
  31.     // to be done within renderer method else cache handling of instructions
  32.     // would ignore this if called e.g. within handle method
  33.     if(DEBUG_MODE_ACTIVE != false) { $this->get_execution_time(); }
  34.  
  35.      // replace following line by your real plugin code
  36.      usleep(2000000);  // the script will sleep for the next 2 seconds
  37.      
  38.      //  call the function $this->get_execution_time() and output if DEBUG mode
  39.      //  put the styled output of time measurement below all
  40.      if(DEBUG_MODE_ACTIVE != false) {
  41.         $output .= '<span>passed time after usleep</span><div class="xm2stuff">'.NL.
  42.                    '(Script execution 1: '.$this->get_execution_time().' seconds)</div><br />'.NL; }    
  43.  
  44.     /********************************************************************/
  45.     /* finally, when all the work was done, handover output to renderer */
  46.     /********************************************************************/        
  47.      $renderer->doc .=  $output;
  48. }
  49. /******************************************************************************/
  50. /* get execution time in seconds at current point of call                     *
  51.  * @return float Execution time at this point of call                         *
  52.  * source: http://www.php.net/microtime                                       */
  53.   function get_execution_time()
  54.   {   static $microtime_start = null;
  55.       if($microtime_start === null)
  56.       {   $microtime_start = microtime(true);
  57.           return 0.0; }    
  58.       return microtime(true) - $microtime_start;
  59.   }  
  60. }
  61.  

The Syntax

{<xm2stuff>dbg=on,a,b,c>}

The Output

The Plugin

To get the complete plugin please download: xm2stuff.zip





see also

tips/perf_plugin.txt · Last modified: 2014/04/30 09:36 (external edit)

This Wiki is hosted and ruled by Policies of Frister Consultancy Services.
By using this wiki you accept these rules. -> Impressum