1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 16/4/6
- * Time: 下午9:25
- */
- class performance_helper
- {
- private $mMsgs;
- private $mStart;
- static private $stInstance = NULL;
- public function __construct() {
- $this->mMsgs = array();
- $this->mStart = microtime(true);
- }
- static public function instance()
- {
- if(self::$stInstance == NULL) {
- self::$stInstance = new performance_helper();
- }
- return self::$stInstance;
- }
- public function start()
- {
- $this->mStart = microtime(true);
- }
- public function push($tag,$info = '',$start = NULL)
- {
- if($start == NULL) {
- $start = $this->mStart;
- $this->mStart = microtime(true);
- }
- if(empty($info)) {
- $msg = sprintf("%s:%.6f",$tag,microtime(true) - $start);
- } else {
- $msg = sprintf("%s:%.6f,info=",$tag,microtime(true) - $start);
- $msg = $msg.$info;
- }
- array_push($this->mMsgs,$msg);
- }
- public function clear()
- {
- $this->mMsgs = [];
- }
- public function format_log() {
- $content = '';
- foreach($this->mMsgs as $val) {
- $content .= $val . "\n";
- }
- return $content;
- }
- }
- function perfor_start()
- {
- performance_helper::instance()->start();
- }
- function perfor_log()
- {
- return performance_helper::instance()->format_log();
- }
- function perfor_end($tag,$info = '')
- {
- performance_helper::instance()->push($tag,$info);
- }
- function perfor_period($tag,$start,$info = '')
- {
- performance_helper::instance()->push($tag,$info,$start);
- }
- function perfor_clear()
- {
- performance_helper::instance()->clear();
- }
|