performance_helper.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/4/6
  6. * Time: 下午9:25
  7. */
  8. class performance_helper
  9. {
  10. private $mMsgs;
  11. private $mStart;
  12. static private $stInstance = NULL;
  13. public function __construct() {
  14. $this->mMsgs = array();
  15. $this->mStart = microtime(true);
  16. }
  17. static public function instance()
  18. {
  19. if(self::$stInstance == NULL) {
  20. self::$stInstance = new performance_helper();
  21. }
  22. return self::$stInstance;
  23. }
  24. public function start()
  25. {
  26. $this->mStart = microtime(true);
  27. }
  28. public function push($tag,$info = '',$start = NULL)
  29. {
  30. if($start == NULL) {
  31. $start = $this->mStart;
  32. $this->mStart = microtime(true);
  33. }
  34. if(empty($info)) {
  35. $msg = sprintf("%s:%.6f",$tag,microtime(true) - $start);
  36. } else {
  37. $msg = sprintf("%s:%.6f,info=",$tag,microtime(true) - $start);
  38. $msg = $msg.$info;
  39. }
  40. array_push($this->mMsgs,$msg);
  41. }
  42. public function clear()
  43. {
  44. $this->mMsgs = [];
  45. }
  46. public function format_log() {
  47. $content = '';
  48. foreach($this->mMsgs as $val) {
  49. $content .= $val . "\n";
  50. }
  51. return $content;
  52. }
  53. }
  54. function perfor_start()
  55. {
  56. performance_helper::instance()->start();
  57. }
  58. function perfor_log()
  59. {
  60. return performance_helper::instance()->format_log();
  61. }
  62. function perfor_end($tag,$info = '')
  63. {
  64. performance_helper::instance()->push($tag,$info);
  65. }
  66. function perfor_period($tag,$start,$info = '')
  67. {
  68. performance_helper::instance()->push($tag,$info,$start);
  69. }
  70. function perfor_clear()
  71. {
  72. performance_helper::instance()->clear();
  73. }