ShowRuntimeBehavior.class.php 2.9 KB

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