Use XHGui to test PHP performance tutorial, xhguiphp performance tutorial

Source: Internet
Author: User
Tags install mongodb mongodb driver mongodb version

Use XHGui to test PHP performance tutorial, xhguiphp performance tutorial

Profiling is a technique used to observe program performance and is very suitable for discovering program bottlenecks or tight resources. Profiling can go deep into the program to show the performance of each part of the code in the request processing process. It can also identify problematic requests. For problematic requests, we can also determine the internal location of the request where the performance problem occurs. For PHP, We have a variety of Profiling tools. This article focuses on the XHGui, a very good tool. XHGui is built on XHProf (XHProf was released by Facebook), but it adds better storage for analysis results and better information retrieval interfaces. In this regard, XHGui is more like a brand new tool.

XHGui has experienced several iterations, but the current version provides a more beautiful user interface and uses MongoDB to store its analysis results. Compared with the previous version, all these aspects are greatly improved, because the previous version is more like a developer's design and uses files to store data, making the collected data very difficult to use. XHGui 2013 is a comprehensive Profiling tool for administrators and developers. At the same time, XHGui 2013 is designed to be lightweight enough to run in a production environment.

This article will demonstrate the installation of the program step by step and show you all the information that can be collected using this tool.

Step 1: Install Dependencies

Because XHGui has some dependencies, the first step is to solve this problem. All the tutorials below are based on the Ubuntu 13.04 platform. Of course, you should be able to adapt them to your own platform. Currently, we need to install MongoDB and PHP, and have the ability to install PECL extensions.

First, we want to install MongoDB. Here are some official installation tutorials. You can find details related to your system, but now I will install it through APT:
 

aptitude install mongodb

The MongoDB version obtained through this method may not be the latest, because the update speed of this product is really fast. However, if you want to maintain a new version, you can add the library provided by MongoDB to your package manager so that you can get the latest version.


At the same time, we also need the Mongo driver for PHP. The driver version is a little old in the repository. For today's demonstration, we will get it from Pecl. If your machine does not have the pecl command, you can install it using the following command:
 

aptitude install php-pear

Then, run the following command to add the MongoDB driver to PHP:
 

pecl install mongo

To complete the installation, add a new line in the php. ini file. However, the new version of Ubuntu provides a new system for configuring PHP extensions, which is more like installing the Apache module-saving all the configurations in one place, create a symbolic link to start the configuration. First, create a file to save the settings, although in this example, you only need to add a line in the settings to start the extension. We saved it to the/etc/php5/mod-available/mongo. ini file and added the following line:
 

php5enmod mongo
Use pecl again to install the xhprof extension. The program is only in beta version. Therefore, the installation command is as follows:
 
pecl install xhprof-beta

The command line again prompts us to add a new line in php. ini. We use the same method as above to create the file/etc/php5/mod-available/xhprof. ini and add the following content in it:
 

extension=xhprof.so
In this case, we can check whether these modules are correctly installed by running the php-m command on the command line. Remember, do not forget to restart Apache so that web interfaces can enable these extensions.

Install XHGui

XHGui is mainly composed of web pages. It provides a more friendly interface for XHProf to expand the collected data. You can clone the code library from GitHub repo, or directly download the zip file and decompress it. After obtaining the program, make sure that the cache directory has sufficient permissions so that the web server has the permission to write files. Finally, run the installation script:
 

php install.php

This is everything required for program installation, and some dependencies will be automatically installed. If an exception occurs, the installer will prompt you.

I prefer to install XHGui in a virtual host. This requires the. htaccess file to allow and The RUL rewrite must be started. Starting URL rewriting indicates that the mod_rewrite module needs to be started. Run the following command:
 

a2enmod rewrite

(Do not forget to restart Apache ). If everything goes well, you can normally access the XHGui URL and see the following content:

Start XHGui on the VM

In this case, we want to start XHGui to verify the performance of our website. Note: It is best to perform a performance test once before any optimization to detect the effect of the optimization. The simplest way is to add the auto_prepend_file declaration to the VM, as shown in:

 <VirtualHost *:80>  ServerName example.local   DocumentRoot /var/www/example/htdocs/  php_admin_value auto_prepend_file /var/www/xhgui/external/header.php   <Directory /var/www/example/htdocs/>    Options FollowSymLinks Indexes    AllowOverride All  </Directory> </VirtualHost>

After everything is ready, you can start to analyze website requests. XHGui only analyzes 1% of website requests. Therefore, in order for XHGui to obtain meaningful data, you need to allow XHGui to run for a period of time or use a test tool similar to Apache runtime to submit a batch of requests in batches. Why does XHGui only parse one of the 100 requests? Because XHGui is designed to be lightweight enough for use in the production environment, it does not want to incur additional overhead for every request, the sampling rate of 1% can provide a clear overview of the overall traffic of the website.

Meeting data requirements

I use the Test Virtual Machine to run all the examples in this article, using the Joind. in API as the test code. To generate some traffic, I ran the API test case several times. You can also collect data under certain loads, so you can use XHGui during stress testing, and you can even use XHGui to collect data in online sites (it sounds crazy, but Facebook officially developed this tool for this application ). After sending a certain request to the application, access the XHGui again, and now it has saved some data:

