performance_helper.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 static function clear() {
  43. self::instance()->mMsgs = array();
  44. }
  45. public static function format_log() {
  46. foreach(self::instance()->mMsgs as $val) {
  47. echo $val . "<br/><br/>";
  48. }
  49. }
  50. }
  51. function perfor_start()
  52. {
  53. performance_helper::instance()->start();
  54. }
  55. function perfor_end($tag,$info = '')
  56. {
  57. performance_helper::instance()->push($tag,$info);
  58. }
  59. function perfor_period($tag,$start,$info = '')
  60. {
  61. performance_helper::instance()->push($tag,$info,$start);
  62. }