


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

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

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 originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software