ShowRuntimeBehavior.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. defined('THINK_PATH') or exit();
  12. /**
  13. * 系统行为扩展:运行时间信息显示
  14. * @category Think
  15. * @package Think
  16. * @subpackage Behavior
  17. * @author liu21st <liu21st@gmail.com>
  18. */
  19. class ShowRuntimeBehavior extends Behavior {
  20. // 行为参数定义
  21. protected $options = array(
  22. 'SHOW_RUN_TIME' => false, // 运行时间显示
  23. 'SHOW_ADV_TIME' => false, // 显示详细的运行时间
  24. 'SHOW_DB_TIMES' => false, // 显示数据库查询和写入次数
  25. 'SHOW_CACHE_TIMES' => false, // 显示缓存操作次数
  26. 'SHOW_USE_MEM' => false, // 显示内存开销
  27. 'SHOW_LOAD_FILE' => false, // 显示加载文件数
  28. 'SHOW_FUN_TIMES' => false , // 显示函数调用次数
  29. );
  30. // 行为扩展的执行入口必须是run
  31. public function run(&$content){
  32. if(C('SHOW_RUN_TIME')){
  33. if(false !== strpos($content,'{__NORUNTIME__}')) {
  34. $content = str_replace('{__NORUNTIME__}','',$content);
  35. }else{
  36. $runtime = $this->showTime();
  37. if(strpos($content,'{__RUNTIME__}'))
  38. $content = str_replace('{__RUNTIME__}',$runtime,$content);
  39. else
  40. $content .= $runtime;
  41. }
  42. }else{
  43. $content = str_replace(array('{__NORUNTIME__}','{__RUNTIME__}'),'',$content);
  44. }
  45. }
  46. /**
  47. * 显示运行时间、数据库操作、缓存次数、内存使用信息
  48. * @access private
  49. * @return string
  50. */
  51. private function showTime() {
  52. // 显示运行时间
  53. G('beginTime',$GLOBALS['_beginTime']);
  54. G('viewEndTime');
  55. $showTime = 'Process: '.G('beginTime','viewEndTime').'s ';
  56. if(C('SHOW_ADV_TIME')) {
  57. // 显示详细运行时间
  58. $showTime .= '( Load:'.G('beginTime','loadTime').'s Init:'.G('loadTime','initTime').'s Exec:'.G('initTime','viewStartTime').'s Template:'.G('viewStartTime','viewEndTime').'s )';
  59. }
  60. if(C('SHOW_DB_TIMES') && class_exists('Db',false) ) {
  61. // 显示数据库操作次数
  62. $showTime .= ' | DB :'.N('db_query').' queries '.N('db_write').' writes ';
  63. }
  64. if(C('SHOW_CACHE_TIMES') && class_exists('Cache',false)) {
  65. // 显示缓存读写次数
  66. $showTime .= ' | Cache :'.N('cache_read').' gets '.N('cache_write').' writes ';
  67. }
  68. if(MEMORY_LIMIT_ON && C('SHOW_USE_MEM')) {
  69. // 显示内存开销
  70. $showTime .= ' | UseMem:'. number_format((memory_get_usage() - $GLOBALS['_startUseMems'])/1024).' kb';
  71. }
  72. if(C('SHOW_LOAD_FILE')) {
  73. $showTime .= ' | LoadFile:'.count(get_included_files());
  74. }
  75. if(C('SHOW_FUN_TIMES')) {
  76. $fun = get_defined_functions();
  77. $showTime .= ' | CallFun:'.count($fun['user']).','.count($fun['internal']);
  78. }
  79. return $showTime;
  80. }
  81. }