ShowRuntimeBehavior.class.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 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. namespace Behavior;
  12. /**
  13. * 系统行为扩展:运行时间信息显示
  14. */
  15. class ShowRuntimeBehavior {
  16. // 行为扩展的执行入口必须是run
  17. public function run(&$content){
  18. if(C('SHOW_RUN_TIME')){
  19. if(false !== strpos($content,'{__NORUNTIME__}')) {
  20. $content = str_replace('{__NORUNTIME__}','',$content);
  21. }else{
  22. $runtime = $this->showTime();
  23. if(strpos($content,'{__RUNTIME__}'))
  24. $content = str_replace('{__RUNTIME__}',$runtime,$content);
  25. else
  26. $content .= $runtime;
  27. }
  28. }else{
  29. $content = str_replace(array('{__NORUNTIME__}','{__RUNTIME__}'),'',$content);
  30. }
  31. }
  32. /**
  33. * 显示运行时间、数据库操作、缓存次数、内存使用信息
  34. * @access private
  35. * @return string
  36. */
  37. private function showTime() {
  38. // 显示运行时间
  39. G('beginTime',$GLOBALS['_beginTime']);
  40. G('viewEndTime');
  41. $showTime = 'Process: '.G('beginTime','viewEndTime').'s ';
  42. if(C('SHOW_ADV_TIME')) {
  43. // 显示详细运行时间
  44. $showTime .= '( Load:'.G('beginTime','loadTime').'s Init:'.G('loadTime','initTime').'s Exec:'.G('initTime','viewStartTime').'s Template:'.G('viewStartTime','viewEndTime').'s )';
  45. }
  46. if(C('SHOW_DB_TIMES') ) {
  47. // 显示数据库操作次数
  48. $showTime .= ' | DB :'.N('db_query').' queries '.N('db_write').' writes ';
  49. }
  50. if(C('SHOW_CACHE_TIMES') ) {
  51. // 显示缓存读写次数
  52. $showTime .= ' | Cache :'.N('cache_read').' gets '.N('cache_write').' writes ';
  53. }
  54. if(MEMORY_LIMIT_ON && C('SHOW_USE_MEM')) {
  55. // 显示内存开销
  56. $showTime .= ' | UseMem:'. number_format((memory_get_usage() - $GLOBALS['_startUseMems'])/1024).' kb';
  57. }
  58. if(C('SHOW_LOAD_FILE')) {
  59. $showTime .= ' | LoadFile:'.count(get_included_files());
  60. }
  61. if(C('SHOW_FUN_TIMES')) {
  62. $fun = get_defined_functions();
  63. $showTime .= ' | CallFun:'.count($fun['user']).','.count($fun['internal']);
  64. }
  65. return $showTime;
  66. }
  67. }