程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP代碼性能的分析方法

PHP代碼性能的分析方法

編輯:關於PHP編程

     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>";
        }
    }

    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved