ShowRuntimeBehavior.class.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. // +--------------------------------------------------------------------------
  3. // | Senthot [ DEVELOPED BY ME ]
  4. // +--------------------------------------------------------------------------
  5. // | Copyright (c) 2005-2013 http://www.senthot.com All rights reserved.
  6. // | License ( http://www.apache.org/licenses/LICENSE-2.0 )
  7. // | Author: ms134n ( [email protected] )
  8. // +--------------------------------------------------------------------------
  9. defined('SEN_PATH') or exit();
  10. /**
  11. * System behavior extension : Run time information display
  12. * @category Sen
  13. * @package Sen
  14. * @subpackage Behavior
  15. * @author ms134n <[email protected]>
  16. */
  17. class ShowRuntimeBehavior extends Behavior {
  18. // Parameter defines the behavior
  19. protected $options = array(
  20. 'SHOW_RUN_TIME' => false, // Running time display
  21. 'SHOW_ADV_TIME' => false, // Show detailed run-time
  22. 'SHOW_DB_TIMES' => false, // Show database queries and write times
  23. 'SHOW_CACHE_TIMES' => false, // Display the number of cache operation
  24. 'SHOW_USE_MEM' => false, // Display memory overhead
  25. 'SHOW_LOAD_FILE' => false, // Display the number of files loaded
  26. 'SHOW_FUN_TIMES' => false , // Shows the number of function calls
  27. );
  28. // Behavior extension execution entry must be run
  29. public function run(&$content){
  30. if(C('SHOW_RUN_TIME')){
  31. if(false !== strpos($content,'{__NORUNTIME__}')) {
  32. $content = str_replace('{__NORUNTIME__}','',$content);
  33. }else{
  34. $runtime = $this->showTime();
  35. if(strpos($content,'{__RUNTIME__}'))
  36. $content = str_replace('{__RUNTIME__}',$runtime,$content);
  37. else
  38. $content .= $runtime;
  39. }
  40. }else{
  41. $content = str_replace(array('{__NORUNTIME__}','{__RUNTIME__}'),'',$content);
  42. }
  43. }
  44. /**
  45. * Show running time, database operations, the number of cache , memory usage information
  46. * @access private
  47. * @return string
  48. */
  49. private function showTime() {
  50. // Show Run Time
  51. G('beginTime',$GLOBALS['_beginTime']);
  52. G('viewEndTime');
  53. $showTime = 'Process: '.G('beginTime','viewEndTime').'s ';
  54. if(C('SHOW_ADV_TIME')) {
  55. // Show Run Time
  56. $showTime .= '( Load:'.G('beginTime','loadTime').'s Init:'.G('loadTime','initTime').'s Exec:'.G('initTime','viewStartTime').'s Template:'.G('viewStartTime','viewEndTime').'s )';
  57. }
  58. if(C('SHOW_DB_TIMES') && class_exists('Db',false) ) {
  59. // Shows the number of database operations
  60. $showTime .= ' | DB :'.N('db_query').' queries '.N('db_write').' writes ';
  61. }
  62. if(C('SHOW_CACHE_TIMES') && class_exists('Cache',false)) {
  63. // Display cache read and write times
  64. $showTime .= ' | Cache :'.N('cache_read').' gets '.N('cache_write').' writes ';
  65. }
  66. if(MEMORY_LIMIT_ON && C('SHOW_USE_MEM')) {
  67. // Display memory overhead
  68. $showTime .= ' | UseMem:'. number_format((memory_get_usage() - $GLOBALS['_startUseMems'])/1024).' kb';
  69. }
  70. if(C('SHOW_LOAD_FILE')) {
  71. $showTime .= ' | LoadFile:'.count(get_included_files());
  72. }
  73. if(C('SHOW_FUN_TIMES')) {
  74. $fun = get_defined_functions();
  75. $showTime .= ' | CallFun:'.count($fun['user']).','.count($fun['internal']);
  76. }
  77. return $showTime;
  78. }
  79. }