Home > Backend Development > PHP Tutorial > Tutorial on using XHGui to test PHP performance, xhguiphp performance tutorial_PHP tutorial

Tutorial on using XHGui to test PHP performance, xhguiphp performance tutorial_PHP tutorial

WBOY
Release: 2016-07-13 09:47:26
Original
847 people have browsed it

Tutorial on using XHGui to test PHP performance, xhguiphp performance tutorial

Profiling is a technology used to observe program performance, and is very suitable for discovering program bottlenecks or tight resources . Profiling can go deep into the program and show the performance of each part of the code in the request processing process; at the same time, it can also identify problematic requests (requests); for problematic requests, we can also determine where the performance problem occurs within the request. For PHP, we have a variety of Profiling tools. This article mainly focuses on XHGui, a very excellent tool. XHGui is built on XHProf (XHProf is released by Facebook), but adds better storage for analysis results and a better information acquisition interface. In this respect, XHGui is more like a brand new tool.

XHGui has gone through several iterations, but the current version provides a more beautiful user interface and uses MongoDB to store its profiling results. All these aspects are huge improvements compared to the previous version; because the previous version was more like a developer design, using files to save data, making the collected data very difficult to use. XHGui 2013 is a very comprehensive profiling tool for both managers 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 aspects of information that can be collected using this tool.

Step 1: Install dependencies

Because XHGui has some dependencies, our 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 and apply them to your own platform. For now, we need to install MongoDB, PHP, and have some ability to install the PECL extension.

First, we have to install MongoDB, there are some official installation tutorials here, you can find the details related to your system, but for now I will install it simply through APT:

aptitude install mongodb
Copy after login

The version of MongoDB obtained through this method may not be the latest because this product is updated really quickly. However, if you want to keep it at a very recent version, you can add the library provided by MongoDB to your package manager so that you get the latest one.


At the same time, we also need the Mongo driver for PHP. The version of the driver in the repository is a bit old, so for today's demonstration we will get it from Pecl. If there is no pecl command on your machine, you can install it with the following command:

aptitude install php-pear
Copy after login

Then, we add the MongoDB driver to PHP via the following command:

pecl install mongo
Copy after login

In order to complete the installation, we finally need to add a new line to the php.ini file. However, new versions of Ubuntu provide a new system for configuring PHP extensions that works more like Apache module installation - save all the configuration in one place and then create a symbolic link to launch the configuration. First, we create a file to hold the settings, although in this example we only need to add a new line to the settings to enable the extension. We save it in the file /etc/php5/mods-available/mongo.ini and add the following line:

php5enmod mongo

Copy after login
Use pecl again to install the xhprof extension. This program is currently only a beta version, so the installation command is as follows:

pecl install xhprof-beta
Copy after login

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

extension=xhprof.so

Copy after login
At this point, we can check that these modules are installed correctly by running the php -m command on the command line. Remember, don't forget to restart Apache so that the web interface can enable these extensions.

Install XHGui

XHGui itself is mainly composed of web pages, which provide a more friendly interface for the data collected by the XHProf extension. You can clone from the repository GitHub repo; you can also download the zip file directly and unzip it. After obtaining the program, make sure the cache directory has sufficient permissions so that the web server has permission to write the file. Finally, run the installation script:

php install.php
Copy after login

This is everything needed for program installation, and some dependent programs will be installed automatically; if an exception occurs, the installer will also prompt you.

I prefer to install XHGui in a virtual host; this requires permission from the .htaccess file and RUL rewriting needs to be enabled. Starting URL rewriting indicates that you need to start the mod_rewrite module, pass the following command:

a2enmod rewrite
Copy after login

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

201573145850225.png (300×210)

Start XHGui in the virtual host

此时,我们希望启动XHGui以便检验我们网站的性能。注意,性能测试最好在进行任何优化之前执行一次,以便检测优化的效果。最简单的方法是在虚拟主机中增加auto_prepend_file声明,如下图所示:

 <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>
Copy after login

一切就绪之后,你可以开始剖析网站的请求。XHGui只会剖析网站请求的1%,所以为了使XHGui获取有意义的数据,你需要让XHGui运行一段时间或者使用类似Apache Bench的测试工具批量提交一批请求。为什么在100个请求当中XHGui只会剖析一个?因为XHGui的设计初衷就是足够的轻巧以便在生产环境中使用,它不想对每一个请求产生额外的开销,1%的采样率已经能够为网站的总体流量提供较为清晰的概览。

满足数据

我使用测试虚拟机运行本文所有的示例,采用Joind.in API作为测试代码。为了产生一些流量,我将API测试案例运行了几遍。你也可以在一定负载的情况下收集数据,所以你可以在压力测试时使用XHGui,你甚至可以在上线站点中使用XHGui收集数据(听起来很疯狂,但是Facebook正式为了此应用才开发了该工具)。在向应用发送了一定的请求之后,重新访问XHGui,现在它就已经保存了一些数据:

201573145913073.png (300×210)

该图向我们展示了XHGui为我们分析的每一个请求,最新的请求排在第一位,并且为每一个请求展示了一些额外信息。这些信息包括:

  •     URL:请求所访问的URL
  •     Time:请求发起时间
  •     wtor: "Wall Time" –请求所经历的所有时间. 这是 "wall clock" time的简称,表示用户等待请求完成所有的时间
  •     cpu:花费在该请求上的CPU时间
  •     mu:该请求所消耗的内存
  •     pmu:请求处理过程中所消耗的最大内存

