Home Backend Development PHP Tutorial Detailed explanation based on using PHP performance testing tool xhprof in production environment Game performance testing tool Web performance testing tool Network performance testing tool

Detailed explanation based on using PHP performance testing tool xhprof in production environment Game performance testing tool Web performance testing tool Network performance testing tool

Jul 29, 2016 am 08:48 AM

This article is a detailed analysis and introduction to the use of the PHP performance testing tool xhprof in a production environment. Friends who need it can refer to it

xhprof is a PHP performance testing tool open sourced by Facebook. It can also be called a profile tool. , I don’t know how to translate this word to express its meaning. Compared with the xdebug I have been using before, there are many similarities. In the past, there were some records of xdebug that can be used for reference, but its disadvantage is that it has a great impact on performance. Even if the profiler_enable_trigger parameter is turned on, it is terrible when used in a production environment, and the CPU will immediately reach high.
xhprof is very lightweight, and whether to record profiles can be controlled by the program. Therefore, it becomes possible to use it in a production environment. You can see such a usage in its documentation:
Enable xhprof with a one-in-10,000 chance, and usually do not shoot quietly.

Copy the codeThe code is as follows:


if (mt_rand(1, 10000) == 1) {
xhprof_enable(XHPROF_FLAGS_MEMORY);
$xhprof_on = true;
}


At the end of the program Call the method to save the profile

Copy the codeThe code is as follows:


if ($xhprof_on) {
// stop profiler
$xhprof_data = xhprof_disable();
// save $xhprof_data somewhere (say a central DB)
...
}


You can also use the register_shutdown_function method to specify to save the xhprof information at the end of the program, thus eliminating the need to judge at the end. Here is an incomplete example of rewriting:

Copy code The code is as follows:


if (mt_rand(1, 10000) == 1) {
data;")) ;
}

As for the logs, I temporarily use the most primitive file format to save them, and just clear them regularly.
BTW: The graphical profile generated by xhprof is really cool. It is clear at a glance which piece of code has become the bottleneck.
by phpe: Here is an example:

Detailed explanation based on using PHP performance testing tool xhprof in production environment Game performance testing tool Web performance testing tool Network performance testing tool Ideas about improving the use of xhprof
Since xhprof was used in the production environment last year, it has brought a lot of convenience to program debugging and performance optimization in the production environment. However, there are still some details that need to be improved during use.

Problem
The profile log of xhprof is saved directly on the production server in the form of a file. It needs to be cleaned regularly, or collected and moved to a tool machine for viewing logs.
Since the profile generated by xhprof is a large array, standard php serialize is used when saving to the file. The log file is relatively large, and it is easy to occupy a lot of server disk space if you are not careful.
When viewing the log list, it is difficult to click on each one to view them.
In response to these questions, I have some small ideas.

Log storage
Deploy a central log server and use Facebook's scribe to collect logs. The xhprof logs generated by the server in the production environment are written to the scribe client, and the client automatically synchronizes them to scribe on the central log server, without occupying local storage space. The changes in the code are also relatively small. Just implement an XhprofRuns class based on the iXHProfRuns interface and adjust the storage method of the save_run method.

Change the serialization method
xhprof defaults to processing the profile information using PHP’s native serialization method and then saving it. In the past two days, I compared the performance and occupancy of igbinary vs serialize vs json_encode. In this test Igbinary has certain advantages in all aspects, especially the storage space occupied will be greatly reduced, so I only need to change the serialization method to igbinary_serialize to get improvement.

Optimize list display
I am tired of looking at the big pictures of profile logs one by one. It is time-consuming and labor-intensive and not targeted. So what I am doing now is to directly output the overall execution time of the first 1000 logs in the list of profile logs to the list, and mark the logs with too long execution times in red boldface. After making this small change, when I want to check the operation status, I just click on the red links in the log list to take a look, which really saves time and effort.
How to get execution time from xhprof log file? The simple code is as follows

Copy the codeThe code is as follows:

/**
* Obtain the execution time from the xhprof log
*
* @param string $log The file path of the xhprof log
* @return int Execution time
*/
function getSpentTime($log) {
$profile = unserialize(file_get_contents($log));
return $profile['main()']['wt'] / 1000;
}

The above has introduced a detailed explanation based on the use of PHP performance testing tool xhprof in a production environment, including xhprof and performance testing tools. I hope it will be helpful to friends who are interested in PHP tutorials.

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

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

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

See all articles