Home php教程 php手册 facebook工具xhprof的安装与使用-分析php执行性能

facebook工具xhprof的安装与使用-分析php执行性能

Jun 13, 2016 am 11:31 AM
facebook http php download and use Bag Install tool performance implement Source code of URL

下载源码包的网址
 
http://pecl.php.net/package/xhprof



上面说了,每个版本适用的php版本。


规划(预先搞清楚思路)



一、这是一个php扩展的形式。我们安装gd2,curl都是php的扩展形式。只不过有的时候编译的时候就安装进去了。
像操作mysql数据库,也是一个mysql.so这样的扩展,安装了扩展,就能调用mysql_query()这些函数。
要操作oracle数据库,也有对应的oracle扩展加到php引擎中去。



现在要把xhprof扩展加到php中去。


很久没使用phpize安装扩展了。我自己忘得差不多了。于是重新去自己的博客找到以前写的文章复习一下。

http://www.cnblogs.com/wangtao_20/archive/2011/03/16/1986508.html




ps:我也在思考,这个东西怎么这么容易忘记。我只知道他的作用。但是完全不记得他的操作步骤。要注意的细节。

看我得想办法以通俗的方式来理解记住它。





二、php.ini需要进行配置的项


[xhprof]
extension=xhprof.so;
; directory used by default implementation of the iXHProfRuns
; interface (namely, the XHProfRuns_Default class) for storing
; XHProf runs.
;
;xhprof.output_dir=
;储存 XHProf 运行数据的默认目录
xhprof.output_dir=/tmp/xhprof



三、有了这个扩展后,就能在自己的php代码中调用这个扩展内置的函数来做性能监控了,像下面这样子


xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

................这里是要被监控的代码块


$data = xhprof_disable();

include_once "xhprof_lib/utils/xhprof_lib.php";  
include_once "xhprof_lib/utils/xhprof_runs.php";  
$objXhprofRun = new XHProfRuns_Default();//数据会保存在php.ini中xhprof.output_dir设置的目录去中
$run_id = $objXhprofRun->save_run($data, "test");


=====================================================

几个扩展函数如下




步骤实施


1、先找到我服务器上php的安装目录,phpize一般都是在安装目录中,如下:

/usr/local/php/bin/phpize

2、找出php-config在哪个目录(下面会用到),我的服务器在:

/usr/local/php/bin/php-config

目的:在下面进行编译的时候,会用到这个文件

./configure --with-php-config=/usr/local/php/bin/php-config



3、找到我服务器上php扩展在哪个目录,不确定的话,我觉得去php.ini中也能看到,如下


extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/" 这个路径感觉有点长。不用去改了php.ini中的设置,目前我觉得没必要(商业与时间成本,这部分还不是制约因素)。按原来的继续放扩展。




现在知道扩展目录为:/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/

也就是说,我要把xhprof的源码包解压到这个目录下去(解压后会生成一个新的文件夹)。


那我就要去这个目录下运行phpize(这样方便在这个目录下面生成configure文件),

phpize的特点:在a目录下运行phpize,就会在a目录下生成configure。











得到上面路径,shell命令实践



cd  /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/

解压下载到的xhprof压缩包(我不是通过wget下载的,我是把这个压缩直接通过ftp上传到no-debug-non-zts-20060613目录中去)。



tar zxf xhprof-0.9.3.tgz               #解压后,里面有个extension文件夹,进入里面去(目的是进入里面去运行phpize),解压后的目录结构如下

 




cd xhprof-0.9.3/extension/            #切换到这个扩展的源码目录去

在这个目录下面运行phpize,就会在extension目录下生成一个configure文件(这是phpize的机制)


/usr/local/php/bin/phpize
 
去看一下扩展目录,会发现在extension目录生成了一个configure文件。运行它


======================================


./configure --with-php-config=/usr/local/php/bin/php-config #用到了前面找到的php-config文件。

make && make install

make test
======================================




运行成功后,会提示生成的xhprof.so文件在哪个位置,提示信息:

Libraries have been installed in:
   /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/xhprof-0.9.3/extension/modules


这个目录下已经存在一个文件:
/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/xhprof.so

 

 



刚才生成了xhprof.so这个模块文件,现在要在php.ini中加载刚才生成xhprof.so模块才能生效:

[xhprof]
extension=xhprof.so;
; directory used by default implementation of the iXHProfRuns
; interface (namely, the XHProfRuns_Default class) for storing
; XHProf runs.
;
;xhprof.output_dir=
xhprof.output_dir=/tmp/xhprof






平滑重新加载php.ini文件:/usr/local/php/sbin/php-fpm reload
提示:

Reload service php-fpm  done

说明成功。

现在去phpinfo中看xhprof扩展是否加载成功了






安装作图工具(选填,可以后续再安装)

yum install -y graphviz





================================================

工具的使用实践

================================================

 

index.php中的代码:

 

error_reporting(-1);


xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
 
$begin_time = microtime_float();




///////////////统计执行速度代码
$end_time = microtime_float();

$exec_time  = $end_time-$begin_time;

//@save_stat($exec_time);

for($i=0;$i

for($j=0;$j
}

}



$data = xhprof_disable();

