OSDN Git Service

FIX: NP_ThumbnailをNucleus 3.65/PHP5.4/MySQL5.5で動作するよう修正
[nucleus-jp/nucleus-plugins.git] / trunk / NP_Benchmark / NP_Benchmark.php
1 <?php\r
2 /*      \r
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
8         \r
9         NP_Benchmark by yu (http://nucleus.datoka.jp/)\r
10         \r
11         Usage\r
12         -----\r
13         <%Benchmark(stylesheet)%> //output embedded stylesheet for head block\r
14         \r
15         <%Benchmark(foo)%> //benchmark label "foo"\r
16         <%Benchmark(bar)%> //benchmark label "bar"\r
17         \r
18         \r
19         History\r
20         -------\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
26 */\r
27 \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
38         \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
48         }\r
49         \r
50         var $flg_enabled;\r
51         var $flg_comment;\r
52         var $flg_sql;\r
53         var $flg_mem;\r
54         var $flg_diff;\r
55         var $flg_average;\r
56         var $member;\r
57         \r
58         function init() {\r
59                 global $member, $blogid;\r
60                 \r
61                 if (!$blogid) {\r
62                         $this->flg_enabled = false;\r
63                         return;\r
64                 }\r
65                 \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
73                 \r
74                 switch ($this->member) {\r
75                 case 'admin':\r
76                         if (!$member->isAdmin($blogid)) $this->flg_enabled = false;\r
77                         else if($this->flg_average) session_start();\r
78                         break;\r
79                 case 'team':\r
80                         if (!$member->isTeamMember($blogid)) $this->flg_enabled = false;\r
81                         else if($this->flg_average) session_start();\r
82                         break;\r
83                 }\r
84         }\r
85         \r
86         function event_PreSkinParse($data) {\r
87                 if (!$this->flg_enabled) return;\r
88                 \r
89                 global $benchmark_start;\r
90                 if ($this->getOption('AutoStart') == 'yes') $benchmark_start = microtime();\r
91         }\r
92         \r
93         function doAction($type) {\r
94                 global $member;\r
95                 \r
96                 switch ($type) {\r
97                 case 'reset':\r
98                         if (!$member->isLoggedIn()) return;\r
99                         session_start();\r
100                         $_SESSION = array();\r
101                         if (isset($_COOKIE[session_name()])) {\r
102                                 setcookie(session_name(), '', time()-42000, '/');\r
103                         }\r
104                         session_destroy();\r
105                         break;\r
106                 }\r
107                 redirect( serverVar('HTTP_REFERER') );\r
108         }\r
109         \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
114         }\r
115         \r
116         var $lastsec;\r
117         var $bmcnt = 0;\r
118         function benchmark($label=''){\r
119                 global $benchmark_start, $SQLCount, $CONF;\r
120                 \r
121                 if (!$this->flg_enabled) return;\r
122                 if (empty($benchmark_start)) {\r
123                         $benchmark_start = microtime();\r
124                         return;\r
125                 }\r
126                 \r
127                 $this->bmcnt++;\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
135                 \r
136                 if ($this->flg_comment) {\r
137                         $pre = '<!--';\r
138                         $post = '-->';\r
139                 }\r
140                 else {\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
144                         $post = '</span>';\r
145                 }\r
146                 \r
147                 echo $pre;\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
152                 \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&amp;name=Benchmark&amp;type=reset" title="session reset">x</a>]';\r
161                         }\r
162                 }\r
163                 echo $post;\r
164                 $this->lastsec = $end;\r
165         }\r
166         \r
167         function embedStylesheet() {\r
168                 echo <<< EOH\r
169 <style type="text/css">\r
170 <!--\r
171 span.benchmark {\r
172         border: 1px solid black;\r
173         background-color: #000000;\r
174         padding: 1px;\r
175         color: white;\r
176         font-size: 9px;\r
177         line-height: 1em;\r
178 }\r
179 span.benchmark a {\r
180         color: #ccc;\r
181 }\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
193 -->\r
194 </style>\r
195 EOH;\r
196         }\r
197 \r
198 }\r
199 \r
200 if( !function_exists('memory_get_usage') )\r
201 {\r
202         function memory_get_usage()\r
203         {\r
204                 //If its Windows\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
209                 {\r
210                                 if ( substr( PHP_OS, 0, 3 ) == 'WIN' )\r
211                                 {\r
212                                         $output = array();\r
213                                         exec( 'tasklist /FI "PID eq ' . getmypid() . '" /FO LIST', $output );\r
214                                         return preg_replace( '/[\D]/', '', $output[5] ) * 1024;\r
215                                 }\r
216                 }else\r
217                 {\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
221                         $pid = getmypid();\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
226                 }\r
227         }\r
228 }\r
229 \r
230 ?>