使用xhprof在开发环境中测试php性能
发布:smiling 来源: PHP粉丝网 添加日期:2015-04-04 15:48:42 浏览: 评论:0
XHProf是一个分层PHP性能分析工具,它报告函数级别的请求次数和各种指标,包括阻塞时间,CPU时间和内存使用情况,现在我们来聊聊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;
- } //开源软件:phpfensi.com
- }
- 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
从以下测试结果看出,耗时最多的居然是连接数据库,所以我们来尽量优化数据库.
Tags: xhprof环境 php性能
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)