include_once "xhprof_lib/utils/xhprof_lib.php";//
include_once "xhprof_lib/utils/xhprof_runs.php";  
$objXhprofRun = new XHProfRuns_Default();//数据会保存在php.ini中xhprof.output_dir设置的目录去中
$run_id = $objXhprofRun->save_run($data, "test");
//第二个参数是定义文件名称。名称如果为"xhprof",则在xhprof.output_dir设置的目录生成的文件:522ab85f40229.xhprof.xhprof。
//格式为:"id标识.名称.xhprof",id标识就是$run_id得到的结果。





var_dump($run_id);

echo "http://www.xxxx.com/xhprof_html/index.php?run={$run_id}&source=test\n";//source的值必须是save_run中指定的名称。这个其实就是根据编号和名称才能定位到对应的文件"522ab85f40229.xhprof.xhprof"

//将这个地址输出来,是为了直接可以去查看分析结果。




function save_stat($time)
{
    static $call_count=1;
    $call_limit = 10;
    if(!$time) return ;
        $date = date("Y-m-d");//暂时按照天来生成文件。方便查阅
    $exec_stat_file = './exec_stat'.DIRECTORY_SEPARATOR."exec_stat_file-".$date.".txt";
    $fp = fopen($exec_stat_file,'ab');
    if(flock($fp,LOCK_EX))
    {
        $s = 'access:'.date("Y-m-d H:i:s").',execute time:'.$time.'s,request_url:http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."||".PHP_EOL;
        fwrite($fp,$s);
        flock($fp,LOCK_UN);
    }else{
        usleep(1000);
        if($call_count        {
            $call_count++;
            save_stat($time);
        }
    }

    @fcolse($fp);
    //var_dump($fp);

}
function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}
?>

 

加粗的黑体,就是分析用到的代码。

其中用到了源码包中的xhprof_lib中的代码,代码中"XHProfRuns_Default"这个类就是里面的。

 

 

生成了分析结果后,现在可以去使用源码包中提供的web版的工具查看了。

 

xhprof_html这个文件夹随便放到哪里,只要放到能够通过web访问的目录下即可,不过这个里面会用到xhprof_lib中的类,所以还是一起复制过去,跟源码包中保持一样的结构才好。xhprof_html与xhprof_lib要保持平行

 

我这里,这个工具的访问方式是:http://www.xxxx.com/xhprof_html/index.php?run=xxx&source=xxx\n

source的值必须是save_run中指定的名称。这个其实就是根据编号和名称才能定位到对应的文件"522ab85f40229.xhprof.xhprof"

访问看到的结果如下

 

 

 

 

图中,红色的表示最耗费性能的,黄色的其次。

 

 

只了解这么多了。具体的分析结果怎么看,在文件夹xhprof_html中有个docs目录,里面是说明文档。说了一些专用术语的定义

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

How to efficiently integrate Node.js or Python services under LAMP architecture? How to efficiently integrate Node.js or Python services under LAMP architecture? Apr 01, 2025 pm 02:48 PM

Many website developers face the problem of integrating Node.js or Python services under the LAMP architecture: the existing LAMP (Linux Apache MySQL PHP) architecture website needs...

How to configure apscheduler timing task as a service on macOS? How to configure apscheduler timing task as a service on macOS? Apr 01, 2025 pm 06:09 PM

Configure the apscheduler timing task as a service on macOS platform, if you want to configure the apscheduler timing task as a service, similar to ngin...

In LangChain, how do I use AgentExecutor to replace the disabled initialize_agent function? In LangChain, how do I use AgentExecutor to replace the disabled initialize_agent function? Apr 01, 2025 pm 04:18 PM

How to replace the disabled initialize_agent function in LangChain? In the LangChain library, initialize_agent...

Can Python parameter annotations use strings? Can Python parameter annotations use strings? Apr 01, 2025 pm 08:39 PM

Alternative usage of Python parameter annotations In Python programming, parameter annotations are a very useful function that can help developers better understand and use functions...

Can the Python interpreter be deleted in Linux system? Can the Python interpreter be deleted in Linux system? Apr 02, 2025 am 07:00 AM

Regarding the problem of removing the Python interpreter that comes with Linux systems, many Linux distributions will preinstall the Python interpreter when installed, and it does not use the package manager...

How to ensure high availability of MongoDB on Debian How to ensure high availability of MongoDB on Debian Apr 02, 2025 am 07:21 AM

This article describes how to build a highly available MongoDB database on a Debian system. We will explore multiple ways to ensure data security and services continue to operate. Key strategy: ReplicaSet: ReplicaSet: Use replicasets to achieve data redundancy and automatic failover. When a master node fails, the replica set will automatically elect a new master node to ensure the continuous availability of the service. Data backup and recovery: Regularly use the mongodump command to backup the database and formulate effective recovery strategies to deal with the risk of data loss. Monitoring and Alarms: Deploy monitoring tools (such as Prometheus, Grafana) to monitor the running status of MongoDB in real time, and

PostgreSQL monitoring method under Debian PostgreSQL monitoring method under Debian Apr 02, 2025 am 07:27 AM

This article introduces a variety of methods and tools to monitor PostgreSQL databases under the Debian system, helping you to fully grasp database performance monitoring. 1. Use PostgreSQL to build-in monitoring view PostgreSQL itself provides multiple views for monitoring database activities: pg_stat_activity: displays database activities in real time, including connections, queries, transactions and other information. pg_stat_replication: Monitors replication status, especially suitable for stream replication clusters. pg_stat_database: Provides database statistics, such as database size, transaction commit/rollback times and other key indicators. 2. Use log analysis tool pgBadg

See all articles