BxProfiler.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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 (!isset($GLOBALS['logged']) || !$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. $sPlanks = $this->_plankMain ();
  89. $sPlanks .= $this->_plankMenus ();
  90. $sPlanks .= $this->_plankTemplates ();
  91. $sPlanks .= $this->_plankInjections ();
  92. $sPlanks .= $this->_plankPagesBlocks ();
  93. $sPlanks .= $this->_plankSql ();
  94. $sPlanks .= $this->_plankModules ();
  95. $sPlanks .= $this->_plankAlerts ();
  96. echo ob_get_clean();
  97. $sContentType = $this->oTemplate->getHeaderContentType();
  98. if ('text/html' == $sContentType) {
  99. echo $sJsCss . $sPlanks;
  100. }
  101. else if ('application/json' == $sContentType) {
  102. echo $sPlanks;
  103. }
  104. }
  105. function _logBegin ($s)
  106. {
  107. $sDate = date ($this->_sLogDateFormat);
  108. return "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" . $sDate . " " . $s . "\n" .
  109. (getLoggedId() ? "User ID: " . getLoggedId() . "\n" : '') .
  110. "User role: " . ($GLOBALS['logged']['admin'] ? 'admin' : ($GLOBALS['logged']['member'] ? 'member' : 'guest')) . "\n";
  111. }
  112. function _logEnd ()
  113. {
  114. return "\n";
  115. }
  116. function _appendToLog ($s)
  117. {
  118. bx_log('bx_profiler', $s);
  119. }
  120. function logSqlQuery ($iTime, $aSqlQuery, &$oStmt)
  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));
  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);
  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 . $this->_iAlertsLevel);
  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. --$this->_iAlertsLevel;
  195. $sHash = md5($sUnit . $sAction . $sHandler . $this->_iAlertsLevel);
  196. $iTime = $this->_calcTime ($this->_aAlerts[$sHash]['begin']);
  197. unset ($this->_aAlerts[$sHash]['begin']);
  198. $this->_aAlerts[$sHash]['time'] = $this->_formatTime($iTime);
  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);
  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);
  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);
  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);
  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 (&$oStmt)
  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);
  279. $this->_aQueries[$this->_sQueryIndex]['raw_time'] = $iTime;
  280. $this->_aQueries[$this->_sQueryIndex]['rows'] = $oStmt ? BxDolDb::getInstance()->getNumRows($oStmt) : '';
  281. $this->_aQueries[$this->_sQueryIndex]['affected'] = $oStmt ? BxDolDb::getInstance()->getAffectedRows($oStmt) : '';
  282. if (isset($this->aConf['long_query']) && $iTime > $this->aConf['long_query'])
  283. $this->logSqlQuery ($iTime, $this->_aQueries[$this->_sQueryIndex], $oStmt);
  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);
  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 (), 3);
  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 = 4)
  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. return false;
  520. }
  521. function _calcTime ($begin)
  522. {
  523. $i1 = explode(' ', microtime ());
  524. $i2 = explode(' ', $begin);
  525. return ($i1[0]+$i1[1]) - ($i2[0]+$i2[1]);
  526. }
  527. function _debugPrintArray ($mixed)
  528. {
  529. $sArgs .= 'Array(';
  530. foreach ($mixed as $mixed2)
  531. $sArgs .= (is_object($mixed2) ? $this->_debugPrintObject($mixed2) : $this->_debugPrintAny($mixed2)) . ',';
  532. $sArgs = substr($sArgs, 0, -1);
  533. $sArgs .= ')';
  534. return $sArgs;
  535. }
  536. function _debugPrintObject ($mixed)
  537. {
  538. return get_class($mixed) . ' instance';
  539. }
  540. function _debugPrintAny ($mixed)
  541. {
  542. if (is_string)
  543. return "'" . (strlen($mixed) > $this->_sLogMaxArgLength ? substr($mixed, 0, $this->_sLogMaxArgLength) . '...' : $mixed) . "'";
  544. else
  545. return $mixed;
  546. }
  547. function _debugBackTrace ($iShifts = 0)
  548. {
  549. $a = debug_backtrace();
  550. while (--$iShifts > -1)
  551. array_shift($a);
  552. $s = '';
  553. foreach ($a as $r) {
  554. $sArgs = '';
  555. foreach ($r['args'] as $mixed) {
  556. switch (true) {
  557. case is_array($mixed):
  558. $sArgs .= $this->_debugPrintArray($mixed);
  559. break;
  560. case is_object($mixed):
  561. $sArgs .= $this->_debugPrintObject($mixed);
  562. break;
  563. default:
  564. $sArgs .= $this->_debugPrintAny($mixed);
  565. }
  566. $sArgs .= ',';
  567. }
  568. $sArgs = substr($sArgs, 0, -1);
  569. $s .= "--------------------------------------\n";
  570. $s .= "{$r['line']} {$r['file']}\n";
  571. $s .= "{$r['class']}{$r['type']}{$r['function']} ({$sArgs});\n";
  572. }
  573. return $s;
  574. }
  575. }
  576. $GLOBALS['bx_profiler'] = new BxProfiler(BX_DOL_START);
  577. if (!$GLOBALS['bx_profiler']->_isProfilerDisabled())
  578. register_shutdown_function (array ($GLOBALS['bx_profiler'], 'output'));
  579. /** @} */