1

在生产环境中使用php性能测试工具xhprof

 2 years ago
source link: https://blogread.cn/it/article/323?f=hot1
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

在生产环境中使用php性能测试工具xhprof

浏览:3308次  出处信息

    xhprof是facebook开源出来的一个php性能测试工具,也可以称之为profile工具,这个词不知道怎么翻译才比较达意。跟之前一直使用的xdebug相比,有很多类似之处。以前对xdebug有一些记录还可以供参考,但是它的缺点是对性能影响太大,即便是开启了profiler_enable_trigger参数,用在生产环境中也是惨不忍睹,cpu立刻就飙到high。

    而xhprof就显得很轻量,是否记录profile可以由程序控制,因此,用在生产环境中也就成为一种可能。在它的文档上可以看到这样一种用法:

    以万分之一的几率启用xhprof,平时悄悄的不打枪。

以下是代码片段:
if (mt_rand(1, 10000) == 1) {
 xhprof_enable(XHPROF_FLAGS_MEMORY);
 $xhprof_on = true;

    在程序结尾处调用方法保存profile

以下是代码片段:
if ($xhprof_on) {
 // stop profiler
 $xhprof_data = xhprof_disable();

// save $xhprof_data somewhere (say a central DB)
 ...

    也可以用register_shutdown_function方法指定在程序结束时保存xhprof信息,这样就免去了结尾处判断,给个改写的不完整例子:

以下是代码片段:
if (mt_rand(1, 10000) == 1) {
 xhprof_enable(XHPROF_FLAGS_MEMORY);
 register_shutdown_function(create_funcion(\’\’, "$xhprof_data = xhprof_disable(); save $xhprof_data;"));

    至于日志,我暂时用的是最土的文件形式保存,定期清除即可。

    BTW:xhprof生成的图形方式profile真是酷毙了,哪段代码成为瓶颈,一目了然。

建议继续学习:

QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK