xhprof 简单学习试用

使用centos 7 ,使用内置的php 以及结合php-fpm

安装

yum install -y nginx php-fpm xhprof

修改配置添加xhprof支持

  • php.ini
    安装的位置在/etc/php.ini
 
[xhprof]
extension=xhprof.so
  • nginx 配置
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    server {
        listen       8090 default_server;
        server_name  _;
        root         /usr/share/xhprof/xhprof_html;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        location / {
            index index.php index.html index.htm;
        }
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
    server {
        listen       8080 default_server;
        server_name  _;
        root         /var/www/;
        include /etc/nginx/default.d/*.conf;
        location / {
            index index.php index.html index.htm;
        }
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}
  • 测试代码
<?php
xhprof_enable();
// 以下为要分析的代码.
function showSleep($num = 5)
{
    $i = 0;
    while ($i++ < $num) {
        sleep(1);
    }
}
function genData($num = 100)
{
    $data = [];
    $i = 0;
    while ($i++ < $num) {
        $data[] = $i * $i;
    }
    sleep(4);
    return $data;
}
showSleep(6);
genData(100);
// 以上为要分析的代码.
// 获取分析结果.
$xhprofData = xhprof_disable();
// 引入xhprof_lib.
$xhprofRoot = '/usr/share/xhprof/';
include_once $xhprofRoot . "/xhprof_lib/utils/xhprof_lib.php";
include_once $xhprofRoot . "/xhprof_lib/utils/xhprof_runs.php";
// 保存结果.
$xhprofRuns = new XHProfRuns_Default();
$runId = $xhprofRuns->save_run($xhprofData, "xhprof_show");
// 输出报告地址.
echo 'http://host:port/index.php?run=' . $runId . '&source=xhprof_show';

效果

 

 

说明

自己对于php的内部机制不是很了解,以上只是一个简单的记录

参考资料

https://www.php.net/xhprof

posted on   荣锋亮  阅读(275)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2019-06-30 Omnibus-ctl: What is it and what can it do for you?
2019-06-30 omnibus-gitlab 架构学习

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示