Home > Backend Development > PHP Tutorial > How the extension Xhprof in php analyzes the performance of the project

How the extension Xhprof in php analyzes the performance of the project

不言
Release: 2023-04-03 22:38:02
Original
1807 people have browsed it

The content of this article is about how the extension Xhprof in PHP analyzes the performance of the project. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Background

The project is about to go online. I want to use some tools to analyze the stability and efficiency of the code. I remembered the xhprof extension I used in the previous team; because I changed to a new computer, Therefore, this extension needs to be recompiled. The installation and actual troubleshooting process are now fully recorded to facilitate your review and help more readers.

2. Operation steps

  1. Install the extension

  2. Configure the extension

  3. Test analysis

3. Installation

xhprof extension PHP does not come with it. The author needs to install it separately before it can be used after installation. The author uses the source code installation method here. The installation process is as follows

3.1 Download the source code

xhprof is already relatively old on PHP’s PECL official version. The author’s PHP version is PHP7.1. Therefore, you need to download the newer version of xhprof on GitHub. Source code, refer to the following command

git clone https://github.com/longxinH/xhprof
Copy after login

3.2 Detection environment

Enter the compiled folder, refer to the command

cd xhprof/extension/
Copy after login

Now I need to compile the source code, you can use phpze before compilation To detect the PHP environment, the reference command is as follows:

phpize
Copy after login

The return result is as follows

Configuring for:
PHP Api Version:         20160303
Zend Module Api No:      20160303
Zend Extension Api No:   320160303
Copy after login

3.3 Compile and install

Generate Makefile to prepare for the next step of compilation

./configure
Copy after login

The return result is as follows

creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h
config.status: config.h is unchanged
Copy after login

Start compiling and install

make && make install
Copy after login

The return result is as follows

Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/local/Cellar/php@7.1/7.1.19/pecl/20160303/
Copy after login

You can see from the return information that the installation has been completed and the extension is displayed The location of file storage

4. Configuration

After compiling and installing the source code, the author also needs to perform some simple configurations on the PHP configuration folder and xhprof. The operation process is as follows

4.1 Find out the location of the configuration file

To modify the configuration of PHP, you first need to know where the configuration file is. Here you can use the PHP command to view the location of the configuration file. The reference command is as follows:

php --ini
Copy after login

After executing the command, the returned results are as follows

Configuration File (php.ini) Path: /usr/local/etc/php/7.1
Loaded Configuration File:         /usr/local/etc/php/7.1/php.ini
Scan for additional .ini files in: /usr/local/etc/php/7.1/conf.d
Additional .ini files parsed:      /usr/local/etc/php/7.1/conf.d/ext-opcache.ini
Copy after login

In the returned results, you can see the paths of multiple configuration files. What the author needs is the second filephp.ini

Check the storage location of the extension directory. The reference command is as follows

cat /usr/local/etc/php/7.1/php.ini | grep extension_dir
Copy after login

The return result is as follows

extension_dir = "/usr/local/lib/php/pecl/20160303"
; extension_dir = "ext"
; Be sure to appropriately set the extension_dir directive.
;sqlite3.extension_dir =
Copy after login

4.2 Modify the configuration

From the returned results, you can see The location of the extension's storage directory is as follows

/usr/local/lib/php/pecl/20160303
Copy after login

Now you need to copy the xhprof extension you just compiled to this directory. The reference command is as follows

cp /usr/local/Cellar/php@7.1/7.1.19/pecl/20160303/xhprof.so  /usr/local/Cellar/php@7.1/7.1.19/pecl/20160303/
Copy after login

Edit the configuration file through the vim editor. The reference command is as follows

vim  /usr/local/etc/php/7.1/php.ini
Copy after login

Add xhprof configuration at the end of the configuration file, and customize a source file generated by xhprof to save the reference configuration as follows

[xhprof]
extension=xhprof.so
xhprof.output_dir=/data/www/xhprof/save_output_dir
Copy after login

4.3 Restart to take effect

After saving , the author restarts php-fpm to make its configuration take effect. The restart command can be viewed through the brew command. The reference command is as follows:

brew info php@7.1
Copy after login

