


How the extension Xhprof in php analyzes the performance of the project
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
Install the extension
Configure the extension
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
3.2 Detection environment
Enter the compiled folder, refer to the command
cd xhprof/extension/
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
The return result is as follows
Configuring for: PHP Api Version: 20160303 Zend Module Api No: 20160303 Zend Extension Api No: 320160303
3.3 Compile and install
Generate Makefile to prepare for the next step of compilation
./configure
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
Start compiling and install
make && make install
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/
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
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
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
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 =
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
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/
Edit the configuration file through the vim editor. The reference command is as follows
vim /usr/local/etc/php/7.1/php.ini
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
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
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
So the restart PHP-FPM command constructed by the author is as follows:
brew services restart php@7.1
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)
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
After the command is executed, the return result of successful extension installation will display xhprof, as shown in the figure below
##5. TestAfter the above operation, the author has successfully installed and configured. Now I need to use PHP code to verify the analysis effect of xhprof5.1 Create virtual HostFirst 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 directoryCreate the project root directory, the reference command is as follows
mkdir -p /Users/song/mycode/work/test
cp -r xhprof/xhprof_html /Users/song/mycode/work/test/ cp -r xhprof/xhprof_lib /Users/song/mycode/work/test/
/usr/local/etc/nginx/nginx.conf
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; } }
/etc/hosts Add a line of parsing records to the file. The record content is as follows:
127.0.0.1 test.localhost
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:
vim /Users/song/mycode/work/test/test.php
<?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 ''; } } //定义测试方法 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";
http://test.localhost/xhprof/test.php
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
php performance monitoring extension xhprof
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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 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 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.

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 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 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 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 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.
