45IT.COM- 电脑学习从此开始!
DIY硬件教程攒机经验装机配置
设计Photoshop网页设计特效
系统注册表DOS系统命令其它
存储主板显卡外设键鼠内存
维修显卡CPU内存打印机
WinXPVistaWin7unix/linux
CPU光驱电源/散热显示器其它
修技主板硬盘键鼠显示器光驱
办公ExcelWordPowerPointWPS
编程数据库CSS脚本PHP
网络局域网QQ服务器
软件网络系统图像安全
页面导航: 首页 > 设计学院 > 网络编程 > PHP教程 >

PHP代码性能的分析方法

电脑软硬件应用网 45IT.COM 时间:2014-07-27 11:43 作者:45itcom

php代码的性能分析。

你可以用xdbug去分析。

但是更好的选择是facebook的性能分析工具xhprof。

它可以图形化。前提是你安装了gd库,你也可能遇到一些小问题。我记得要更新linux的图像库。

安装xhprof扩展:pecl install xhprof .

<?php
/**
 *
 *
 * Beck Confidential
 * Copyright (c) 2013, Beck Corp. <Beck.Bi>.
 * All rights reserved.
 *
 * PHP version 5
 *
 * @category  Aug
 * @package package_name
 * @author beck
 * @date 2013-8-13
 * @license
 * @link
 *
 */
class Xhprof
{
    protected $flags = 0;
    protected $options = array();
    protected $xhprofData = array();
    /**
     * 配置你的xhprof 你可以在php的官网看着个应用的说明
     * @param unknown $config
     * @throws ExtensionNotFoundException
     */
    public function __construct($config = array())
    {
        if (!extension_loaded('xhprof')) {
            throw new ExtensionNotFoundException(
                'Configuration error! Make sure you have xhprof installed correctly.
                please refer http://www.php.net/manual/en/xhprof.examples.php for detail.'
            );
        }
        if (!empty($config['flags'])) {
            $this->flags = (int)$config['flags'];
        }
        if (!empty($config['options'])) {
            $this->options = $config['options'];
        }
    }
    /**
     * 开启调试
     */
    public function enable()
    {
        xhprof_enable($this->flags, $this->options);
    }
    public function disable()
    {
        $this->xhprofData =  xhprof_disable();
    }
    /**
     *显示调试结果
     * 你可能需要配置一个apache/nginx虚拟主机
     */
    public function show()
    {
        $this->disable();
        include_once "xhprof_lib/utils/xhprof_lib.php";
        include_once "xhprof_lib/utils/xhprof_runs.php";
        $xhprof_runs = new XHProfRuns_Default();
        $run_id = $xhprof_runs->save_run($this->xhprofData, "xhprof_testing");
        echo "<a href='http://pear.kang.com/xhprof_html/index.php?run={$run_id}&source=xhprof_testing' target='_blank'>see xhprof result</a>";
    }
}
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
无法在这个位置找到: baidushare.htm
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码:点击我更换图片
推荐知识