After the command is executed, the following information can be seen in the returned information

To have launchd start php@7.1 now and restart at login:
  brew services start php@7.1
Or, if you don't want/need a background service you can just run:
  php-fpm
Copy after login

So the restart PHP-FPM command constructed by the author is as follows:

brew services restart php@7.1
Copy after login

After the restart is completed, the return result is as follows

Stopping `php@7.1`... (might take a while)
==> Successfully stopped `php@7.1` (label: homebrew.mxcl.php@7.1)
==> Successfully started `php@7.1` (label: homebrew.mxcl.php@7.1)
Copy after login

4.4 Verify the installation

Now verify whether the xhprof extension has been installed Completed, the reference command is as follows

php -m | grep xhprof
Copy after login

After the command is executed, the return result of successful extension installation will display xhprof, as shown in the figure below

How the extension Xhprof in php analyzes the performance of the project

##5. Test

After the above operation, the author has successfully installed and configured. Now I need to use PHP code to verify the analysis effect of xhprof

5.1 Create virtual Host

First create a virtual host so that users can access it through a browser. To create a virtual host, you need to have a root directory and edit the nginx configuration file. The specific operations are as follows:

5.1. 1 Create the project directory

Create the project root directory, the reference command is as follows

mkdir -p /Users/song/mycode/work/test
Copy after login
After the creation is successful, the author needs to copy part of the code pulled down by git to the project root directory, the reference command is as follows

cp -r xhprof/xhprof_html /Users/song/mycode/work/test/
cp -r xhprof/xhprof_lib /Users/song/mycode/work/test/
Copy after login
5.1.2 Edit the configuration file

Add the configuration file, refer to the command

/usr/local/etc/nginx/nginx.conf
Copy after login
Add the configuration file as follows

    server {
        listen       80;
        server_name  test.localhost;

        root   /Users/song/mycode/work/test;
        index  index.html index.htm index.php;
        
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }


        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;
        }

    }
Copy after login
In

/etc/hosts Add a line of parsing records to the file. The record content is as follows:

127.0.0.1 test.localhost
Copy after login
5.2 Create a new test code

It is already in the

examples folder of the git warehouse. A demo code, but the comments of this code are all in English, and the formatting method is not easy for the author to understand, so I re-edited this file. The reference steps are as follows:

Use vim to create a new PHP file

vim /Users/song/mycode/work/test/test.php
Copy after login
Add the following code to the file

<?php //加载所需文件
include_once "./xhprof_lib/utils/xhprof_lib.php";
include_once "./xhprof_lib/utils/xhprof_runs.php";

//随意定义一个函数
function test($max)
{
    for ($idx = 0; $idx < $max; $idx++) {
        echo &#39;&#39;;
    }
}

//定义测试方法
function a()
{
    test(rand(1000,5000));
}

//开始分析
xhprof_enable();

//需要分析的函数
a();

//结束分析
$xhprof_data = xhprof_disable();
//实例化xhprof类
$xhprof_runs = new XHProfRuns_Default();
//获取当前当前页面分析结果
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");

echo "\nhttp://test.localhost/xhprof/xhprof_html/index.php?run=$run_id&source=xhprof_foo\n";
Copy after login
After saving the code, access the corresponding URL address through the browser. The URL address is as follows

http://test.localhost/xhprof/test.php
Copy after login
5.3 Result Analysis

The result after running is as shown below

How the extension Xhprof in php analyzes the performance of the project

You can see a URL address on the page. After copying and opening this URL address, you can see The analysis results of this code are as shown in the figure below

How the extension Xhprof in php analyzes the performance of the project

There is a list on the page that shows the time consumed by each method. If you feel that the list is not clear enough, click ## on the page. #View Full Callgraph The link can directly generate a picture, as shown in the picture below

How the extension Xhprof in php analyzes the performance of the project

You can clearly see the execution time in the picture All are consumed in the test method, so the author can carry out targeted optimization for this method.

Related recommendations:

php performance monitoring extension xhprof

##Use xhprof (PHP) extension for WEB performance analysis

The above is the detailed content of How the extension Xhprof in php analyzes the performance of the project. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template