1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
<?php
/*
** ZABBIX
** Copyright (C) 2000-2005 SIA Zabbix
**
** 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.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**/
/* SPEED Measurement
-= slow =-
vs
-= fast =-
1)strlen
if (strlen($foo) < 5) { echo "Foo is too short"; }
vs
if (!isset($foo{5})) { echo "Foo is too short"; }
2)++
$i++
vs
++$i
3)print
print
vs
echo
4)regexps
preg_match("![0-9]+!", $foo);
vs
ctype_digit($foo);
5)in_array
$keys = array("apples", "oranges", "mangoes", "tomatoes", "pickles");
if (in_array('mangoes', $keys)) { ... }
vs
$keys = array("apples" => 1, "oranges" => 1, "mangoes" => 1, "tomatoes" => 1, "pickles" => 1);
if (isset($keys['mangoes'])) { ... }
6)key_exist
if(array_key_exists('mangoes', $keys))
vs
if (isset($keys['mangoes'])) { ... }
7)regexps
POSIX-regexps
vs
Perl-regexps
8)constants
UPPER case constans (TRUE, FALSE)
vs
lower case constans (true, false)
9)for
for ($i = 0; $i < FUNCTION($j); $i++) {...}
vs
for ($i = 0, $k = FUNCTION($j); $i < $k; $i = $i + 1) {...}
10)strings
"var=$var"
vs
'var='.$var
*/
/*
** Description:
** Optimization class. Provide functions for
** PHP code optimization.
**
** Author:
** Eugene Grigorjev (eugene.grigorjev@zabbix.com)
**/
// define("USE_PROFILING",1);
define("USE_VAR_MON",1);
define("USE_TIME_PROF",1);
define("USE_MEM_PROF",1);
define("USE_COUNTER_PROF",1);
define("USE_MENU_PROF",1);
//define("USE_MENU_DETAILS",1);
define("USE_SQLREQUEST_PROF",1);
//define("SHOW_SQLREQUEST_DETAILS",1);
if(!defined("OBR")) define("OBR","<br/>\n");
if(defined('USE_PROFILING'))
{
$starttime=array();
$memorystamp=array();
$sqlrequests = defined('SHOW_SQLREQUEST_DETAILS') ? array() : 0;
$sqlmark = array();
$perf_counter = array();
$var_list = array();
class COpt
{
/* protected static $starttime[]=array(); */
/* protected static */ function getmicrotime() {
if(defined('USE_TIME_PROF')) {
list($usec, $sec) = explode(' ',microtime()); return ((float)$usec + (float)$sec);
}else {
return 0;
}
}
/* public static */ function showmemoryusage($descr=null)
{
if(defined('USE_MEM_PROF')) {
$memory_usage = COpt::getmemoryusage();
$memory_usage = $memory_usage.'b | '.($memory_usage>>10).'K | '.($memory_usage>>20).'M';
SDI('PHP memory usage ['.$descr.'] '.$memory_usage);
}
}
/* protected static */ function getmemoryusage() {
if(defined('USE_MEM_PROF')) {
return memory_get_usage('memory_limit');
} else {
return 0;
}
}
/* public static */ function counter_up($type=NULL)
{
if(defined('USE_COUNTER_PROF'))
{
global $perf_counter;
global $starttime;
foreach(array_keys($starttime) as $keys)
{
if(!isset($perf_counter[$keys][$type]))
$perf_counter[$keys][$type]=1;
else
$perf_counter[$keys][$type]++;
}
}
}
/* public static */ function profiling_start($type=NULL)
{
global $starttime;
global $memorystamp;
global $sqlmark;
global $sqlrequests;
global $var_list;
if(is_null($type)) $type='global';
$starttime[$type] = COpt::getmicrotime();
$memorystamp[$type] = COpt::getmemoryusage();
if(defined('USE_VAR_MON'))
{
$var_list[$type] = isset($GLOBALS) ? array_keys($GLOBALS) : array();
}
if(defined('USE_SQLREQUEST_PROF'))
{
if(defined('SHOW_SQLREQUEST_DETAILS')){
$sqlmark[$type] = count($sqlrequests);
}
else {
$sqlmark[$type] = $sqlrequests;
}
}
}
/* public static */ function savesqlrequest($sql)
{
if(defined('USE_SQLREQUEST_PROF'))
{
global $sqlrequests;
if(defined('SHOW_SQLREQUEST_DETAILS')){
array_push($sqlrequests, $sql);
}else{
$sqlrequests++;
}
}
}
/* public static */ function profiling_stop($type=NULL)
{
global $starttime;
global $memorystamp;
global $sqlrequests;
global $sqlmark;
global $perf_counter;
global $var_list;
$endtime = COpt::getmicrotime();
$memory = COpt::getmemoryusage();
if(is_null($type)) $type='global';
echo OBR;
if(defined('USE_TIME_PROF'))
{
echo "(".$type.") Time to execute: ".round($endtime - $starttime[$type],6)." seconds!".OBR;
}
if(defined('USE_MEM_PROF'))
{
echo "(".$type.") Memory limit : ".ini_get('memory_limit').OBR;
echo "(".$type.") Memory usage : ".mem2str($memorystamp[$type])." - ".mem2str($memory).OBR;
echo "(".$type.") Memory leak : ".mem2str($memory - $memorystamp[$type]).OBR;
}
if(defined('USE_VAR_MON'))
{
$curr_var_list = isset($GLOBALS) ? array_keys($GLOBALS) : array();
$var_diff = array_diff($curr_var_list, $var_list[$type]);
echo "(".$type.") Undeleted vars : ".count($var_diff)." [";
print_r(implode(', ',$var_diff));
echo "]".OBR;
}
if(defined('USE_COUNTER_PROF'))
{
if(isset($perf_counter[$type]))
{
ksort($perf_counter[$type]);
foreach($perf_counter[$type] as $name => $value)
{
echo "(".$type.") Counter '".$name."' : ".$value.OBR;
}
}
}
if(defined('USE_SQLREQUEST_PROF'))
{
if(defined('SHOW_SQLREQUEST_DETAILS'))
{
$requests_cnt = count($sqlrequests);
echo "(".$type.") SQL requests count: ".($requests_cnt - $sqlmark[$type]).OBR;
for($i = $sqlmark[$type]; $i < $requests_cnt; $i++)
{
echo "(".$type.") SQL request : ".$sqlrequests[$i].OBR;
}
}
else
{
echo "(".$type.") SQL requests count: ".($sqlrequests - $sqlmark[$type]).OBR;
}
}
}
/* public static */ function set_memory_limit($limit='8M')
{
ini_set('memory_limit',$limit);
}
/* public static */ function compare_files_with_menu($menu=null)
{
if(defined('USE_MENU_PROF'))
{
$files_list = glob('*.php');
$result = array();
foreach($files_list as $file)
{
$list = array();
foreach($menu as $label=>$sub)
{
foreach($sub['pages'] as $sub_pages)
{
if(!isset($sub_pages["label"])) $sub_pages["label"]=$sub_pages['url'];
$menu_path = $sub["label"].'->'.$sub_pages["label"];
if($sub_pages['url'] == $file)
{
array_push($list, $menu_path);
}
if(!in_array($sub_pages['url'], $files_list))
$result['error'][$sub_pages['url']] = array($menu_path);
if(isset($sub_pages['sub_pages'])) foreach($sub_pages['sub_pages'] as $page)
{
$menu_path = $sub["label"].'->'.$sub_pages["label"].'->sub_pages';
if(!in_array($page, $files_list))
$result['error'][$page] = array($menu_path);
if($page != $file) continue;
array_push($list, $menu_path);
}
}
}
if(count($list) != 1) $level = 'worning';
else $level = 'normal';
$result[$level][$file] = $list;
}
foreach($result as $level => $files_list)
{
if(defined('USE_MENU_DETAILS'))
{
echo OBR.'(menu check) ['.$level.OBR;
foreach($files_list as $file => $menu_list)
{
echo "(menu check)".SPACE.SPACE.SPACE.SPACE.$file.' {'.implode(',',$menu_list)."}".OBR;
}
}
else
{
echo OBR.'(menu check) ['.$level."] = ".count($files_list).OBR;
}
}
}
}
}
COpt::set_memory_limit('8M');
COpt::profiling_start("script");
}
else
{
if(version_compare(phpversion(),'5.0','<'))
{
class COpt
{
/* public static */ function profiling_start($type=NULL) {}
/* public static */ function profiling_stop($type=NULL) {}
/* public static */ function savesqlrequest($sql) {}
/* public static */ function showmemoryusage($descr=null) {}
/* public static */ function compare_files_with_menu($menu=null) {}
/* public static */ function counter_up($type=NULL) {}
}
}
else
{
class COpt
{
static function profiling_start($type=NULL) {}
static function profiling_stop($type=NULL) {}
static function savesqlrequest($sql) {}
static function showmemoryusage($descr=null) {}
static function compare_files_with_menu($menu=null) {}
static function counter_up($type=NULL) {}
}
}
}
?>
|