Non-intrusive monitoring of PHP application performance monitoring and analysis

Source: Internet
Author: User
Non-intrusive monitoring of PHP application performance monitoring analysis

The so-called non-intrusive monitoring of PHP application performance means to monitor the system without modifying the existing system code. Such a system can be easily applied to PHP applications. You are welcome to exchange ideas.

Solution 1

If you only want to monitor the access time of each request. Check nginx logs directly. There are two options in nginx logs. $ Request_time and $ upstream_response_time. These two options record the response time.

1. $ request_time refers to the time from the first byte of the user request to the time when the response data is sent, that is, the time when the request data is received, the program response time, and the time when the response data is output.

2. $ upstream_response_time refers to the time from when Nginx establishes a connection to the backend (php-cgi) to when the connection is received and then closed.

If you only want to monitor the performance of the backend PHP service, you only need to pay more attention to the $ upstream_response_time option.

Solution 2

If you want to further process a PHP request, the specific part takes a lot of time, you need to use xhprof. Xhprof can generate a call relationship diagram, which clearly shows that the part takes a lot of time. For example, (from the network ):

The following is a complete setup procedure:

Download and install xhprof

1. download and install the following command:

$wget https://github.com/phacility/xhprof/archive/master.zip$unzip ./xhprof_master.zip$cd ./xhprof_master/extension$/usr/local/php/bin/phpize$./configure --with-php-config=/usr/local/php/bin/php-config$make$make install

Note that my php is installed in the/usr/local/php directory. Modify the preceding path as needed.

2. modify the configuration file php. ini.

$vim /etc/php.ini

Add the following content at the bottom:

[Xhprof]

Extension = xhprof. so

Xhprof. output_dir =/tmp/xhprof

3. run the following command to check whether xhprof has been installed successfully.

$/usr/local/php/bin/php -m

If the preceding command output contains xhprof, the xhprof extension is successfully installed.

4. copy the xhprof program to the specified directory.

$mkdir -p /www/sites/xhprof$cp -r ./xhprof_master/xhprof_html /www/sites/xhprof$cp -r ./xhprof_master/xhprof_lib /www/sites/xhprof

5. modify nginx configuration to access performance data through url: add the following code to nginx:

server {        listen  8999;        root    /opt/sites/xhprof/;        index  index.php index.html;        location ~ .*\.php$ {        add_header  Cache-Control "no-cache, no-store, max-age=0, must-revalidate";        add_header  Pragma  no-cache;        add_header Access-Control-Allow-Origin *;        add_header      Via "1.0 xgs-150";        fastcgi_pass   127.0.0.1:9000;        fastcgi_index  index.php;        include fastcgi.conf;        fastcgi_connect_timeout 300;         fastcgi_send_timeout 300;        fastcgi_read_timeout 300;        }   }

6. deploy the performance data collection program to deploy the xhprof. php file to/www/sites/xhprof. php. The content of the xhprof. php file is as follows:

 Max_time "/I/content/getdetail. json "=> 100,); function xh_save_data () {global $ start_time, $ xh_force_disable, $ xh_enable, $ max_time; $ end_time = microtime (true ); $ cost_time = $ end_time-$ start_time; $ cost_time * = 1000; if ($ cost_time> $ max_time &&! $ Export & $ xh_enable) {export de_once "/www/sites/xhprof/xhprof_lib/utils/sources"; export de_once "/www/sites/xhprof/xhprof_lib/utils/sources "; $ xhprof_data = xhprof_disable (); $ objXhprofRun = new XHProfRuns_Default (); $ run_id = $ objXhprofRun-> save_run ($ xhprof_data, "xhprof "); $ log_data = "cost_time | $ cost_time | run_id | $ run_id | request_uri | ". $ _ SERVER ["REQUEST_URI"]. "\ n ";/ /Errors may occur in high concurrency. We recommend that you write const_time run_id request_uri to the database file_put_contents (XH_LOG_PATH, $ log_data, FILE_APPEND) ;}$ xh_request_uri = isset ($ _ SERVER ["REQUEST_URI"])? $ _ SERVER ["REQUEST_URI"]: ""; $ arr_xh_cur_url = explode ("? ", $ Xh_request_uri); $ xh_cur_url = $ arr_xh_cur_url [0]; if (! $ Xh_force_disable & isset ($ xh_conf ["urls"] [$ xh_cur_url]) {$ xh_enable = true; $ max_time = $ xh_conf ["urls"] [$ xh_cur_url]; xhprof_enable (XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY); register_shutdown_function ("xh_save_data");} else {$ xh_enable = false;}?>

Deploy the code file to the/www/sites/xhprof. php directory and enable the file. Because we want to avoid code intrusion into the PHP application code, we can only enable it in the following ways:

* Nginx/PHP-FPM mode:

Fastcgi_param PHP_VALUE "auto_prepend_file =/www/sites/xhprof. php ";

* Apache method:

Php_value auto_prepend_file "/www/sites/xhprof. php"

* Php. ini:

Auto_prepend_file = "/www/sites/xhprof. php"

Note: If opcode cache is used, restart your php process.

7. View performance analysis logs

$ Tail/tmp/xhprof. log

$ Cost_time | 200 | run_id | adadfdsadad | request_uri |/I/content/getcontent. json

In the output above:

Cost_time takes 200 milliseconds

Run_id is adadfdsadad

Request_uri is/I/content/getcontent. json

8. View performance analysis data by run_id

Http: // fig: 8999/xhprof_html/index. php? Run = adadfdsadad

See http://www.cnblogs.com/siqi/p/3790186.html for how to view

Note:

1. make sure that the normal data output is not affected before it is officially enabled. After confirming that the output content is the same, go online again.

2. do not set the value of max_time for each url to be too small.

3. xhprof will affect the performance of online services. Therefore, it is best to monitor the service on only one machine, or modify the xhprof. php code to randomly monitor requests.

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.