This figure shows each request analyzed by XHGui. The latest request comes first and some additional information is displayed for each request. The information includes:

  • URL: the requested URL
  • Time: Request initiation Time
  • Wtor: "Wall Time"-All time the request has taken. This is short for "wall clock" Time, indicating that the user waits for the request to complete all the time
  • Cpu: the CPU time spent on the request
  • Mu: memory consumed by the request
  • Pmu: maximum memory consumed during request processing

To obtain more detailed information about each request ("run"), you can click the columns you are interested in for each request. You can click a URL to obtain detailed information about all requests of the URL. Either way, you can obtain more detailed information about the request:

This is a very long and very detailed page, so I referenced two (five if all the information is displayed ). The left part of the above figure shows the request-related information to help you track the statistics; the main part on the right shows the most time-consuming parts and the memory consumed by each function call during the request process. There is a primary key below the graph to indicate each column.

The second figure shows more detailed information about each component of the request. We can see the number of calls and time consumption of each part, as well as the CPU and memory information. No matter whether it is implicit or exclusive, the details are displayed in detail: exclusive indicates that this method is only consumed by calling the method. Lateral sive not only includes the consumption of this function, it also includes the consumption of other functions called by this function.


Another feature of XHGui is "Call Graph". The "Call Graph" shows how time is consumed in a vivid and virtual way:

This shows the hierarchy of function calls. The best thing is that the graph is interactive. You can drag it to view the connection better. You can also move the mouse over the "circle" (blob) to view more information. When you interact with it, it will be fun to play back and move. This is not a very important feature, but it makes me feel very fun.

Understanding data

It is very important to have a large amount of statistics, but it is hard to know where to start. Take the following steps for a page with poor performance: first, sort the exclusive CPU time of each function to view the list of functions that consume the most time. Analyze these time-consuming function calls and refactor and optimize them.

Once the changes are made, let the profiling tool re-examine the new version of the program and test the performance improvement. XHGui has a built-in perfect tool to Compare two runs. Click the "Compare this run" button in the upper-right corner of the details page. This button will show you the results of each test of the URL and select an object to be compared. For the object you want to compare, click the "compare" button. XHGui will switch to the comparison view, as shown in:

The statistical table shows the main differences between the new and old statistical information versions, including the actual number and percentage of each information change. It is shown that the request wait time for the new version is only 8% of the old version. The statistical table details the changes in each statistical information, which can be seen frequently on the "details" page; you can sort any column to find the information you are interested in.

Once you have successfully reconstructed a new version, view the details page to check the actual effect of the new version, and then select other aspects for optimization. Try to sort memory usage or exclusive wall time to select a function that can maximize the overall performance of the Application for optimization. At the same time, do not forget to check the number of calls. After a function that is repeatedly called is optimized, the performance of the program can be doubled.

Optimization Method

It is difficult for you to know how much you have improved before quantifying the results. That is why we often detect an application before optimization-otherwise, how do you know if you have actually optimized it? We also need to think about how to express a set of real data. Otherwise, we may move toward an impossible target. A useful method is to try your best to find the most suitable data structure and the minimum storage space you need. If you cannot run a "Hello world" program in half a second in a work environment that you are good at, don't expect that the web pages built with the same tool can do well.

The above description is not disrespectful to the programming framework. The programming framework exists because it is convenient to use, supports rapid development, and is easy to maintain. Compared to writing code manually, the performance reduction of the programming framework is the result of our comprehensive reduction in all aspects. Using a programming framework for application development is a good way to get online as soon as possible. When necessary, you can use the Profiling tool to analyze and improve program performance. For example, many modules of Zend Framework 1 can provide poor and powerful features, but they can also be very low. The Profiling tool can be used to identify and replace them. All other frameworks have similar problems. XHGui can show you where the problem is located and check whether they have quantifiable effects on your program.


In addition to your program, some other strategies may be useful sooner or later to gain the upper hand:

  • Beware of non-dangerous slow Association functions (not-dangerously-slow-but-related functions) appearing on a page. If your page spends 50% of its time in a series of functions in view helper for formatting highlights (I promise this is a hypothetical example), you may want to research and refactor the entire component.
  • Do less. Try removing features if performance is more important than them.
  • Be careful with the content generated in a request but not used in a special view, or the content that has not been changed but is repeatedly regenerated.
  • A good Cache Policy. This will be another article about it, but consider using an OpCode cache in PHP (built-in from PHP 5.5) and adding a reverse proxy in front of your web server, simply send appropriate cache headers for infrequently changed content.
  • Brute force decoupling. If there is a special feature with terrible resource shortage, remove it from your web server. Maybe it can be processed asynchronously, so your program can add only one message to the queue, or move it to another separate server and access it as a separate service model. In either way, splitting helps reduce the load on your web server while enabling effective scaling.

XHGui is your friend

XHGui is easy to install and used, so it can be presented at board meetings with great output. It can identify errors in our application and help us to confirm that the application actually works (or does not !). This may go through some repetitive processes. However, whether or not you have used XHProf or XHGui before, I suggest you try it on your application, you will be surprised by your findings.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.