生产环境使用xhprof测试php性能

以百分之一的概率产生测试数据,尽量不影响正式环境效率。

class XHProf {

    // private $XHProfPath = ‘xhprof/';
    private $XHProfPath = ‘/usr/local/apache/htdocs/xhprof/';
    private $applicationName = ‘sias_application';
    private $sampleSize = 100;
    private static $enabled = false;

    public function XHProf_Start() {
        if (mt_rand(1, $this->sampleSize) == 1) {
            include_once $this->XHProfPath . ‘xhprof_lib/utils/xhprof_lib.php';
            include_once $this->XHProfPath . ‘xhprof_lib/utils/xhprof_runs.php';
            xhprof_enable(XHPROF_FLAGS_NO_BUILTINS + XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

            self::$enabled = true;
        }
    }

    public function XHProf_End() {
        if (self::$enabled) {
            $XHProfData = xhprof_disable();

            $XHProfRuns = new XHProfRuns_Default();
            $XHProfRuns->save_run($XHProfData, $this->applicationName);
        }
    }

}

 

测试效果:


Overall Summary
Total Incl. Wall Time (microsec): 48,162 microsecs
Total Incl. CPU (microsecs): 32,994 microsecs
Total Incl. MemUse (bytes): 2,773,464 bytes
Total Incl. PeakMemUse (bytes): 2,867,664 bytes
Number of Function Calls: 749

小Q截图-20140213111829

小Q截图-20140213111841

耗时最多的居然是连接数据库、、、

About 智足者富

http://chenpeng.info

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>