<?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 static function clear() {
        self::instance()->mMsgs = array();
    }

    public static function format_log() {
        foreach(self::instance()->mMsgs as $val) {
            echo $val . "<br/><br/>";
        }
    }
}

function perfor_start()
{
    performance_helper::instance()->start();
}

function perfor_end($tag,$info = '')
{
    performance_helper::instance()->push($tag,$info);
}

function perfor_period($tag,$start,$info = '')
{
    performance_helper::instance()->push($tag,$info,$start);
}