为了获取每一遍请求("run")更为详细的信息,你可以点击每一个请求你感兴趣的列。你可以点击URL以便获取该URL所有请求的详细信息。无论哪种方法,你都可以获取该请求更为详细的信息:

201573145938626.png (300×210)

201573145957832.png (300×210)

 这是一个非常长并且非常详细的页面,所以我引用了两个截图(如果展示所有的信息将需要5个截图)。上面一幅图的左边部分展示了该请求相关的一些信息,以便帮助你跟踪这些统计信息与哪些方面有关;右边的主要部分展示了最消耗时间的各部分以及在请求过程中每个函数调用所消耗的内存。在图的下方有一个主键以表明每一栏。

第二幅图展示了该请求每一个组成部分更为详细的信息。我们可以看到每一部分调用的次数以及时间消耗,还包括CPU和内存信息。无论是inclusive还是exclusive信息都做了详细的展示:exclusive表示仅仅是该方法调用所产生的消耗;inclusive不仅包括本函数所产生的消耗,还包括本函数调用的其他函数所产生的消耗。


XHGui另一个特性是“调用图”(Callgraph),“调用图”以生动的虚拟方式展示了时间是如何消耗的:

201573150016938.png (300×210)

 这很好的展示了函数调用的层次。最好的一点是,该图是可交互的,你可以拖拽以更好的查看连接;你还可以用鼠标滑过“圆环”(blob)以查看更多的信息。当你与它交互时,他会很好玩的弹回和移动,这不是一个非常重要的特性但却让我感觉非常好玩。

理解数据

拥有大量的统计数据非常重要,但是你很难知道从哪里下手。对于一个性能不如预期的页面采用如下步骤:首先,对每一个函数的exclusive CPU时间进行排序,查看最消耗时间的函数列表。分析这些耗时的函数调用并进行重构和优化。

一旦做出了修改,让剖析工具再次检验新版本的程序,测试性能的改进。XHGui内置了完美的工具以比较两次运行;点击详细信息页面右上角的“Compare this run"按钮即可。该按钮会向你展示该URL每一次测试的结果,从中选择一个你要比较的对象。对你想比较的对象,点击”compare“按钮,XHGui将会转向比较视图,如下图所示:

201573150035219.png (300×210)

The statistics table shows the main differences between the new version and the old version of statistics, including the actual number and percentage of each information change. The figure above shows that the request waiting time of the new version is only 8% of the old version. The statistical table shows the changes in each statistical information in detail, which we can often see on the "Details" page; you can sort any column to find the information you are interested in.

Once you have successfully refactored in one area, check the detail page to check the new version in action, and then select other areas for optimization. Try to sort by memory usage or exclusive wall time to pick and optimize functions that maximize the overall performance of your application. At the same time, don't forget to check the number of calls. A repeatedly called function can exponentially improve the performance of the program after optimization.

Optimization method

It’s hard to know how much you’ve improved before you quantify the results, which is why we often test an application before optimizing it – otherwise how do you know if you’ve really optimized it? We also need to think about how a set of real data should be represented, otherwise, we may be heading towards an impossible goal. A very useful method is to try your best to find the most suitable data structure and the smallest storage space that needs to be used. If you can't run a "Hello world" program in half a second in your preferred work environment, don't expect a web page built with the same tools to perform well.

The above description is not disrespectful to programming frameworks; programming frameworks exist because they are easy to use, support rapid development, and are easy to maintain. Compared with writing code by hand, the reduction in performance of the programming framework is the result of our compromise in all aspects. Using a programming framework for application development is a good way to get it online as soon as possible. When needed, you can use Profiling tools to analyze and improve the performance of the program. For example, many modules of Zend Framework 1 provide very powerful features, but are very slow; using Profiling tools you can identify the low-performance parts and replace them. All other frameworks have similar problems, and XHGui can show you where the problems are and check whether they have a quantifiable impact on your application.


Outside of your program, some other strategies may come in handy sooner or later to gain the upper hand:

  • Beware of not-dangerously-slow-but-related functions appearing on a page. If your page spends 50% of its time in a series of functions in the view helper that handle formatting points (I promise this is a hypothetical example), then you may want to look into refactoring the entire component.
  • Do less. Try removing features if performance is more important than them.
  • Beware of content generated in one request but not used in a particular view, or content that is unchanged but regenerated multiple times.
  • Good caching strategy. This will be another article about it, but consider using an OpCode cache in PHP (built-in as of PHP 5.5), adding a reverse proxy in front of your web server, and simply serving content that changes infrequently. Send appropriate cache headers.
  • Violent decoupling. If there's a special feature that's horribly resource-intensive, remove it from your web server. Perhaps it could be handled asynchronously, so your program could just add a message to the queue, or be moved to a separate server and accessed as a separate service model. Either way, separation will help reduce load on your web server while enabling efficient scaling.

XHGui is your friend

XHGui is easy to install, easy to use, and the output is so great that it can be presented at a board meeting. It identifies bugs in our apps and helps us confirm that our apps actually work (or don’t!). This may go through some repetitive processes, but regardless of whether you have used XHProf or XHGui before, I urge you to take the time to try it on your application. You will be surprised by what you find.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1026543.htmlTechArticleTutorial on using XHGui to test PHP performance, xhguiphp performance tutorial Profiling is a technology used to observe program performance. Very useful for discovering program bottlenecks or tight resources. Prof...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template