Table of Contents
1. Background
2. Operation steps
3. Installation
3.1 Download the source code
3.2 Detection environment
3.3 Compile and install
4. Configuration
4.1 Find out the location of the configuration file
4.2 Modify the configuration
4.3 Restart to take effect
4.4 Verify the installation
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

Aug 29, 2018 am 10:52 AM
php optimization Performance analysis

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!

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 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)

Hot Topics

Java Tutorial
1673
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

See all articles