8 define (
'CH_PROFILER_DISPLAY', 1);
10 require_once(CH_DIRECTORY_PATH_MODULES .
'cheetah/profiler/install/config.php');
17 'class_prefix' =>
$aConfig[
'class_prefix'],
18 'db_prefix' =>
$aConfig[
'db_prefix'],
52 'modules/cheetah/profiler/templates/base/css/|profiler.css',
53 'modules/cheetah/profiler/plugins/jush/|jush.css',
57 'plugins/jquery/|jquery.tablesorter.js',
58 'modules/cheetah/profiler/plugins/jush/|jush.js',
61 foreach ($aCss
as $sCssPath) {
62 $this->oTemplate->addCss ($sCssPath);
63 $this->oTemplate->addCssAdmin ($sCssPath);
65 foreach ($aJs
as $sJsPath) {
66 $this->oTemplate->addJs($sJsPath);
67 $this->oTemplate->addJsAdmin($sJsPath);
70 if (
getParam (
'ch_profiler_long_sql_queries_log'))
71 $this->aConf[
'long_query'] =
getParam (
'ch_profiler_long_sql_queries_time');
72 if (
getParam (
'ch_profiler_long_module_query_log'))
73 $this->aConf[
'long_module'] =
getParam (
'ch_profiler_long_module_query_time');
74 if (
getParam (
'ch_profiler_long_page_log'))
75 $this->aConf[
'long_page'] =
getParam (
'ch_profiler_long_page_time');
77 $this->_iTimeStart = $iTimeStart;
86 if (isset($this->aConf[
'long_page']) && $iPageTIme > $this->aConf[
'long_page'])
89 switch (
getParam(
'ch_profiler_show_debug_panel')) {
111 $sDate = date ($this->_sLogDateFormat);
112 return "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" . $sDate .
" " .
$s .
"\n" .
114 "User role: " . (
isAdmin() ?
'admin' : (
isMember() ?
'member' :
'guest')) .
"\n";
124 $f = fopen ( CH_DIRECTORY_PATH_MODULES .
$GLOBALS[
'ch_profiler_module'][
'path'] .
'log/profiler.log',
'a');
137 if (
getParam(
'ch_profiler_long_sql_queries_debug'))
145 $s = $this->
_logBegin (
'LONG MODULE QUERY: ' . $aModuleQuery[
'time']);
146 $s .=
"Module name: " . $aModuleQuery[
'name'] .
"\n";
147 $s .=
"Query type: " . $aModuleQuery[
'type'] .
"\n";
148 $s .=
"Class/file: " . $aModuleQuery[
'class/file'] .
"\n";
149 $s .=
"Method: " . $aModuleQuery[
'method'] .
"\n";
150 if (
getParam(
'ch_profiler_long_module_query_debug'))
159 $s .=
"Request method: " . $_SERVER[
'REQUEST_METHOD'] .
"\n";
160 $s .=
"Query string: " . $_SERVER[
'QUERY_STRING'] .
"\n";
161 $s .=
"Request URI: " . $_SERVER[
'REQUEST_URI'] .
"\n";
162 $s .=
"Script name: " . $_SERVER[
'SCRIPT_NAME'] .
"\n";
163 $s .=
"PHP self: " . $_SERVER[
'PHP_SELF'] .
"\n";
164 if (
getParam(
'ch_profiler_long_page_debug'))
165 $s .=
"All server vars: \n" . print_r ($_SERVER,
true);
173 $this->_aModulesNames[
$aModule[
'title']] = isset($this->_aModulesNames[
$aModule[
'title']]) ? $this->_aModulesNames[
$aModule[
'title']] + 1 : 1;
174 $this->_aModules[$sHash] = array (
175 'name' => str_repeat(
'  ', $this->_aModulesLevel-1) .
$aModule[
'title'],
177 'class/file' => $sClassFile,
178 'method' => $sMethod,
179 'begin' => microtime (),
187 $iTime = $this->
_calcTime ($this->_aModules[$sHash][
'begin']);
188 unset ($this->_aModules[$sHash][
'begin']);
189 $this->_aModules[$sHash][
'time'] = $this->
_formatTime($iTime, 5);
190 $this->_aModules[$sHash][
'raw_time'] = $iTime;
191 if (isset($this->aConf[
'long_module']) && $iTime > $this->aConf[
'long_module'])
197 $this->_sInjectionIndex =
$sId;
198 $this->_aInjections[
$sId][
'begin'] = microtime ();
203 if (!isset($this->_aInjections[
$sId]))
205 $iTime = $this->
_calcTime ($this->_aInjections[
$sId][
'begin']);
206 unset ($this->_aInjections[
$sId][
'begin']);
208 $this->_aInjections[
$sId][
'key'] = $sKey;
209 $this->_aInjections[
$sId][
'replace'] = $isReplace ?
'yes' :
'no';
211 $this->_aInjections[
$sId][
'raw_time'] = $iTime;
216 $this->_sPageBlockIndex = $iBlockId;
217 $this->_aPagesBlocks[$this->_sPageBlockIndex][
'name'] =
$sName;
218 $this->_aPagesBlocks[$this->_sPageBlockIndex][
'begin'] = microtime ();
223 if (!$this->_sPageBlockIndex)
225 $iTime = $this->
_calcTime ($this->_aPagesBlocks[$this->_sPageBlockIndex][
'begin']);
226 unset ($this->_aPagesBlocks[$this->_sPageBlockIndex][
'begin']);
227 $this->_aPagesBlocks[$this->_sPageBlockIndex][
'cached'] = $isCached ?
'yes' :
'no';
228 $this->_aPagesBlocks[$this->_sPageBlockIndex][
'empty'] = $isEmpty ?
'yes' :
'no';
229 $this->_aPagesBlocks[$this->_sPageBlockIndex][
'time'] = $this->
_formatTime($iTime, 5);
230 $this->_aPagesBlocks[$this->_sPageBlockIndex][
'raw_time'] = $iTime;
235 $this->_sPageIndex = md5 (
$sName.time().rand());
236 $this->_aPages[$this->_sPageIndex][
'name'] =
$sName;
237 $this->_aPages[$this->_sPageIndex][
'begin'] = microtime ();
242 if (!$this->_sPageIndex)
244 $iTime = $this->
_calcTime ($this->_aPages[$this->_sPageIndex][
'begin']);
245 unset ($this->_aPages[$this->_sPageIndex][
'begin']);
246 $this->_aPages[$this->_sPageIndex][
'time'] = $this->
_formatTime($iTime, 5);
247 $this->_aPages[$this->_sPageIndex][
'raw_time'] = $iTime;
252 $this->_aTemplateIndexes[
$sName.$sRand] = 1;
254 $this->_aTemplates[
$sName.$sRand][
'begin'] = microtime ();
259 if (!isset($this->_aTemplateIndexes[
$sName.$sRand]))
261 $iTime = $this->
_calcTime ($this->_aTemplates[
$sName.$sRand][
'begin']);
262 unset ($this->_aTemplates[
$sName.$sRand][
'begin']);
263 $this->_aTemplates[
$sName.$sRand][
'cached'] = $isCached ?
'yes' :
'no';
265 $this->_aTemplates[
$sName.$sRand][
'raw_time'] = $iTime;
270 $this->_sQueryIndex = md5 ($sSql.time().rand());
277 if (!$this->_sQueryIndex)
279 $iTime = $this->
_calcTime ($this->_aQueries[$this->_sQueryIndex][
'begin']);
280 unset ($this->_aQueries[$this->_sQueryIndex][
'begin']);
285 if (isset($this->aConf[
'long_query']) && $iTime > $this->aConf[
'long_query'])
286 $this->
logSqlQuery ($iTime, $this->_aQueries[$this->_sQueryIndex], $res);
292 $this->_aMenus[
$sName][
'begin'] = microtime ();
297 if (!isset($this->_aMenus[
$sName]))
300 unset ($this->_aMenus[
$sName][
'begin']);
302 $this->_aMenus[
$sName][
'raw_time'] = $iTime;
307 $i1 = explode(
' ', microtime ());
308 $i2 = explode(
' ', $this->_iTimeStart);
309 return ($i1[0]+$i1[1]) - ($i2[0]+$i2[1]);
315 if (function_exists(
'memory_get_usage'))
316 $sMemory = $this->
_formatBytes(memory_get_usage(
true)) .
' of ' . ini_get(
'memory_limit') .
' allowed';
318 return $this->oTemplate->plank(
319 $this->oTemplate->nameValue(
'Time:',
$sTime) .
320 (function_exists(
'memory_get_usage') ? $this->oTemplate->nameValue(
'Memory:', $sMemory) :
'') .
321 $this->oTemplate->nameValue(
'PHP:', phpversion()) .
322 $this->oTemplate->nameValue(
'SAPI:', php_sapi_name()) .
323 $this->oTemplate->nameValue(
'OS:', php_uname(
's r m'))
335 foreach (
$GLOBALS[
'ch_profiler']->_aPages
as $k => $r) {
336 $iTimePages += $r[
'raw_time'];
337 unset (
$GLOBALS[
'ch_profiler']->_aPages[$k][
'raw_time']);
339 $sPages = count(
$GLOBALS[
'ch_profiler']->_aPages);
342 $sTemplatesCached =
'';
343 $sTemplatesNotCached =
'';
344 if (
$GLOBALS[
'ch_profiler']->_aTemplates) {
345 $iTimeTemplatesCached = 0;
346 $iTimeTemplatesNotCached = 0;
347 $sTemplatesCached = 0;
348 $sTemplatesNotCached = 0;
349 foreach (
$GLOBALS[
'ch_profiler']->_aTemplates
as $k => $r) {
350 if (
'yes' == $r[
'cached']) {
351 $iTimeTemplatesCached += $r[
'raw_time'];
354 $iTimeTemplatesNotCached += $r[
'raw_time'];
355 ++$sTemplatesNotCached;
357 unset (
$GLOBALS[
'ch_profiler']->_aTemplates[$k][
'raw_time']);
359 if ($sTemplatesCached)
360 $sTemplatesCached .=
' (' . $this->
_formatTime($iTimeTemplatesCached, 3) .
')';
361 if ($sTemplatesNotCached)
362 $sTemplatesNotCached .=
' (' . $this->
_formatTime($iTimeTemplatesNotCached, 3) .
')';
365 return $this->oTemplate->plank(
366 ($sPages ? $this->oTemplate->nameValue(
'Pages:', $sPages) :
'') .
367 ($sTemplatesCached ? $this->oTemplate->nameValue(
'Templates Cached:', $sTemplatesCached) :
'') .
368 ($sTemplatesNotCached ? $this->oTemplate->nameValue(
'Templates Not Cached:', $sTemplatesNotCached) :
''),
369 $this->oTemplate->table(
$GLOBALS[
'ch_profiler']->_aTemplates)
375 if (!
$GLOBALS[
'ch_profiler']->_aInjections)
378 $iTimeInjections = 0;
379 foreach (
$GLOBALS[
'ch_profiler']->_aInjections
as $k => $r) {
380 $iTimeInjections += $r[
'raw_time'];
381 unset (
$GLOBALS[
'ch_profiler']->_aInjections[$k][
'raw_time']);
384 $sInjections = count(
$GLOBALS[
'ch_profiler']->_aInjections) .
' injection (' . $this->
_formatTime($iTimeInjections, 3) .
')';
386 return $this->oTemplate->plank(
387 $this->oTemplate->nameValue(
'Injections:', $sInjections),
388 $this->oTemplate->table(
$GLOBALS[
'ch_profiler']->_aInjections)
398 $iTimeBlocksCached = 0;
399 $iTimeBlocksNotCached = 0;
400 $iTimeBlocksEmpty = 0;
401 $iTimeBlocksNotEmpty = 0;
403 $iCountBlocksCached = 0;
404 $iCountBlocksNotCached = 0;
405 $iCountBlocksEmpty = 0;
406 $iCountBlocksNotEmpty = 0;
408 foreach (
$GLOBALS[
'ch_profiler']->_aPagesBlocks
as $k => $r) {
409 $iTimeBlocks += $r[
'raw_time'];
410 if ($r[
'cached'] ==
'yes') {
411 $iTimeBlocksCached += $r[
'raw_time'];
412 ++$iCountBlocksCached;
414 $iTimeBlocksNotCached += $r[
'raw_time'];
415 ++$iCountBlocksNotCached;
417 if ($r[
'empty'] ==
'yes') {
418 $iTimeBlocksEmpty += $r[
'raw_time'];
419 ++$iCountBlocksEmpty;
421 $iTimeBlocksNotEmpty += $r[
'raw_time'];
422 ++$iCountBlocksNotEmpty;
424 unset (
$GLOBALS[
'ch_profiler']->_aPagesBlocks[$k][
'raw_time']);
427 $sBlocks = count(
$GLOBALS[
'ch_profiler']->_aPagesBlocks) .
' (' . $this->
_formatTime($iTimeBlocks, 3) .
')';
428 if ($iCountBlocksCached)
429 $sBlocksCached = $iCountBlocksCached .
' (' . $this->
_formatTime($iTimeBlocksCached, 3) .
')';
430 if ($iCountBlocksNotCached)
431 $sBlocksNotCached = $iCountBlocksNotCached .
' (' . $this->
_formatTime($iTimeBlocksNotCached, 3) .
')';
432 if ($iCountBlocksEmpty)
433 $sBlocksEmpty = $iCountBlocksEmpty .
' (' . $this->
_formatTime($iTimeBlocksEmpty, 3) .
')';
434 if ($iCountBlocksNotEmpty)
435 $sBlocksNotEmpty = $iCountBlocksNotEmpty .
' (' . $this->
_formatTime($iTimeBlocksNotEmpty, 3) .
')';
437 return $this->oTemplate->plank(
438 $this->oTemplate->nameValue(
'Pages Blocks:', $sBlocks) .
439 ($iCountBlocksCached ? $this->oTemplate->nameValue(
'Cached:', $sBlocksCached) :
'') .
440 ($iCountBlocksNotCached ? $this->oTemplate->nameValue(
'Not Cached:', $sBlocksNotCached) :
'') .
441 ($iCountBlocksEmpty ? $this->oTemplate->nameValue(
'Empty:', $sBlocksEmpty) :
'') .
442 ($iCountBlocksNotEmpty ? $this->oTemplate->nameValue(
'Not Empty:', $sBlocksNotEmpty) :
''),
443 $this->oTemplate->table(
$GLOBALS[
'ch_profiler']->_aPagesBlocks)
453 foreach (
$GLOBALS[
'ch_profiler']->_aMenus
as $k => $r) {
454 $iTimeMenus += $r[
'raw_time'];
455 unset (
$GLOBALS[
'ch_profiler']->_aMenus[$k][
'raw_time']);
458 $sMenus = count(
$GLOBALS[
'ch_profiler']->_aMenus) .
' menus (' . $this->
_formatTime($iTimeMenus, 3) .
')';
460 return $this->oTemplate->plank(
461 $this->oTemplate->nameValue(
'Menus:', $sMenus),
462 $this->oTemplate->table(
$GLOBALS[
'ch_profiler']->_aMenus)
472 foreach (
$GLOBALS[
'ch_profiler']->_aQueries
as $k => $r) {
473 $iTimeQueries += $r[
'raw_time'];
474 unset (
$GLOBALS[
'ch_profiler']->_aQueries[$k][
'raw_time']);
477 $sQueries = count(
$GLOBALS[
'ch_profiler']->_aQueries) .
' queries (' . $this->
_formatTime($iTimeQueries, 3) .
')';
479 return $this->oTemplate->plank(
480 $this->oTemplate->nameValue(
'SQL:', $sQueries),
481 $this->oTemplate->table(
$GLOBALS[
'ch_profiler']->_aQueries,
'sql')
491 foreach (
$GLOBALS[
'ch_profiler']->_aModules
as $k => $r) {
492 $iTimeModules += $r[
'raw_time'];
493 unset (
$GLOBALS[
'ch_profiler']->_aModules[$k][
'raw_time']);
496 $sModules = count(
$GLOBALS[
'ch_profiler']->_aModulesNames) .
' modules loaded';
497 $sModulesQueries = count(
$GLOBALS[
'ch_profiler']->_aModules) .
' modules queries (' . $this->
_formatTime($iTimeModules, 3) .
')';
499 return $this->oTemplate->plank(
500 $this->oTemplate->nameValue(
'Modules:', $sModules) .
501 $this->oTemplate->nameValue(
'Modules Queries:', $sModulesQueries),
502 $this->oTemplate->table(
$GLOBALS[
'ch_profiler']->_aModules)
508 return round($i, $iPrecision) .
' sec';
514 return round($i/1024/1024, 1) .
'M';
516 return round($i/1024, 1) .
'K';
523 if (isset(
$GLOBALS[
'ch_profiler_disable']) || isset(
$_GET[
'ch_profiler_disable']))
526 preg_match(
'/gzip_loader\.php/', $_SERVER[
'PHP_SELF']) ||
527 preg_match(
'/get_rss_feed\.php/', $_SERVER[
'PHP_SELF']) ||
528 preg_match(
'/fields\.parse\.php/', $_SERVER[
'PHP_SELF']) ||
529 preg_match(
'/flash/', $_SERVER[
'PHP_SELF']) ||
530 preg_match(
'/forum/', $_SERVER[
'PHP_SELF'])
540 $i1 = explode(
' ', microtime ());
541 $i2 = explode(
' ', $begin);
542 return ($i1[0]+$i1[1]) - ($i2[0]+$i2[1]);
548 foreach ($mixed
as $mixed2)
550 $sArgs = substr($sArgs, 0, -1);
557 return get_class($mixed) .
' instance';
563 return "'" . (strlen($mixed) > $this->_sLogMaxArgLength ? substr($mixed, 0, $this->_sLogMaxArgLength) .
'...' : $mixed) .
"'";
570 $a = debug_backtrace();
571 while (--$iShifts > -1)
578 foreach ($r[
'args']
as $mixed) {
580 case is_array($mixed):
583 case is_object($mixed):
591 $sArgs = substr($sArgs, 0, -1);
593 $s .=
"--------------------------------------\n";
594 $s .=
"{$r['line']} {$r['file']}\n";
595 $s .=
"{$r['class']}{$r['type']}{$r['function']} ({$sArgs});\n";
601 if (defined(
'CH_PROFILER') && CH_PROFILER) {
603 if (!
$GLOBALS[
'ch_profiler']->_isProfilerDisabled())
604 register_shutdown_function (array (
$GLOBALS[
'ch_profiler'],
'output'));