86
86
** Eugene Grigorjev (eugene.grigorjev@zabbix.com)
89
89
// define("USE_PROFILING",1);
90
// define("USE_TIME_PROF",1);
91
// define("USE_MEM_PROF",1);
92
// define("USE_SQLREQUEST_PROF",1);
93
// define("SHOW_SQLREQUEST_DETAILS",1);
90
define("USE_VAR_MON",1);
91
define("USE_TIME_PROF",1);
92
define("USE_MEM_PROF",1);
93
define("USE_COUNTER_PROF",1);
94
define("USE_MENU_PROF",1);
95
//define("USE_MENU_DETAILS",1);
96
define("USE_SQLREQUEST_PROF",1);
97
//define("SHOW_SQLREQUEST_DETAILS",1);
99
if(!defined("OBR")) define("OBR","<br/>\n");
95
101
if(defined('USE_PROFILING'))
97
103
$starttime=array();
98
104
$memorystamp=array();
105
$sqlrequests = defined('SHOW_SQLREQUEST_DETAILS') ? array() : 0;
100
106
$sqlmark = array();
107
$perf_counter = array();
124
/* public static */ function showmemoryusage($descr=null)
126
if(defined('USE_MEM_PROF')) {
127
$memory_usage = COpt::getmemoryusage();
128
$memory_usage = $memory_usage.'b | '.($memory_usage>>10).'K | '.($memory_usage>>20).'M';
129
SDI('PHP memory usage ['.$descr.'] '.$memory_usage);
116
133
/* protected static */ function getmemoryusage() {
117
134
if(defined('USE_MEM_PROF')) {
118
135
return memory_get_usage('memory_limit');
124
/* protected static */ function mem2str($size)
141
/* public static */ function counter_up($type=NULL)
127
if($size > 1048576) { $size = $size/1048576; $prefix = 'M'; }
128
elseif($size > 1024) { $size = $size/1024; $prefix = 'K'; }
129
return round($size, 6).$prefix;
143
if(defined('USE_COUNTER_PROF'))
145
global $perf_counter;
148
foreach(array_keys($starttime) as $keys)
150
if(!isset($perf_counter[$keys][$type]))
151
$perf_counter[$keys][$type]=1;
153
$perf_counter[$keys][$type]++;
132
158
/* public static */ function profiling_start($type=NULL)
134
160
global $starttime;
135
161
global $memorystamp;
137
163
global $sqlrequests;
139
166
if(is_null($type)) $type='global';
141
168
$starttime[$type] = COpt::getmicrotime();
142
169
$memorystamp[$type] = COpt::getmemoryusage();
170
if(defined('USE_VAR_MON'))
173
$var_list[$type] = isset($GLOBALS) ? array_keys($GLOBALS) : array();
143
175
if(defined('USE_SQLREQUEST_PROF'))
177
if(defined('SHOW_SQLREQUEST_DETAILS')){
145
178
$sqlmark[$type] = count($sqlrequests);
181
$sqlmark[$type] = $sqlrequests;
161
202
global $memorystamp;
162
203
global $sqlrequests;
205
global $perf_counter;
165
208
$endtime = COpt::getmicrotime();
166
209
$memory = COpt::getmemoryusage();
168
211
if(is_null($type)) $type='global';
171
214
if(defined('USE_TIME_PROF'))
173
echo "(".$type.") Time to execute: ".round($endtime - $starttime[$type],6)." seconds!\n<br>\n";
216
echo "(".$type.") Time to execute: ".round($endtime - $starttime[$type],6)." seconds!".OBR;
175
218
if(defined('USE_MEM_PROF'))
177
echo "(".$type.") Memory limit : ".ini_get('memory_limit')."<br>\n";
178
echo "(".$type.") Memory usage : ".COpt::mem2str($memorystamp[$type])." - ".COpt::mem2str($memory)."\n<br>\n";
179
echo "(".$type.") Memory leak : ".COpt::mem2str($memory - $memorystamp[$type])."\n<br>\n";
220
echo "(".$type.") Memory limit : ".ini_get('memory_limit').OBR;
221
echo "(".$type.") Memory usage : ".mem2str($memorystamp[$type])." - ".mem2str($memory).OBR;
222
echo "(".$type.") Memory leak : ".mem2str($memory - $memorystamp[$type]).OBR;
224
if(defined('USE_VAR_MON'))
226
$curr_var_list = isset($GLOBALS) ? array_keys($GLOBALS) : array();
227
$var_diff = array_diff($curr_var_list, $var_list[$type]);
228
echo "(".$type.") Undeleted vars : ".count($var_diff)." [";
229
print_r(implode(', ',$var_diff));
232
if(defined('USE_COUNTER_PROF'))
234
if(isset($perf_counter[$type]))
236
ksort($perf_counter[$type]);
237
foreach($perf_counter[$type] as $name => $value)
239
echo "(".$type.") Counter '".$name."' : ".$value.OBR;
181
243
if(defined('USE_SQLREQUEST_PROF'))
245
if(defined('SHOW_SQLREQUEST_DETAILS'))
183
247
$requests_cnt = count($sqlrequests);
184
echo "(".$type.") SQL requests count: ".($requests_cnt - $sqlmark[$type])."<br>\n";
185
if(defined('SHOW_SQLREQUEST_DETAILS'))
248
echo "(".$type.") SQL requests count: ".($requests_cnt - $sqlmark[$type]).OBR;
187
250
for($i = $sqlmark[$type]; $i < $requests_cnt; $i++)
189
echo "(".$type.") SQL request : ".$sqlrequests[$i]."<br>\n";;
252
echo "(".$type.") SQL request : ".$sqlrequests[$i].OBR;
257
echo "(".$type.") SQL requests count: ".($sqlrequests - $sqlmark[$type]).OBR;
198
265
ini_set('memory_limit',$limit);
268
/* public static */ function compare_files_with_menu($menu=null)
270
if(defined('USE_MENU_PROF'))
272
$files_list = glob('*.php');
275
foreach($files_list as $file)
278
foreach($menu as $label=>$sub)
280
foreach($sub['pages'] as $sub_pages)
282
if(!isset($sub_pages["label"])) $sub_pages["label"]=$sub_pages['url'];
284
$menu_path = $sub["label"].'->'.$sub_pages["label"];
286
if($sub_pages['url'] == $file)
288
array_push($list, $menu_path);
290
if(!in_array($sub_pages['url'], $files_list))
291
$result['error'][$sub_pages['url']] = array($menu_path);
293
if(isset($sub_pages['sub_pages'])) foreach($sub_pages['sub_pages'] as $page)
295
$menu_path = $sub["label"].'->'.$sub_pages["label"].'->sub_pages';
297
if(!in_array($page, $files_list))
298
$result['error'][$page] = array($menu_path);
300
if($page != $file) continue;
301
array_push($list, $menu_path);
305
if(count($list) != 1) $level = 'worning';
306
else $level = 'normal';
308
$result[$level][$file] = $list;
310
foreach($result as $level => $files_list)
312
if(defined('USE_MENU_DETAILS'))
314
echo OBR.'(menu check) ['.$level.OBR;
315
foreach($files_list as $file => $menu_list)
317
echo "(menu check)".SPACE.SPACE.SPACE.SPACE.$file.' {'.implode(',',$menu_list)."}".OBR;
322
echo OBR.'(menu check) ['.$level."] = ".count($files_list).OBR;
202
329
COpt::set_memory_limit('8M');
335
if(version_compare(phpversion(),'5.0','>='))
209
/* public static */ function profiling_start($type=NULL) {}
210
/* public static */ function profiling_stop($type=NULL) {}
211
/* public static */ function savesqlrequest($sql) {}
341
'.$static.' function profiling_start($type=NULL) {}
342
'.$static.' function profiling_stop($type=NULL) {}
343
'.$static.' function savesqlrequest($sql) {}
344
'.$static.' function showmemoryusage($descr=null) {}
345
'.$static.' function compare_files_with_menu($menu=null) {}
346
'.$static.' function counter_up($type=NULL) {}