OSDN Git Service

v0.4
authorkadota <kadota@1ca29b6e-896d-4ea0-84a5-967f57386b96>
Fri, 12 Dec 2008 09:50:14 +0000 (09:50 +0000)
committerkadota <kadota@1ca29b6e-896d-4ea0-84a5-967f57386b96>
Fri, 12 Dec 2008 09:50:14 +0000 (09:50 +0000)
git-svn-id: https://svn.sourceforge.jp/svnroot/nucleus-jp/plugin@722 1ca29b6e-896d-4ea0-84a5-967f57386b96

trunk/NP_Benchmark/NP_Benchmark.php

index 06bd874..35669c1 100644 (file)
-<?php
-/*     
-       This program is free software; you can redistribute it and/or
-       modify it under the terms of the GNU General Public License
-       as published by the Free Software Foundation; either version 2
-       of the License, or (at your option) any later version.
-       (see nucleus/documentation/index.html#license for more info)
-       
-       History
-       -------
-       Ver0.3 2007/07/05 average, memory usage (commented out now)
-       Ver0.2 2006/05/01 improved by sato(na)
-       Ver0.1 2004/03/08 initial release.
-*/
-
-class NP_Benchmark extends NucleusPlugin { 
-       function getName()             { return 'Benchmark'; } 
-       function getAuthor()           { return 'yu + sato(na)'; } 
-       function getURL()              { return 'http://works.datoka.jp/?itemid=224'; } 
-       function getVersion()          { return '0.3'; } 
-       function getMinNucleusVersion(){ return 330; }
-       function getTableList()        { return array(); }
-       function supportsFeature($w)   { return ($w == 'SqlTablePrefix') ? 1 : 0; }
-       function getEventList()        { return array( 'PreSkinParse','PostSkinParse' ); }
-       function getDescription()      { return 'Benchmark'; }
-       function install()             {
-               $this->createOption("TeamDisp", "Benchmark for blog team only", "yesno", "yes");
-               $this->createOption("AutoStart", "Start benchmark automatically on PreSkinParse", "yesno", "yes");
-               $this->createOption("PostSkinParseDisp", "Output on PostSkinParse", "yesno", "no");
-               $this->createOption("UseComment", "Use comment tag for output", "yesno", "no");
-               $this->createOption("UseSession", "Use session", "yesno", "no");
-       }
-       function init() {
-               global $member, $blogid;
-               $this->TeamDisp  = ($this->getOption('TeamDisp') == 'yes' and $member->isTeamMember($blogid)) ? TRUE : FALSE;
-               if ($this->TeamDisp and $this->getOption('UseSession') == 'yes') session_start();
-       }
-       function doAction($type) {
-               switch ($type) {
-               case 'reset':
-                       session_unset();
-                       break;
-               }
-               redirect( serverVar('HTTP_REFERER') );
-       }
-       function doTemplateVar(&$item, $disp=FALSE){ $this->benchmark($disp); }
-       function doSkinVar($skinType, $disp=FALSE) { $this->benchmark($disp); }
-       function event_PreSkinParse($data) {
-               global $benchmark_start;
-               if ($this->getOption('AutoStart') == 'yes') $benchmark_start = microtime();
-       }
-       function event_PostSkinParse($data) {
-               if ($this->getOption('PostSkinParseDisp') == 'yes') $this->benchmark(FALSE);
-       }
-       function benchmark($disp=FALSE){
-               global $benchmark_start, $SQLCount;
-               
-               if ($this->getOption('TeamDisp') == 'yes' and $this->TeamDisp == FALSE) return;
-               if (empty($benchmark_start)) {
-                       $benchmark_start = microtime();
-                       return;
-               }
-               
-               list($msec1, $sec1) = explode(' ', $benchmark_start);
-               list($msec2, $sec2) = explode(' ', microtime());
-               $diff = (float) $sec2 + (float) $msec2 - (float) $sec1 - (float) $msec1;
-               if ($this->getOption('UseComment') == 'yes') {
-                       $pre = '<!--';
-                       $post = '-->';
-               }
-               else {
-                       $pre = '[';
-                       $post = ']';
-               }
-               echo $pre;
-               echo (int)$SQLCount.'q./'.
-                       //number_format(memory_get_usage()).'bites/'. // returns apache's process size on xampp/win ...
-                       number_format($diff, 3).'sec.';
-               if ($this->getOption('UseSession') == 'yes') {
-                       if ( empty($disp) ) $disp = 'default';
-                       $_SESSION[ $disp ][] = $diff;
-                       $cnt = count($_SESSION[ $disp ]);
-                       $avr_sec = number_format(array_sum($_SESSION[ $disp ]) / $cnt, 3);
-                       echo '/<a href="'. $CONF['ActionURL'] .'?action=plugin&amp;name=Benchmark&amp;type=reset">AVR '.$avr_sec.'sec.('.$cnt.')</a>';
-               }
-               echo $post;
-       }
-}
-
-if( !function_exists('memory_get_usage') )
-{
-       function memory_get_usage()
-       {
-               //If its Windows
-               //Tested on Win XP Pro SP2. Should work on Win 2003 Server too
-               //Doesn't work for 2000
-               //If you need it to work for 2000 look at http://us2.php.net/manual/en/function.memory-get-usage.php#54642
-               if ( substr(PHP_OS,0,3) == 'WIN')
-               {
-                               if ( substr( PHP_OS, 0, 3 ) == 'WIN' )
-                               {
-                                       $output = array();
-                                       exec( 'tasklist /FI "PID eq ' . getmypid() . '" /FO LIST', $output );
-                                       return preg_replace( '/[\D]/', '', $output[5] ) * 1024;
-                               }
-               }else
-               {
-                       //We now assume the OS is UNIX
-                       //Tested on Mac OS X 10.4.6 and Linux Red Hat Enterprise 4
-                       //This should work on most UNIX systems
-                       $pid = getmypid();
-                       exec("ps -eo%mem,rss,pid | grep $pid", $output);
-                       $output = explode("  ", $output[0]);
-                       //rss is given in 1024 byte units
-                       return $output[1] * 1024;
-               }
-       }
-}
-
+<?php\r
+/*     \r
+       This program is free software; you can redistribute it and/or\r
+       modify it under the terms of the GNU General Public License\r
+       as published by the Free Software Foundation; either version 2\r
+       of the License, or (at your option) any later version.\r
+       (see nucleus/documentation/index.html#license for more info)\r
+       \r
+       NP_Benchmark by yu (http://nucleus.datoka.jp/)\r
+       \r
+       Usage\r
+       -----\r
+       <%Benchmark(stylesheet)%> //output embedded stylesheet for head block\r
+       \r
+       <%Benchmark(foo)%> //benchmark label "foo"\r
+       <%Benchmark(bar)%> //benchmark label "bar"\r
+       \r
+       \r
+       History\r
+       -------\r
+       Ver0.4  2008/12/12 Add more options for output. Add embedding styles. (yu)\r
+       Ver0.31 2008/06/18 Add labels to calc diff time. Add CSS "level" class. (yu)\r
+       Ver0.3  2007/07/05 Add average time (use session) and memory usage. Memory usage is disabled in default. (yu)\r
+       Ver0.2  2006/05/01 Improved and add some features. (sato(na))\r
+       Ver0.1  2004/03/08 Initial release. (yu)\r
+*/\r
+\r
+class NP_Benchmark extends NucleusPlugin { \r
+       function getName()             { return 'Benchmark'; } \r
+       function getAuthor()           { return 'yu + sato(na)'; } \r
+       function getURL()              { return 'http://works.datoka.jp/plugins/224.html'; } \r
+       function getVersion()          { return '0.4'; } \r
+       function getMinNucleusVersion(){ return 330; }\r
+       function getTableList()        { return array(); }\r
+       function supportsFeature($w)   { return ($w == 'SqlTablePrefix') ? 1 : 0; }\r
+       function getEventList()        { return array( 'PreSkinParse' ); }\r
+       function getDescription()      { return 'Benchmark'; }\r
+       \r
+       function install()             {\r
+               $this->createOption("EnableThis", "Enable benchmarking", "yesno", "yes");\r
+               $this->createOption("EnabledFor", "Benchmark is enabled for:", "select", "admin", "Admin|admin|Team Member|team|All|all");\r
+               $this->createOption("AutoStart",  "Start benchmark automatically on PreSkinParse", "yesno", "yes");\r
+               $this->createOption("UseComment", "Use comment tag for output", "yesno", "no");\r
+               $this->createOption("ShowSQL", "Show SQL count", "yesno", "yes");\r
+               $this->createOption("ShowMem", "Show memory usage", "yesno", "no");\r
+               $this->createOption("ShowDiff", "Show diffs of several benchmark points", "yesno", "no");\r
+               $this->createOption("ShowAverage", "Show average of diffs", "yesno", "no");\r
+       }\r
+       \r
+       var $flg_enabled;\r
+       var $flg_comment;\r
+       var $flg_sql;\r
+       var $flg_mem;\r
+       var $flg_diff;\r
+       var $flg_average;\r
+       var $member;\r
+       \r
+       function init() {\r
+               global $member, $blogid;\r
+               \r
+               if (!$blogid) {\r
+                       $this->flg_enabled = false;\r
+                       return;\r
+               }\r
+               \r
+               $this->member = $this->getOption('EnabledFor');\r
+               $this->flg_enabled = ($this->getOption('EnableThis') == 'yes');\r
+               $this->flg_comment = ($this->getOption('UseComment') == 'yes');\r
+               $this->flg_sql     = ($this->getOption('ShowSQL') == 'yes');\r
+               $this->flg_mem     = ($this->getOption('ShowMem') == 'yes');\r
+               $this->flg_diff    = ($this->getOption('ShowDiff') == 'yes');\r
+               $this->flg_average = ($this->getOption('ShowAverage') == 'yes' and $this->member != 'all');\r
+               \r
+               switch ($this->member) {\r
+               case 'admin':\r
+                       if (!$member->isAdmin($blogid)) $this->flg_enabled = false;\r
+                       else if($this->flg_average) session_start();\r
+                       break;\r
+               case 'team':\r
+                       if (!$member->isTeamMember($blogid)) $this->flg_enabled = false;\r
+                       else if($this->flg_average) session_start();\r
+                       break;\r
+               }\r
+       }\r
+       \r
+       function event_PreSkinParse($data) {\r
+               if (!$this->flg_enabled) return;\r
+               \r
+               global $benchmark_start;\r
+               if ($this->getOption('AutoStart') == 'yes') $benchmark_start = microtime();\r
+       }\r
+       \r
+       function doAction($type) {\r
+               global $member;\r
+               \r
+               switch ($type) {\r
+               case 'reset':\r
+                       if (!$member->isLoggedIn()) return;\r
+                       session_start();\r
+                       $_SESSION = array();\r
+                       if (isset($_COOKIE[session_name()])) {\r
+                               setcookie(session_name(), '', time()-42000, '/');\r
+                       }\r
+                       session_destroy();\r
+                       break;\r
+               }\r
+               redirect( serverVar('HTTP_REFERER') );\r
+       }\r
+       \r
+       function doTemplateVar(&$item, $label=''){ $this->benchmark($label); }\r
+       function doSkinVar($skinType, $label='') {\r
+               if ($label == 'stylesheet') $this->embedStylesheet();\r
+               else $this->benchmark($label);\r
+       }\r
+       \r
+       var $lastsec;\r
+       var $bmcnt = 0;\r
+       function benchmark($label=''){\r
+               global $benchmark_start, $SQLCount, $CONF;\r
+               \r
+               if (!$this->flg_enabled) return;\r
+               if (empty($benchmark_start)) {\r
+                       $benchmark_start = microtime();\r
+                       return;\r
+               }\r
+               \r
+               $this->bmcnt++;\r
+               list($msec1, $sec1) = explode(' ', $benchmark_start);\r
+               list($msec2, $sec2) = explode(' ', microtime());\r
+               $start = (float) $sec1 + (float) $msec1;\r
+               $end   = (float) $sec2 + (float) $msec2;\r
+               $diff = $end - $start;\r
+               if (!$this->lastsec) $this->lastsec = $start;\r
+               $diff2 = $end - $this->lastsec;\r
+               \r
+               if ($this->flg_comment) {\r
+                       $pre = '<!--';\r
+                       $post = '-->';\r
+               }\r
+               else {\r
+                       $lv = (int)($diff2 * 10); //unit:0.1sec.\r
+                       $lv = ($lv > 10) ? 10 : $lv;\r
+                       $pre  = '<span class="benchmark level'. $lv .'" title="['. $this->bmcnt.']'.$label .'">';\r
+                       $post = '</span>';\r
+               }\r
+               \r
+               echo $pre;\r
+               if ($this->flg_sql) echo (int)$SQLCount.'q. ';\r
+               if ($this->flg_mem) echo number_format(memory_get_usage()).'b. '; // returns apache's process size on xampp/win ...\r
+               echo number_format($diff, 2).'s. ';\r
+               if ($this->flg_diff) echo '+'. number_format($diff2, 2) .'s. ';\r
+               \r
+               if ($this->flg_average) {\r
+                       if ( empty($label) ) $label = 'default';\r
+                       $_SESSION[ $label ][] = $diff2;\r
+                       $cnt = count($_SESSION[ $label ]);\r
+                       $avr_sec = number_format(array_sum($_SESSION[ $label ]) / $cnt, 2);\r
+                       echo '+'.$avr_sec.'s./avr.('.$cnt.') ';\r
+                       if ($this->bmcnt == 1) {\r
+                               echo '[<a href="'. $CONF['ActionURL'] .'?action=plugin&amp;name=Benchmark&amp;type=reset" title="session reset">x</a>]';\r
+                       }\r
+               }\r
+               echo $post;\r
+               $this->lastsec = $end;\r
+       }\r
+       \r
+       function embedStylesheet() {\r
+               echo <<< EOH\r
+<style type="text/css">\r
+<!--\r
+span.benchmark {\r
+       border: 1px solid black;\r
+       background-color: #000000;\r
+       padding: 1px;\r
+       color: white;\r
+       font-size: 9px;\r
+       line-height: 1em;\r
+}\r
+span.benchmark a {\r
+       color: #ccc;\r
+}\r
+span.benchmark.level0  { background-color: #113333; }\r
+span.benchmark.level1  { background-color: #336666; }\r
+span.benchmark.level2  { background-color: #666633; }\r
+span.benchmark.level3  { background-color: #aa9911; }\r
+span.benchmark.level4  { background-color: #ddaa00; }\r
+span.benchmark.level5  { background-color: #ffcc00; }\r
+span.benchmark.level6  { background-color: #ff9900; }\r
+span.benchmark.level7  { background-color: #ff6600; }\r
+span.benchmark.level8  { background-color: #ff3311; }\r
+span.benchmark.level9  { background-color: #ff0033; }\r
+span.benchmark.level10 { background-color: #ff0066; }\r
+-->\r
+</style>\r
+EOH;\r
+       }\r
+\r
+}\r
+\r
+if( !function_exists('memory_get_usage') )\r
+{\r
+       function memory_get_usage()\r
+       {\r
+               //If its Windows\r
+               //Tested on Win XP Pro SP2. Should work on Win 2003 Server too\r
+               //Doesn't work for 2000\r
+               //If you need it to work for 2000 look at http://us2.php.net/manual/en/function.memory-get-usage.php#54642\r
+               if ( substr(PHP_OS,0,3) == 'WIN')\r
+               {\r
+                               if ( substr( PHP_OS, 0, 3 ) == 'WIN' )\r
+                               {\r
+                                       $output = array();\r
+                                       exec( 'tasklist /FI "PID eq ' . getmypid() . '" /FO LIST', $output );\r
+                                       return preg_replace( '/[\D]/', '', $output[5] ) * 1024;\r
+                               }\r
+               }else\r
+               {\r
+                       //We now assume the OS is UNIX\r
+                       //Tested on Mac OS X 10.4.6 and Linux Red Hat Enterprise 4\r
+                       //This should work on most UNIX systems\r
+                       $pid = getmypid();\r
+                       exec("ps -eo%mem,rss,pid | grep $pid", $output);\r
+                       $output = explode("  ", $output[0]);\r
+                       //rss is given in 1024 byte units\r
+                       return $output[1] * 1024;\r
+               }\r
+       }\r
+}\r
+\r
 ?>
\ No newline at end of file