BxProfiler.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup Profiler Profiler
  7. * @ingroup TridentModules
  8. *
  9. * @{
  10. */
  11. define ('BX_PROFILER', true);
  12. define ('BX_PROFILER_DISPLAY', 1);
  13. require_once(BX_DIRECTORY_PATH_MODULES . 'boonex/profiler/install/config.php');
  14. $GLOBALS['bx_profiler_module'] = array (
  15. 'title' => $aConfig['title'],
  16. 'vendor' => $aConfig['vendor'],
  17. 'path' => $aConfig['home_dir'],
  18. 'uri' => $aConfig['home_uri'],
  19. 'class_prefix' => $aConfig['class_prefix'],
  20. 'db_prefix' => $aConfig['db_prefix'],
  21. );
  22. bx_import('Template', $GLOBALS['bx_profiler_module']);
  23. bx_import('Config', $GLOBALS['bx_profiler_module']);
  24. class BxProfiler extends BxDol
  25. {
  26. protected $oConfig, $oTemplate;
  27. protected $aConf = array ();
  28. protected $_iTimeStart = 0;
  29. protected $_aQueries = array();
  30. protected $_sQueryIndex = 0;
  31. protected $_aModules = array();
  32. protected $_aModulesNames = array();
  33. protected $_aModulesLevel = 0;
  34. protected $_aAlerts = array();
  35. protected $_iAlertsLevel = 0;
  36. protected $_aInjections = array();
  37. protected $_sInjectionIndex = 0;
  38. protected $_sLogDateFormat = "Y-m-d H:i:s";
  39. protected $_sLogMaxArgLength = 64;
  40. protected $_sLogFilename = 64;
  41. function __construct($iTimeStart)
  42. {
  43. parent::__construct();
  44. $this->oConfig = new BxProfilerConfig ($GLOBALS['bx_profiler_module']);
  45. $this->oTemplate = new BxProfilerTemplate ($this->oConfig);
  46. $this->oTemplateAdmin = BxDolStudioTemplate::getInstance();
  47. if (getParam ('bx_profiler_long_sql_queries_log'))
  48. $this->aConf['long_query'] = getParam ('bx_profiler_long_sql_queries_time');
  49. if (getParam ('bx_profiler_long_module_query_log'))
  50. $this->aConf['long_module'] = getParam ('bx_profiler_long_module_query_time');
  51. if (getParam ('bx_profiler_long_page_log'))
  52. $this->aConf['long_page'] = getParam ('bx_profiler_long_page_time');
  53. $this->_iTimeStart = $iTimeStart;
  54. }
  55. // output profiler debug panel
  56. function output ()
  57. {
  58. $iPageTIme = $this->_getCurrentDelay ();
  59. if (isset($this->aConf['long_page']) && $iPageTIme > $this->aConf['long_page'])
  60. $this->logPageOpen ($iPageTIme);
  61. switch (getParam('bx_profiler_show_debug_panel')) {
  62. case 'all':
  63. break;
  64. case 'admins':
  65. if (!$GLOBALS['logged']['admin'])
  66. return;
  67. break;
  68. case 'none':
  69. default:
  70. return;
  71. }
  72. $aCss = array (
  73. 'profiler.css',
  74. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'jush/|jush.css',
  75. );
  76. $aJs = array (
  77. 'jquery.tablesorter.min.js',
  78. 'profiler.js',
  79. 'jush/jush.js',
  80. );
  81. foreach ($aCss as $sCssPath) {
  82. $this->oTemplate->addCss($sCssPath);
  83. $this->oTemplateAdmin->addCss($sCssPath);
  84. }
  85. foreach ($aJs as $sJsPath) {
  86. $this->oTemplate->addJs($sJsPath);
  87. $this->oTemplateAdmin->addJs($sJsPath);
  88. }
  89. $sContentType = $this->_getHeaderContentType();
  90. if ('text/html' == $sContentType) {
  91. echo $this->_plankMain ();
  92. echo $this->_plankMenus ();
  93. echo $this->_plankTemplates ();
  94. echo $this->_plankInjections ();
  95. echo $this->_plankPagesBlocks ();
  96. echo $this->_plankSql ();
  97. echo $this->_plankModules ();
  98. echo $this->_plankAlerts ();
  99. }
  100. }
  101. function _logBegin ($s)
  102. {
  103. $sDate = date ($this->_sLogDateFormat);
  104. return "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" . $sDate . " " . $s . "\n" .
  105. (isset($_COOKIE['memberID']) ? "User ID: " . $_COOKIE['memberID'] . "\n" : '') .
  106. "User role: " . ($GLOBALS['logged']['admin'] ? 'admin' : ($GLOBALS['logged']['member'] ? 'member' : 'guest')) . "\n";
  107. }
  108. function _logEnd ()
  109. {
  110. return "\n";
  111. }
  112. function _appendToLog ($s)
  113. {
  114. $f = fopen ( BX_DIRECTORY_PATH_ROOT . 'logs/bx_profiler.log', 'a');
  115. if (!$f)
  116. return;
  117. fwrite($f, $s);
  118. fclose($f);
  119. }
  120. function logSqlQuery ($iTime, $aSqlQuery, &$res)
  121. {
  122. $s = $this->_logBegin ('LONG SQL QUERY: ' . $aSqlQuery['time']);
  123. $s .= "Rows: " . $aSqlQuery['rows'] . "\n";
  124. $s .= "Affected: " . $aSqlQuery['affected'] . "\n";
  125. $s .= "Query: " . $aSqlQuery['sql'] . "\n";
  126. if (getParam('bx_profiler_long_sql_queries_debug'))
  127. $s .= "Backtrace: \n" . $this->_debugBackTrace (3);
  128. $s .= $this->_logEnd();
  129. $this->_appendToLog ($s);
  130. }
  131. function logModuleQuery ($iTime, $aModuleQuery)
  132. {
  133. $s = $this->_logBegin ('LONG MODULE QUERY: ' . $aModuleQuery['time']);
  134. $s .= "Module name: " . $aModuleQuery['name'] . "\n";
  135. $s .= "Query type: " . $aModuleQuery['type'] . "\n";
  136. $s .= "Class/file: " . $aModuleQuery['class/file'] . "\n";
  137. $s .= "Method: " . $aModuleQuery['method'] . "\n";
  138. if (getParam('bx_profiler_long_module_query_debug'))
  139. $s .= "Backtrace: \n" . $this->_debugBackTrace (3);
  140. $s .= $this->_logEnd();
  141. $this->_appendToLog ($s);
  142. }
  143. function logPageOpen ($iTime)
  144. {
  145. $s = $this->_logBegin ('LONG PAGE OPEN: ' . $this->_formatTime($iTime, 5));
  146. $s .= "Request method: " . $_SERVER['REQUEST_METHOD'] . "\n";
  147. $s .= "Query string: " . $_SERVER['QUERY_STRING'] . "\n";
  148. $s .= "Request URI: " . $_SERVER['REQUEST_URI'] . "\n";
  149. $s .= "Script name: " . $_SERVER['SCRIPT_NAME'] . "\n";
  150. $s .= "PHP self: " . $_SERVER['PHP_SELF'] . "\n";
  151. if (getParam('bx_profiler_long_page_debug'))
  152. $s .= "All server vars: \n" . print_r ($_SERVER, true);
  153. $s .= $this->_logEnd();
  154. $this->_appendToLog ($s);
  155. }
  156. function beginModule($sType, $sHash, &$aModule, $sClassFile, $sMethod = '' )
  157. {
  158. ++$this->_aModulesLevel;
  159. $this->_aModulesNames[$aModule['title']] = isset($this->_aModulesNames[$aModule['title']]) ? $this->_aModulesNames[$aModule['title']] + 1 : 1;
  160. $this->_aModules[$sHash] = array (
  161. 'name' => str_repeat("--", $this->_aModulesLevel-1) . $aModule['title'],
  162. 'type' => $sType,
  163. 'class/file' => $sClassFile,
  164. 'method' => $sMethod,
  165. 'begin' => microtime (),
  166. 'time' => -1,
  167. );
  168. }
  169. function endModule($sType, $sHash)
  170. {
  171. --$this->_aModulesLevel;
  172. $iTime = $this->_calcTime ($this->_aModules[$sHash]['begin']);
  173. unset ($this->_aModules[$sHash]['begin']);
  174. $this->_aModules[$sHash]['time'] = $this->_formatTime($iTime, 5);
  175. $this->_aModules[$sHash]['raw_time'] = $iTime;
  176. if (isset($this->aConf['long_module']) && $iTime > $this->aConf['long_module'])
  177. $this->logModuleQuery ($iTime, $this->_aModules[$sHash]);
  178. }
  179. function beginAlert($sUnit, $sAction, $sHandler)
  180. {
  181. $sHash = md5($sUnit . $sAction . $sHandler);
  182. ++$this->_iAlertsLevel;
  183. $this->_aAlerts[$sHash] = array (
  184. 'name' => str_repeat("--", $this->_iAlertsLevel-1) . $sHandler,
  185. 'unit' => $sUnit,
  186. 'action' => $sAction,
  187. 'handler' => $sHandler,
  188. 'begin' => microtime (),
  189. 'time' => -1,
  190. );
  191. }
  192. function endAlert($sUnit, $sAction, $sHandler)
  193. {
  194. $sHash = md5($sUnit . $sAction . $sHandler);
  195. --$this->_iAlertsLevel;
  196. $iTime = $this->_calcTime ($this->_aAlerts[$sHash]['begin']);
  197. unset ($this->_aAlerts[$sHash]['begin']);
  198. $this->_aAlerts[$sHash]['time'] = $this->_formatTime($iTime, 5);
  199. $this->_aAlerts[$sHash]['raw_time'] = $iTime;
  200. }
  201. function beginInjection ($sId)
  202. {
  203. $this->_sInjectionIndex = $sId;
  204. $this->_aInjections[$sId]['begin'] = microtime ();
  205. }
  206. function endInjection ($sId, $aInjection)
  207. {
  208. if (!isset($this->_aInjections[$sId]))
  209. return;
  210. $iTime = $this->_calcTime ($this->_aInjections[$sId]['begin']);
  211. unset ($this->_aInjections[$sId]['begin']);
  212. $this->_aInjections[$sId]['name'] = isset($aInjection['name']) ? $aInjection['name'] : 'no-name';
  213. $this->_aInjections[$sId]['key'] = $aInjection['key'];
  214. $this->_aInjections[$sId]['replace'] = $aInjection['replace'] ? 'yes' : 'no';
  215. $this->_aInjections[$sId]['time'] = $this->_formatTime($iTime, 5);
  216. $this->_aInjections[$sId]['raw_time'] = $iTime;
  217. }
  218. function beginPageBlock ($sName, $iBlockId)
  219. {
  220. $this->_sPageBlockIndex = $iBlockId;
  221. $this->_aPagesBlocks[$this->_sPageBlockIndex]['name'] = $sName;
  222. $this->_aPagesBlocks[$this->_sPageBlockIndex]['begin'] = microtime ();
  223. }
  224. function endPageBlock ($iBlockId, $isEmpty, $isCached)
  225. {
  226. if (!$this->_sPageBlockIndex)
  227. return;
  228. $iTime = $this->_calcTime ($this->_aPagesBlocks[$this->_sPageBlockIndex]['begin']);
  229. unset ($this->_aPagesBlocks[$this->_sPageBlockIndex]['begin']);
  230. $this->_aPagesBlocks[$this->_sPageBlockIndex]['cached'] = $isCached ? 'yes' : 'no';
  231. $this->_aPagesBlocks[$this->_sPageBlockIndex]['empty'] = $isEmpty ? 'yes' : 'no';
  232. $this->_aPagesBlocks[$this->_sPageBlockIndex]['time'] = $this->_formatTime($iTime, 5);
  233. $this->_aPagesBlocks[$this->_sPageBlockIndex]['raw_time'] = $iTime;
  234. }
  235. function beginPage ($sName)
  236. {
  237. $this->_sPageIndex = md5 ($sName.time().rand());
  238. $this->_aPages[$this->_sPageIndex]['name'] = $sName;
  239. $this->_aPages[$this->_sPageIndex]['begin'] = microtime ();
  240. }
  241. function endPage (&$sContent)
  242. {
  243. if (!$this->_sPageIndex || !isset($this->_aPages[$this->_sPageIndex]['begin']))
  244. return;
  245. $iTime = $this->_calcTime ($this->_aPages[$this->_sPageIndex]['begin']);
  246. unset ($this->_aPages[$this->_sPageIndex]['begin']);
  247. $this->_aPages[$this->_sPageIndex]['time'] = $this->_formatTime($iTime, 5);
  248. $this->_aPages[$this->_sPageIndex]['raw_time'] = $iTime;
  249. }
  250. function beginTemplate ($sName, $sRand)
  251. {
  252. $this->_aTemplateIndexes[$sName.$sRand] = 1;
  253. $this->_aTemplates[$sName.$sRand]['name'] = $sName;
  254. $this->_aTemplates[$sName.$sRand]['begin'] = microtime ();
  255. }
  256. function endTemplate ($sName, $sRand, &$sContent, $isCached)
  257. {
  258. if (!isset($this->_aTemplateIndexes[$sName.$sRand]))
  259. return;
  260. $iTime = $this->_calcTime ($this->_aTemplates[$sName.$sRand]['begin']);
  261. unset ($this->_aTemplates[$sName.$sRand]['begin']);
  262. $this->_aTemplates[$sName.$sRand]['cached'] = $isCached ? 'yes' : 'no';
  263. $this->_aTemplates[$sName.$sRand]['time'] = $this->_formatTime($iTime, 5);
  264. $this->_aTemplates[$sName.$sRand]['raw_time'] = $iTime;
  265. }
  266. function beginQuery ($sSql)
  267. {
  268. $this->_sQueryIndex = md5 ($sSql.time().rand());
  269. $this->_aQueries[$this->_sQueryIndex]['sql'] = $sSql;
  270. $this->_aQueries[$this->_sQueryIndex]['begin'] = microtime ();
  271. }
  272. function endQuery (&$res)
  273. {
  274. if (!$this->_sQueryIndex)
  275. return;
  276. $iTime = $this->_calcTime ($this->_aQueries[$this->_sQueryIndex]['begin']);
  277. unset ($this->_aQueries[$this->_sQueryIndex]['begin']);
  278. $this->_aQueries[$this->_sQueryIndex]['time'] = $this->_formatTime($iTime, 5);
  279. $this->_aQueries[$this->_sQueryIndex]['raw_time'] = $iTime;
  280. $this->_aQueries[$this->_sQueryIndex]['rows'] = $res ? BxDolDb::getInstance()->getNumRows($res) : '';
  281. $this->_aQueries[$this->_sQueryIndex]['affected'] = $res ? BxDolDb::getInstance()->getAffectedRows($res) : '';
  282. if (isset($this->aConf['long_query']) && $iTime > $this->aConf['long_query'])
  283. $this->logSqlQuery ($iTime, $this->_aQueries[$this->_sQueryIndex], $res);
  284. }
  285. function beginMenu ($sName)
  286. {
  287. $this->_aMenus[$sName]['name'] = $sName;
  288. $this->_aMenus[$sName]['begin'] = microtime ();
  289. }
  290. function endMenu ($sName)
  291. {
  292. if (!isset($this->_aMenus[$sName]))
  293. return;
  294. $iTime = $this->_calcTime ($this->_aMenus[$sName]['begin']);
  295. unset ($this->_aMenus[$sName]['begin']);
  296. $this->_aMenus[$sName]['time'] = $this->_formatTime($iTime, 5);
  297. $this->_aMenus[$sName]['raw_time'] = $iTime;
  298. }
  299. function _getCurrentDelay ()
  300. {
  301. $i1 = explode(' ', microtime ());
  302. $i2 = explode(' ', $this->_iTimeStart);
  303. return ($i1[0]+$i1[1]) - ($i2[0]+$i2[1]);
  304. }
  305. function _plankMain ()
  306. {
  307. $sTime = $this->_formatTime($this->_getCurrentDelay ());
  308. if (function_exists('memory_get_usage'))
  309. $sMemory = $this->_formatBytes(memory_get_usage(true)) . ' of ' . ini_get('memory_limit') . ' allowed';
  310. return $this->oTemplate->plank(
  311. $this->oTemplate->nameValue('Time:', $sTime) .
  312. (function_exists('memory_get_usage') ? $this->oTemplate->nameValue('Memory:', $sMemory) : '') .
  313. $this->oTemplate->nameValue('PHP:', phpversion()) .
  314. $this->oTemplate->nameValue('SAPI:', php_sapi_name()) .
  315. $this->oTemplate->nameValue('OS:', php_uname('s r m'))
  316. );
  317. }
  318. function _plankTemplates ()
  319. {
  320. if (empty($GLOBALS['bx_profiler']->_aPages) && empty($GLOBALS['bx_profiler']->_aTemplates))
  321. return;
  322. $sPages = '';
  323. if (!empty($GLOBALS['bx_profiler']->_aPages)) {
  324. $iTimePages = 0;
  325. foreach ($GLOBALS['bx_profiler']->_aPages as $k => $r) {
  326. if (!isset($r['raw_time']))
  327. continue;
  328. $iTimePages += $r['raw_time'];
  329. unset ($GLOBALS['bx_profiler']->_aPages[$k]['raw_time']);
  330. }
  331. $sPages = count($GLOBALS['bx_profiler']->_aPages) . ' (' . $this->_formatTime($iTimePages, 3) . ')';
  332. }
  333. $sTemplatesCached = '';
  334. $sTemplatesNotCached = '';
  335. if ($GLOBALS['bx_profiler']->_aTemplates) {
  336. $iTimeTemplatesCached = 0;
  337. $iTimeTemplatesNotCached = 0;
  338. $sTemplatesCached = 0;
  339. $sTemplatesNotCached = 0;
  340. foreach ($GLOBALS['bx_profiler']->_aTemplates as $k => $r) {
  341. if (!isset($r['raw_time']))
  342. continue;
  343. if ('yes' == $r['cached']) {
  344. $iTimeTemplatesCached += $r['raw_time'];
  345. ++$sTemplatesCached;
  346. } else {
  347. $iTimeTemplatesNotCached += $r['raw_time'];
  348. ++$sTemplatesNotCached;
  349. }
  350. unset ($GLOBALS['bx_profiler']->_aTemplates[$k]['raw_time']);
  351. }
  352. if ($sTemplatesCached)
  353. $sTemplatesCached .= ' (' . $this->_formatTime($iTimeTemplatesCached, 3) . ')';
  354. if ($sTemplatesNotCached)
  355. $sTemplatesNotCached .= ' (' . $this->_formatTime($iTimeTemplatesNotCached, 3) . ')';
  356. }
  357. return $this->oTemplate->plank(
  358. ($sPages ? $this->oTemplate->nameValue('Pages:', $sPages) : '') .
  359. ($sTemplatesCached ? $this->oTemplate->nameValue('Templates Cached:', $sTemplatesCached) : '') .
  360. ($sTemplatesNotCached ? $this->oTemplate->nameValue('Templates Not Cached:', $sTemplatesNotCached) : ''),
  361. $this->oTemplate->table($GLOBALS['bx_profiler']->_aTemplates)
  362. );
  363. }
  364. function _plankInjections ()
  365. {
  366. if (!$GLOBALS['bx_profiler']->_aInjections)
  367. return;
  368. $iTimeInjections = 0;
  369. foreach ($GLOBALS['bx_profiler']->_aInjections as $k => $r) {
  370. if (!isset($r['raw_time']))
  371. continue;
  372. $iTimeInjections += $r['raw_time'];
  373. unset ($GLOBALS['bx_profiler']->_aInjections[$k]['raw_time']);
  374. }
  375. $sInjections = count($GLOBALS['bx_profiler']->_aInjections) . ' injection (' . $this->_formatTime($iTimeInjections, 3) . ')';
  376. return $this->oTemplate->plank(
  377. $this->oTemplate->nameValue('Injections:', $sInjections),
  378. $this->oTemplate->table($GLOBALS['bx_profiler']->_aInjections)
  379. );
  380. }
  381. function _plankPagesBlocks ()
  382. {
  383. if (empty($GLOBALS['bx_profiler']->_aPagesBlocks))
  384. return;
  385. $iTimeBlocks = 0;
  386. $iTimeBlocksCached = 0;
  387. $iTimeBlocksNotCached = 0;
  388. $iTimeBlocksEmpty = 0;
  389. $iTimeBlocksNotEmpty = 0;
  390. $iCountBlocksCached = 0;
  391. $iCountBlocksNotCached = 0;
  392. $iCountBlocksEmpty = 0;
  393. $iCountBlocksNotEmpty = 0;
  394. foreach ($GLOBALS['bx_profiler']->_aPagesBlocks as $k => $r) {
  395. if (!isset($r['raw_time']))
  396. continue;
  397. $iTimeBlocks += $r['raw_time'];
  398. if ($r['cached'] == 'yes') {
  399. $iTimeBlocksCached += $r['raw_time'];
  400. ++$iCountBlocksCached;
  401. } else {
  402. $iTimeBlocksNotCached += $r['raw_time'];
  403. ++$iCountBlocksNotCached;
  404. }
  405. if ($r['empty'] == 'yes') {
  406. $iTimeBlocksEmpty += $r['raw_time'];
  407. ++$iCountBlocksEmpty;
  408. } else {
  409. $iTimeBlocksNotEmpty += $r['raw_time'];
  410. ++$iCountBlocksNotEmpty;
  411. }
  412. unset ($GLOBALS['bx_profiler']->_aPagesBlocks[$k]['raw_time']);
  413. }
  414. $sBlocks = count($GLOBALS['bx_profiler']->_aPagesBlocks) . ' (' . $this->_formatTime($iTimeBlocks, 3) . ')';
  415. if ($iCountBlocksCached)
  416. $sBlocksCached = $iCountBlocksCached . ' (' . $this->_formatTime($iTimeBlocksCached, 3) . ')';
  417. if ($iCountBlocksNotCached)
  418. $sBlocksNotCached = $iCountBlocksNotCached . ' (' . $this->_formatTime($iTimeBlocksNotCached, 3) . ')';
  419. if ($iCountBlocksEmpty)
  420. $sBlocksEmpty = $iCountBlocksEmpty . ' (' . $this->_formatTime($iTimeBlocksEmpty, 3) . ')';
  421. if ($iCountBlocksNotEmpty)
  422. $sBlocksNotEmpty = $iCountBlocksNotEmpty . ' (' . $this->_formatTime($iTimeBlocksNotEmpty, 3) . ')';
  423. return $this->oTemplate->plank(
  424. $this->oTemplate->nameValue('Pages Blocks:', $sBlocks) .
  425. ($iCountBlocksCached ? $this->oTemplate->nameValue('Cached:', $sBlocksCached) : '') .
  426. ($iCountBlocksNotCached ? $this->oTemplate->nameValue('Not Cached:', $sBlocksNotCached) : '') .
  427. ($iCountBlocksEmpty ? $this->oTemplate->nameValue('Empty:', $sBlocksEmpty) : '') .
  428. ($iCountBlocksNotEmpty ? $this->oTemplate->nameValue('Not Empty:', $sBlocksNotEmpty) : ''),
  429. $this->oTemplate->table($GLOBALS['bx_profiler']->_aPagesBlocks)
  430. );
  431. }
  432. function _plankMenus ()
  433. {
  434. if (empty($GLOBALS['bx_profiler']->_aMenus))
  435. return;
  436. $iTimeMenus = 0;
  437. foreach ($GLOBALS['bx_profiler']->_aMenus as $k => $r) {
  438. if (!isset($r['raw_time']))
  439. continue;
  440. $iTimeMenus += $r['raw_time'];
  441. unset ($GLOBALS['bx_profiler']->_aMenus[$k]['raw_time']);
  442. }
  443. $sMenus = count($GLOBALS['bx_profiler']->_aMenus) . ' menus (' . $this->_formatTime($iTimeMenus, 3) . ')';
  444. return $this->oTemplate->plank(
  445. $this->oTemplate->nameValue('Menus:', $sMenus),
  446. $this->oTemplate->table($GLOBALS['bx_profiler']->_aMenus)
  447. );
  448. }
  449. function _plankSql ()
  450. {
  451. if (empty($GLOBALS['bx_profiler']->_aQueries))
  452. return;
  453. $iTimeQueries = 0;
  454. foreach ($GLOBALS['bx_profiler']->_aQueries as $k => $r) {
  455. if (!isset($r['raw_time']))
  456. continue;
  457. $iTimeQueries += $r['raw_time'];
  458. unset ($GLOBALS['bx_profiler']->_aQueries[$k]['raw_time']);
  459. }
  460. $sQueries = count($GLOBALS['bx_profiler']->_aQueries) . ' queries (' . $this->_formatTime($iTimeQueries, 3) . ')';
  461. return $this->oTemplate->plank(
  462. $this->oTemplate->nameValue('SQL:', $sQueries),
  463. $this->oTemplate->table($GLOBALS['bx_profiler']->_aQueries, 'sql')
  464. );
  465. }
  466. function _plankModules ()
  467. {
  468. if (empty($GLOBALS['bx_profiler']->_aModules))
  469. return;
  470. $iTimeModules = 0;
  471. foreach ($GLOBALS['bx_profiler']->_aModules as $k => $r) {
  472. if (!isset($r['raw_time']))
  473. continue;
  474. $iTimeModules += $r['raw_time'];
  475. unset ($GLOBALS['bx_profiler']->_aModules[$k]['raw_time']);
  476. }
  477. $sModules = count($GLOBALS['bx_profiler']->_aModulesNames) . ' modules loaded';
  478. $sModulesQueries = count($GLOBALS['bx_profiler']->_aModules) . ' modules queries (' . $this->_formatTime($iTimeModules, 3) . ')';
  479. return $this->oTemplate->plank(
  480. $this->oTemplate->nameValue('Modules:', $sModules) .
  481. $this->oTemplate->nameValue('Modules Queries:', $sModulesQueries),
  482. $this->oTemplate->table($GLOBALS['bx_profiler']->_aModules)
  483. );
  484. }
  485. function _plankAlerts ()
  486. {
  487. if (empty($GLOBALS['bx_profiler']->_aAlerts))
  488. return;
  489. $iTimeAlerts = 0;
  490. foreach ($GLOBALS['bx_profiler']->_aAlerts as $k => $r) {
  491. if (!isset($r['raw_time']))
  492. continue;
  493. $iTimeAlerts += $r['raw_time'];
  494. unset ($GLOBALS['bx_profiler']->_aAlerts[$k]['raw_time']);
  495. }
  496. $sAlerts = count($GLOBALS['bx_profiler']->_aAlerts) . ' alerts responses (' . $this->_formatTime($iTimeAlerts, 3) . ')';
  497. return $this->oTemplate->plank(
  498. $this->oTemplate->nameValue('Alerts:', $sAlerts) .
  499. $this->oTemplate->table($GLOBALS['bx_profiler']->_aAlerts)
  500. );
  501. }
  502. function _formatTime ($i, $iPrecision = 3)
  503. {
  504. return round($i, $iPrecision) . ' sec';
  505. }
  506. function _formatBytes ($i)
  507. {
  508. if ($i > 1024*1024)
  509. return round($i/1024/1024, 1) . 'M';
  510. elseif ($i > 1024)
  511. return round($i/1024, 1) . 'K';
  512. else
  513. return $i . 'B';
  514. }
  515. function _isProfilerDisabled()
  516. {
  517. if (isset($GLOBALS['bx_profiler_disable']) || isset($_GET['bx_profiler_disable']))
  518. return true;
  519. if (
  520. preg_match('/gzip_loader\.php/', $_SERVER['PHP_SELF']) ||
  521. preg_match('/get_rss_feed\.php/', $_SERVER['PHP_SELF']) ||
  522. preg_match('/fields\.parse\.php/', $_SERVER['PHP_SELF']) ||
  523. preg_match('/flash/', $_SERVER['PHP_SELF']) ||
  524. preg_match('/forum/', $_SERVER['PHP_SELF'])
  525. )
  526. return true;
  527. return false;
  528. }
  529. function _calcTime ($begin)
  530. {
  531. $i1 = explode(' ', microtime ());
  532. $i2 = explode(' ', $begin);
  533. return ($i1[0]+$i1[1]) - ($i2[0]+$i2[1]);
  534. }
  535. function _debugPrintArray ($mixed)
  536. {
  537. $sArgs .= 'Array(';
  538. foreach ($mixed as $mixed2)
  539. $sArgs .= (is_object($mixed2) ? $this->_debugPrintObject($mixed2) : $this->_debugPrintAny($mixed2)) . ',';
  540. $sArgs = substr($sArgs, 0, -1);
  541. $sArgs .= ')';
  542. return $sArgs;
  543. }
  544. function _debugPrintObject ($mixed)
  545. {
  546. return get_class($mixed) . ' instance';
  547. }
  548. function _debugPrintAny ($mixed)
  549. {
  550. if (is_string)
  551. return "'" . (strlen($mixed) > $this->_sLogMaxArgLength ? substr($mixed, 0, $this->_sLogMaxArgLength) . '...' : $mixed) . "'";
  552. else
  553. return $mixed;
  554. }
  555. function _debugBackTrace ($iShifts = 0)
  556. {
  557. $a = debug_backtrace();
  558. while (--$iShifts > -1)
  559. array_shift($a);
  560. $s = '';
  561. foreach ($a as $r) {
  562. $sArgs = '';
  563. foreach ($r['args'] as $mixed) {
  564. switch (true) {
  565. case is_array($mixed):
  566. $sArgs .= $this->_debugPrintArray($mixed);
  567. break;
  568. case is_object($mixed):
  569. $sArgs .= $this->_debugPrintObject($mixed);
  570. break;
  571. default:
  572. $sArgs .= $this->_debugPrintAny($mixed);
  573. }
  574. $sArgs .= ',';
  575. }
  576. $sArgs = substr($sArgs, 0, -1);
  577. $s .= "--------------------------------------\n";
  578. $s .= "{$r['line']} {$r['file']}\n";
  579. $s .= "{$r['class']}{$r['type']}{$r['function']} ({$sArgs});\n";
  580. }
  581. return $s;
  582. }
  583. function _getHeaderContentType()
  584. {
  585. $aHeaders = headers_list();
  586. foreach ($aHeaders as $s) {
  587. $a = explode(':', $s);
  588. if (isset($a[0]) && isset($a[1]) && 'content-type' == strtolower(trim($a[0]))) {
  589. $aa = explode(';', $a[1]);
  590. return strtolower(trim($aa[0]));
  591. }
  592. }
  593. return false;
  594. }
  595. }
  596. $GLOBALS['bx_profiler'] = new BxProfiler(BX_DOL_START);
  597. if (!$GLOBALS['bx_profiler']->_isProfilerDisabled())
  598. register_shutdown_function (array ($GLOBALS['bx_profiler'], 'output'));
  599. /** @} */