Xhprof分析php性能

https://windows.php.net/downloads/pecl/releases/xhprof/0.10.6/ 下载Xhprof版本php

配置一个本地访问url,指向index.php,能访问便可。http://loc.oms.xhprof:9091前端

 

而后取php官网下载扩展,写在php.ini上java

[xhprof] 
extension=php_xhprof.dll 
; directory used by default implementation of the iXHProfRuns 
; interface (namely, the XHProfRuns_Default class) for storing 
; XHProf runs. 
xhprof.output_dir="D:/xampp/php/xhprof" ;目录要事先建好

  在项目里,创建一个公共类文件:windows

<?php

namespace Order\Common\Tool;

class XhprofHelper
{
    public static function start()
    {
        if(extension_loaded('xhprof')){
            //载入下载的XHPROF包中的2个文件夹
            include_once __DIR__ . '/xhprof-0.9.4/xhprof_lib/utils/xhprof_lib.php';
            include_once __DIR__ . '/xhprof-0.9.4/xhprof_lib/utils/xhprof_runs.php';
            //xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
            xhprof_enable( XHPROF_FLAGS_NO_BUILTINS | XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY);
        }
    }

    public static function stop()
    {
        if(extension_loaded('xhprof')){
            $ns = 'myXhprof';
            //关闭profiler
            $xhprofData = xhprof_disable();
            //实例化类
            $xhprofRuns = new \XHProfRuns_Default();
            $runId = $xhprofRuns->save_run($xhprofData, $ns);
            //前端展现库的URL
            $url = 'http://loc.oms.xhprof:9091/index.php';
            $url .= '?run=%s&source=%s';
            //变量替换
            $url = sprintf($url, $runId, $ns);
            //输入URL
            echo '<a href="'.$url.'" target="_blank">查看结果</a>';
        }
    }
}

  在项目文件里,调用类方法:url

public function getSearchConfig(){
        XhprofHelper::start();
		$lan = I('get.language', 'zh', 'trim');

  

$selfPickupType = C('TaiwanYourPickup');
        $data['selfPickupType'] = $selfPickupType[$lan];
        XhprofHelper::stop();exit;

  访问:http://loc.oms.xhprof:9091/callgraph.php?run=5c5430d519c87&source=myXhprofspa