BxProfiler.php 25 KB

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