使用XHProf优化分析PHP程序性能

WBOY
Freigeben: 2016-06-20 13:04:20
Original
1064 Leute haben es durchsucht

使用XHProf优化分析PHP程序性能

XHProf 是 FaceBook 开发的一个函数级别的 PHP 分层分析器。

数据收集部分是一个基于 C 的 PHP 扩展,分析报告是一系列基于 PHP 的 HTML 导航页面。

XHProf 能统计每个函数的调用次数、内存使用、CPU占用等多项重要的数据。

并且 XHProf 还能比较两个统计样本,或从多个数据样本中汇总结果。

XHProf 是分析 PHP 程序执行效率的利器,能让我们得到更底层的的分析数据。

安装 XHProf

XHProf 目前的最新版本是 0.92,你可以从它的主页(http://pecl.php.net/package/xhprof)下载它的最新版本。

例如,我的 Web 目录是 /data/scutephp.com,那么我可以通过下面的命令来安装 XHProf 。
查看源代码

<span style="font-size: 14px;">wget http://pecl.php.net/get/xhprof-0.9.2.tgz</span><p>tar zxf xhprof-0.9.2.tgz<br />cd xhprof-0.9.2<br />cp -r xhprof_html xhprof_lib /data/scutephp.com/<br />cd extension<br />phpize<br />./configure<br />make<br />make install<span style="font-size: 14px;">
Nach dem Login kopieren

当然,我们还需要修改 php.ini 来确保 XHProf 的正常运行。

我们需要先建立一个目录用来存储分析数据文件,例如 /data/scutephp.com/xhprof/。
查看源代码

[xhprof]<br />extension=xhprof.so<br />xhprof.output_dir=/data/scutephp.com/xhprof/
Nach dem Login kopieren


使用 XHProf

使用 XHProf 非常简单,我们只需要修改少许代码即可实现。

例如,我们需要分析代码执行时关于 CPU 和内存的数据。
查看源代码
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);<br /> // Test Code Start<br />   …………<br />// Test Code End<br />   <br />$xhprofData = xhprof_disable();<br />    <br />include_once "/data/scutephp.com/xhprof_lib/utils/xhprof_lib.php";<br />include_once "/data/scutephp.com/xhprof_lib/utils/xhprof_runs.php";<br /> <br />$xhprofRuns = new XHProfRuns_Default();<br />$xhprofRuns->save_run($xhprofData, "xhprof");
Nach dem Login kopieren


这样,当我们运行程序之后,会在 /data/scutephp.com/xhprof/ 之中生成分析数据文件。

文件名类似于 4b4c239a86593.xhprof ,我们可以通过改变 save_run 的参数,来改变文件后缀。

由于分析可能会影响响应速度,通常我们会加上一个随机数,随机取样,而不是分析所有用户请求的执行过程。
查看源代码

 $randKey = mt_rand(1, 10000);<br /> <br />  if ($randKey == 1)<br />{<br />      xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);<br />}<br />  <br />  // Test Code Start<br />   …………<br />// Test Code End<br />  <br /> if ($randKey == 1)<br /> {<br />       $xhprofData = xhprof_disable();<br />   <br />       include_once "/data/scutephp.com/xhprof_lib/utils/xhprof_lib.php";<br />       include_once "/data/scutephp.com/xhprof_lib/utils/xhprof_runs.php";<br />   <br />        $xhprofRuns = new XHProfRuns_Default();<br />        $xhprofRuns->save_run($xhprofData, "xhprof");<br />}
Nach dem Login kopieren

查看 XHProf 分析数据

例如,我的域名是 scutephp.com,现在我们就可以通过 web 来查看详细的分析数据了。


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!