Xhprof.php 3.16 KB
<?php
/**
 * Created by PhpStorm.
 * User: Zip
 * Date: 2/22/15
 * Time: 11:42 AM
 */

namespace Plugin;

use Yaf\Request_Abstract;
use Yaf\Response_Abstract;
use Yaf\Plugin_abstract;
use Hood\Debug\XHProf as hoodXHProf;

class Xhprof extends Plugin_abstract
{

    private $beginTime = 0;

    private $requestSlowTimeout = 500;

    private $outputDir = null;

    public function __construct($requestSlowTimeout = 500)
    {
        $this->requestSlowTimeout = $requestSlowTimeout;
    }

    /**
     * 设置分析数据路径
     * @param null $outputDir
     * @return $this
     */
    public function setOutputDir($outputDir = null)
    {
        $this->outputDir = $outputDir;
        return $this;
    }

    /**
     * (Yaf >= 2.2.9)
     * 在路由之前触发
     *
     * @param Request_Abstract $request 当前请求对象
     * @param Response_Abstract $response 当前响应对象
     *
     * @return mixed
     */
    public function routerStartup(Request_Abstract $request, Response_Abstract $response)
    {
        $this->beginTime = microtime(true);
        xhprof_enable();
    }

    /**
     * (Yaf >= 2.2.9)
     * 路由结束之后触发
     *
     * @param Request_Abstract $request 当前请求对象
     * @param Response_Abstract $response 当前响应对象
     *
     * @return mixed
     */
    public function routerShutdown(Request_Abstract $request, Response_Abstract $response)
    {
    }

    /**
     * (Yaf >= 2.2.9)
     * 分发循环开始之前被触发
     *
     * @param Request_Abstract $request 当前请求对象
     * @param Response_Abstract $response 当前响应对象
     *
     * @return mixed
     */
    public function dispatchLoopStartup(Request_Abstract $request, Response_Abstract $response)
    {
    }

    /**
     * (Yaf >= 2.2.9)
     * 分发之前触发
     *
     * @param Request_Abstract $request 当前请求对象
     * @param Response_Abstract $response 当前响应对象
     *
     * @return mixed
     */
    public function preDispatch(Request_Abstract $request, Response_Abstract $response)
    {
    }

    /**
     * (Yaf >= 2.2.9)
     * 分发结束之后触发
     *
     * @param Request_Abstract $request 当前请求对象
     * @param Response_Abstract $response 当前响应对象
     *
     * @return mixed
     */
    public function postDispatch(Request_Abstract $request, Response_Abstract $response)
    {
    }

    /**
     * (Yaf >= 2.2.9)
     * dispatchLoopShutdown
     *
     * @param Request_Abstract $request 当前请求对象
     * @param Response_Abstract $response 当前响应对象
     *
     * @return mixed
     */
    public function dispatchLoopShutdown(Request_Abstract $request, Response_Abstract $response)
    {
        $useTime = round((microtime(true) - $this->beginTime) * 1000);
        if ($useTime > $this->requestSlowTimeout) {
            $xhprofData = xhprof_disable();
            $dir = $this->outputDir . $request->getRequestUri();
            $xhprofRuns = new hoodXHProf\Runs($dir);
            $runID = $xhprofRuns->save_run($xhprofData, "service");
        }
        //echo "<p><a href='http://x/index.php?run=$run_id&source=xhprof_foo' target='_blank'>Xhprof</a></p>";
    }
}