Use XHProf and XHGui in Linux to analyze the PHP running performance

Source: Internet
Author: User
Tags mongodb server composer install
This article describes how to use XHProf and XHGui in Linux to analyze the PHP running performance. this solution supports Apache and Nginx servers and multiple database environments. For more information, see What is performance analysis?
Performance analysis is to measure the relative performance of an application at the code level. Performance analysis captures the following events: CPU usage, memory usage, function call length and times, and call diagram. Performance analysis also affects application performance.
When should I perform performance analysis?
When considering whether to perform performance analysis, you must first consider whether the application has performance problems? If so, you need to further consider: How big is this problem?

If you do not do this, you will fall into a trap-premature optimization, which may waste your time.

To determine whether the application has performance problems, you should determine the performance objectives. For example, the response time of 100 concurrent users is less than 1 s. Then, you need to conduct a benchmark test to see if this goal is achieved. A common error is benchmarking in the development environment. In fact, you must perform a benchmark test in the production environment. (Actual production environment or simulated production environment, which can be easily implemented in SaaS.
Many products are used for benchmarking, including AB, siege, and JMeter. I personally prefer the JMeter feature set, but AB and siege are easier to use.

Once you confirm that the application has performance problems, you need to analyze its performance, implement improvements, and then conduct a benchmark test again to check whether the problem is resolved. After each change, you should perform a benchmark test to check the effect. If you have made many changes but find that the application performance has declined, you cannot determine which change caused this problem.

Is the performance life cycle I defined:

General causes of performance degradation
Some of the general causes of performance degradation are quite unexpected. Even in advanced languages like PHP, the quality of code is rarely the root cause of the problem. In today's hardware configuration conditions, CPU is rarely the cause of performance restrictions. The common cause is:

Data storage

  • PostgreSQL
  • MySQL
  • Oracle
  • MSSQL
  • MongoDB
  • Riak
  • Cassandra
  • Memcache
  • CouchDB
  • Redis

External resources

  • APIs
  • File system
  • Network interface
  • External process
  • Bad Code

Which performance analyzer is selected?
In the PHP world, there are two completely different performance analyzers: active and passive.

Active VS passive performance analysis
Active analyzer is used during development and is enabled by developers. The active analyzer collects more information than the passive analyzer, which has a greater impact on performance. Generally, the active analyzer cannot be used in the production environment. XDebug is an active analyzer.

Because the active analyzer cannot be used in the production environment, Facebook launched a passive analyzer-XHProf. XHProf is created for use in the production environment. It has minimal impact on performance and collects enough information to diagnose performance problems. Both XHProf and OneAPM are passive analyzer.

Generally, the additional information collected by XDebug is not necessary for analyzing general performance problems. This means that the passive analyzer is a better choice for uninterrupted performance analysis, even in the development environment.

XHProf + XHGui
XHProf, developed by Facebook, contains a basic user interface for viewing performance data. In addition, Paul Reinheim developed XHGui and an enhanced user interface (UI) for viewing, comparing, and analyzing performance data.

Install
Install XHProf
XHProf can be installed through PECL. The steps are as follows:

$ pecl install xhprof-beta

The pecl command will automatically update your php. ini settings. To update the pecl file, run the following command:

$ pecl config-get php_ini

It will add a new configuration line at the top of the specified file (if any. You may want to move them to a more appropriate location.

Once you compile the extension, you must enable it. To do this, you need to add the following code in the php ini file:

[xhprof]extension=xhprof.so 

Then, with XHGui, you can easily perform performance analysis and check.

Install XHGui
To install XHGui, you must obtain it directly from git. This project can be found on github at: https://github.com/perftools/xhgui

XHGui requirements:

  • PHP 5.3 +
  • Ext/mongo
  • Composer
  • MongoDB (optional if you only need to collect data; required if you need data analysis)

First, clone the project to any location. In Debian-based Linux systems (such as Ubuntu), it may be/var/www. In Mac OS X, it may be/Library/WebServer/Documents.

$ cd /var/www$ git clone https://github.com/perftools/xhgui.git$ cd xhgui$ php install.php

The Last Command is to run composer to install dependencies and check the XHGui cache directory permissions. If it fails, you can manually run composer install.

Next, you may need to create a configuration file. This step is easy to implement. you can use the default configuration file in/path/to/XHGui/config. default. php.

If you are running MongoDB locally without authentication, you may not need to do so. Because it will be rolled back to the default value. In a multi-server environment, you need a remote MongoDB server that can be stored on all servers for proper configuration.

To improve MongoDB performance, run the following command to add an index:

$ mongo> use xhprofdb.results.ensureIndex( { 'meta.SERVER.REQUEST_TIME' : -1 } ) db.results.ensureIndex( { 'profile.main().wt' : -1 } ) db.results.ensureIndex( { 'profile.main().mu' : -1 } ) db.results.ensureIndex( { 'profile.main().cpu' : -1 } ) db.results.ensureIndex( { 'meta.url' : 1 } ) 

Other configurations
If you do not want to install mongo in the production environment, or you cannot allow the Web server to access the mongo server, you can save the performance analysis data on the disk and import it to the local MongoDB for later analysis.

To do this, make the following changes in config. php:

<?php 'save.handler' = 'file', 'save.handler.filename' => '/path/to/xhgui/xhprof-' .uniqid("", true). '.dat', ?>

Change the save. handler in the file, unannotate save. handler. filename, and assign it an appropriate value.

Note: By default, only one analysis file is saved every day.

Once the analysis data is ready, you can use the script that comes with XHGui to import it:

$php /path/to/xhgui/external/import.php /path/to/file.dat

The subsequent steps are the same.

Run XHGui
XHGui is a PHP-based Web application. you can use/path/to/xhgui/webroot as the root file to set up a standard VM.

Alternatively, you can simply use PHP 5.4 + cli-server for example:

$ cd /path/to/xhgui$ php -S 0:8080 -t webroot/

This enables XHGui to communicate with all network interfaces through port 8080.

Performance Analyzer
When running the analyzer, you need to include the external/header. php script on all pages to be analyzed. Therefore, you can set auto_prepend_file in the PHP ini file. You can either set it directly in the public INI file or restrict it to a single virtual host.

For the Apache server, add the following code:

php_admin_value auto_prepend_file "/path/to/xhgui/external/header.php"

For Nginx servers, add the following code in server configuration:

fastcgi_param PHP_VALUE "auto_prepend_file=/path/to/xhgui/external/header.php";

If you are using PHP 5.4 + cli-server (PHP-S), you must use the command line flag to set it:

$ php -S 0:8080 -dauto_prepend_file=/path/to/xhgui/external/header.php

By default, Analyzer only analyzes (about) 1% of requests at runtime. This is controlled by the following external/header. php code:

<?php if (rand(0, 100) !== 42) {   return;}?>

If you want to analyze every request (for example, in the development stage), you can comment out this code. If you want to analyze 10% of requests, you can make the following changes:

<?phpif (rand(0, 10) !== 4) {  return;}?>

This allows you to analyze a small number of user requests, but does not affect a single user or too many users.

If you want to manually control the performance during performance analysis, you can do this:

<?php if (!isset($_REQUEST['A9v3XUsnKX3aEiNsUDZzV']) && !isset($_COOKIE['A9v3XUsnKX3aEiNsUDZzV'])) {  return;} else { // Remove trace of the special variable from REQUEST_URI $_SERVER['REQUEST_URI'] = str_replace(array('?A9v3XUsnKX3aEiNsUDZzV', '&A9v3XUsnKX3aEiNsUDZzV'), '', $_SERVER['REQUEST_URI']); setcookie('A9v3XUsnKX3aEiNsUDZzV', 1);}if (isset($_REQUEST['no-A9v3XUsnKX3aEiNsUDZzV'])) {  setcookie('A9v3XUsnKX3aEiNsUDZzV', 0, time() - 86400); return;}?>

This code checks a random GET/POST/COOKIE variable (A9v3XUsnKX3aEiNsUDZzV in this example) and creates a Cookie with the same name to analyze the entire request process. for example: redirection after form submission, Ajax request, and so on.

In addition, it allows a GET/POST variable named no-A9v3XUsnKX3aEiNsUDZzV to delete the Cookie and stop the analysis.

Of course, we welcome you to try OneAPM for free performance analysis for your PHP and Java applications. OneAPM's exclusive probe can go deep into all PHP and Java applications to complete application performance management and monitoring, including visibility of code-level performance problems, fast identification and tracing of performance bottlenecks, Real User Experience Monitoring, server monitoring, and end-to-end application performance management. OneAPM can trace to SQL statement Traces records with poor performance, third-party APIs with poor performance, Web services, and Cache.

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.