3 This program is free software; you can redistribute it and/or
\r
4 modify it under the terms of the GNU General Public License
\r
5 as published by the Free Software Foundation; either version 2
\r
6 of the License, or (at your option) any later version.
\r
7 (see nucleus/documentation/index.html#license for more info)
\r
9 NP_Benchmark by yu (http://nucleus.datoka.jp/)
\r
13 <%Benchmark(stylesheet)%> //output embedded stylesheet for head block
\r
15 <%Benchmark(foo)%> //benchmark label "foo"
\r
16 <%Benchmark(bar)%> //benchmark label "bar"
\r
21 Ver0.4 2008/12/12 Add more options for output. Add embedding styles. (yu)
\r
22 Ver0.31 2008/06/18 Add labels to calc diff time. Add CSS "level" class. (yu)
\r
23 Ver0.3 2007/07/05 Add average time (use session) and memory usage. Memory usage is disabled in default. (yu)
\r
24 Ver0.2 2006/05/01 Improved and add some features. (sato(na))
\r
25 Ver0.1 2004/03/08 Initial release. (yu)
\r
28 class NP_Benchmark extends NucleusPlugin {
\r
29 function getName() { return 'Benchmark'; }
\r
30 function getAuthor() { return 'yu + sato(na)'; }
\r
31 function getURL() { return 'http://works.datoka.jp/plugins/224.html'; }
\r
32 function getVersion() { return '0.4'; }
\r
33 function getMinNucleusVersion(){ return 330; }
\r
34 function getTableList() { return array(); }
\r
35 function supportsFeature($w) { return ($w == 'SqlTablePrefix') ? 1 : 0; }
\r
36 function getEventList() { return array( 'PreSkinParse' ); }
\r
37 function getDescription() { return 'Benchmark'; }
\r
39 function install() {
\r
40 $this->createOption("EnableThis", "Enable benchmarking", "yesno", "yes");
\r
41 $this->createOption("EnabledFor", "Benchmark is enabled for:", "select", "admin", "Admin|admin|Team Member|team|All|all");
\r
42 $this->createOption("AutoStart", "Start benchmark automatically on PreSkinParse", "yesno", "yes");
\r
43 $this->createOption("UseComment", "Use comment tag for output", "yesno", "no");
\r
44 $this->createOption("ShowSQL", "Show SQL count", "yesno", "yes");
\r
45 $this->createOption("ShowMem", "Show memory usage", "yesno", "no");
\r
46 $this->createOption("ShowDiff", "Show diffs of several benchmark points", "yesno", "no");
\r
47 $this->createOption("ShowAverage", "Show average of diffs", "yesno", "no");
\r
59 global $member, $blogid;
\r
62 $this->flg_enabled = false;
\r
66 $this->member = $this->getOption('EnabledFor');
\r
67 $this->flg_enabled = ($this->getOption('EnableThis') == 'yes');
\r
68 $this->flg_comment = ($this->getOption('UseComment') == 'yes');
\r
69 $this->flg_sql = ($this->getOption('ShowSQL') == 'yes');
\r
70 $this->flg_mem = ($this->getOption('ShowMem') == 'yes');
\r
71 $this->flg_diff = ($this->getOption('ShowDiff') == 'yes');
\r
72 $this->flg_average = ($this->getOption('ShowAverage') == 'yes' and $this->member != 'all');
\r
74 switch ($this->member) {
\r
76 if (!$member->isAdmin($blogid)) $this->flg_enabled = false;
\r
77 else if($this->flg_average) session_start();
\r
80 if (!$member->isTeamMember($blogid)) $this->flg_enabled = false;
\r
81 else if($this->flg_average) session_start();
\r
86 function event_PreSkinParse($data) {
\r
87 if (!$this->flg_enabled) return;
\r
89 global $benchmark_start;
\r
90 if ($this->getOption('AutoStart') == 'yes') $benchmark_start = microtime();
\r
93 function doAction($type) {
\r
98 if (!$member->isLoggedIn()) return;
\r
100 $_SESSION = array();
\r
101 if (isset($_COOKIE[session_name()])) {
\r
102 setcookie(session_name(), '', time()-42000, '/');
\r
107 redirect( serverVar('HTTP_REFERER') );
\r
110 function doTemplateVar(&$item, $label=''){ $this->benchmark($label); }
\r
111 function doSkinVar($skinType, $label='') {
\r
112 if ($label == 'stylesheet') $this->embedStylesheet();
\r
113 else $this->benchmark($label);
\r
118 function benchmark($label=''){
\r
119 global $benchmark_start, $SQLCount, $CONF;
\r
121 if (!$this->flg_enabled) return;
\r
122 if (empty($benchmark_start)) {
\r
123 $benchmark_start = microtime();
\r
128 list($msec1, $sec1) = explode(' ', $benchmark_start);
\r
129 list($msec2, $sec2) = explode(' ', microtime());
\r
130 $start = (float) $sec1 + (float) $msec1;
\r
131 $end = (float) $sec2 + (float) $msec2;
\r
132 $diff = $end - $start;
\r
133 if (!$this->lastsec) $this->lastsec = $start;
\r
134 $diff2 = $end - $this->lastsec;
\r
136 if ($this->flg_comment) {
\r
141 $lv = (int)($diff2 * 10); //unit:0.1sec.
\r
142 $lv = ($lv > 10) ? 10 : $lv;
\r
143 $pre = '<span class="benchmark level'. $lv .'" title="['. $this->bmcnt.']'.$label .'">';
\r
148 if ($this->flg_sql) echo (int)$SQLCount.'q. ';
\r
149 if ($this->flg_mem) echo number_format(memory_get_usage()).'b. '; // returns apache's process size on xampp/win ...
\r
150 echo number_format($diff, 2).'s. ';
\r
151 if ($this->flg_diff) echo '+'. number_format($diff2, 2) .'s. ';
\r
153 if ($this->flg_average) {
\r
154 if ( empty($label) ) $label = 'default';
\r
155 $_SESSION[ $label ][] = $diff2;
\r
156 $cnt = count($_SESSION[ $label ]);
\r
157 $avr_sec = number_format(array_sum($_SESSION[ $label ]) / $cnt, 2);
\r
158 echo '+'.$avr_sec.'s./avr.('.$cnt.') ';
\r
159 if ($this->bmcnt == 1) {
\r
160 echo '[<a href="'. $CONF['ActionURL'] .'?action=plugin&name=Benchmark&type=reset" title="session reset">x</a>]';
\r
164 $this->lastsec = $end;
\r
167 function embedStylesheet() {
\r
169 <style type="text/css">
\r
172 border: 1px solid black;
\r
173 background-color: #000000;
\r
182 span.benchmark.level0 { background-color: #113333; }
\r
183 span.benchmark.level1 { background-color: #336666; }
\r
184 span.benchmark.level2 { background-color: #666633; }
\r
185 span.benchmark.level3 { background-color: #aa9911; }
\r
186 span.benchmark.level4 { background-color: #ddaa00; }
\r
187 span.benchmark.level5 { background-color: #ffcc00; }
\r
188 span.benchmark.level6 { background-color: #ff9900; }
\r
189 span.benchmark.level7 { background-color: #ff6600; }
\r
190 span.benchmark.level8 { background-color: #ff3311; }
\r
191 span.benchmark.level9 { background-color: #ff0033; }
\r
192 span.benchmark.level10 { background-color: #ff0066; }
\r
200 if( !function_exists('memory_get_usage') )
\r
202 function memory_get_usage()
\r
205 //Tested on Win XP Pro SP2. Should work on Win 2003 Server too
\r
206 //Doesn't work for 2000
\r
207 //If you need it to work for 2000 look at http://us2.php.net/manual/en/function.memory-get-usage.php#54642
\r
208 if ( substr(PHP_OS,0,3) == 'WIN')
\r
210 if ( substr( PHP_OS, 0, 3 ) == 'WIN' )
\r
213 exec( 'tasklist /FI "PID eq ' . getmypid() . '" /FO LIST', $output );
\r
214 return preg_replace( '/[\D]/', '', $output[5] ) * 1024;
\r
218 //We now assume the OS is UNIX
\r
219 //Tested on Mac OS X 10.4.6 and Linux Red Hat Enterprise 4
\r
220 //This should work on most UNIX systems
\r
222 exec("ps -eo%mem,rss,pid | grep $pid", $output);
\r
223 $output = explode(" ", $output[0]);
\r
224 //rss is given in 1024 byte units
\r
225 return $output[1] * 1024;
\r