


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
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;")) ;
}
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:
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;
}

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











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,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

RESTAPI design principles include resource definition, URI design, HTTP method usage, status code usage, version control, and HATEOAS. 1. Resources should be represented by nouns and maintained at a hierarchy. 2. HTTP methods should conform to their semantics, such as GET is used to obtain resources. 3. The status code should be used correctly, such as 404 means that the resource does not exist. 4. Version control can be implemented through URI or header. 5. HATEOAS boots client operations through links in response.

In PHP, exception handling is achieved through the try, catch, finally, and throw keywords. 1) The try block surrounds the code that may throw exceptions; 2) The catch block handles exceptions; 3) Finally block ensures that the code is always executed; 4) throw is used to manually throw exceptions. These mechanisms help improve the robustness and maintainability of your code.

The main function of anonymous classes in PHP is to create one-time objects. 1. Anonymous classes allow classes without names to be directly defined in the code, which is suitable for temporary requirements. 2. They can inherit classes or implement interfaces to increase flexibility. 3. Pay attention to performance and code readability when using it, and avoid repeatedly defining the same anonymous classes.

In PHP, the difference between include, require, include_once, require_once is: 1) include generates a warning and continues to execute, 2) require generates a fatal error and stops execution, 3) include_once and require_once prevent repeated inclusions. The choice of these functions depends on the importance of the file and whether it is necessary to prevent duplicate inclusion. Rational use can improve the readability and maintainability of the code.

There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

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.
