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

原文地址:https://www.cnblogs.com/rongfengliang/p/13215955.html