Xhprof分析php性能

时间:2022-05-17 16:16:46

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

Xhprof分析php性能

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

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

[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" ;目录要事先建好

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

<?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>';
}
}
}

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

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

  

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

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

Xhprof分析php性能