How to use xhprof to test php performance in php7? (Method introduction)

青灯夜游
Release: 2023-02-17 17:02:01
forward
4562 people have browsed it

How to use xhprof to test php performance in php7? (Method introduction)

Introduction

1 Background

  • PHP's xhprof extension Facebook will no longer be updated and maintained because Facebook has fully used HHVM and no longer uses the PHP zend engine.

  • xhprof does not support the new version of PHP (PHP7). The tideways extension is maintained from the xhprof project fork and currently supports PHP 7.2, 7.1, 7.0, 5.6 and 5.5.

  • tideways is an open source project. It only charges for UI services. In fact, xhgui can fully meet our daily needs

# #2 Function

Tideways is an extension used to test PHP performance. It can obtain the functions called during the entire process of PHP execution, the number of function calls, execution time, CPU time, and memory usage. , memory peak, total execution time, total CPU time, total memory usage, total memory peak and other data, analyze the above data to find out the performance bottleneck of PHP, analyze the PHP execution process, etc.

3 Advantages

  • tideways is a PHP extension, combined with xhgui, there is no need to bury it in the PHP code Monitoring code

  • You can set the execution frequency (for example, 1/100) without generating execution logs for every request, resulting in performance loss; you can also actively control whether to generate execution logs by requesting Parameters to control (debug=1)

  • There is a simple and direct UI to transform data

  • You can freely match the conditions for data filtering. For example, analyze a specific interface, analyze the interface request situation in a certain time period, etc.

4 Disadvantages

Although non-intrusive, if execution logs are generated for each interface, the consumption of CPU and memory is not negligible.

5 Implementation principle

    tideways extension is responsible for generating running logs
  • In nginx, by configuring fastcgi_param PHP_VALUE auto_prepend_file, Execute the PHP file configured with auto_prepend_file before the request starts. The register_shutdown_function method is used in the file. At the end of the PHP process, tideways_disable is called to realize the embedding of tideways. Then the execution log is stored in mongodb or mysql or a file, and displayed after analysis by xhgui. , display formats include bar chart, waterfall flow, and flame chart.

Application

Next, we will introduce two application methods:

IntrusiveandNon-invasive

The intrusive type refers to adding code to the code, and the intrusive type uses the default UI;

The non-intrusive type refers to adding code without making any changes to the code. Modify nginx/apache to implement code injection, using xhgui non-invasively;

Installation

tideways_xhprof

git clone "https://github.com/tideways/php-xhprof-extension.git" cd php-xhprof-extension phpize ./configure --with-php-config=/usr/local/php7/bin/php-config make sudo make install
Copy after login
Add extension=tideways_xhprof.so# in php.ini

##Non-intrusive:


        
Copy after login
The .xhprof file generated here is in the tmp directory. The default UI will also search for .xhprof in the tmp directory. File

Installs the default UI used to find data

git clone git@github.com:phacility/xhprof.git
Copy after login

xhprof_lib

andin this repository xhprof_htmldirectory into your web folder and navigate toxhprof_html/index.phpto see the trace list.If you want to see function call notes, you need to install Callgraph

Install CallgraphCallgraph is actually a combination of three tools become.

One is cflow or calltree which is used to generate C function call tree. The following mainly introduces cflow.

A tool for processing dot text graphics language, enhanced by graphviz.

A script for converting C function call tree to dot format: tree2dotx

Take Ubuntu as an example, install them separately:

sudo apt-get install cflow graphviz
Copy after login

Next install tree2dotx and Callgraph , here they are installed to /usr/local/bin by default.

wget -c https://github.com/tinyclub/linux-0.11-lab/raw/master/tools/tree2dotx wget -c https://github.com/tinyclub/linux-0.11-lab/raw/master/tools/callgraph sudo cp tree2dotx callgraph /usr/local/bin sudo chmod +x /usr/local/bin/{tree2dotx,callgraph}
Copy after login

The next two renderings are shown:

Intrusive:Intrusive use of xhgui requires mongodb

Install xhgui

git clone git@github.com:perftools/xhgui.git
Copy after login

Configure Nginx

server { listen 80; server_name site.localhost; root /Users/markstory/Sites/awesome-thing/app/webroot/; fastcgi_param PHP_VALUE "auto_prepend_file=/home/www/xhgui/external/header.php"; #这里依据个人目录而配 }
Copy after login
What this means is to execute header.php before executing the project php code, so as to achieve the purpose of non-intrusive detection performance

xhgui configuration (Frequency of generating logs)In xhgui’s config/config.default.php, the number of sampling hits can be set;

return rand(1, 100) === 42; 为1%的采样率,改成return True;则标识每次都采样

'profiler.enable' => function() { // url 中包含debug=1则百分百捕获 if(!empty($_GET['debug'])){ return True; }else{ // 1%采样 return rand(1, 100) === 42; } }
Copy after login

mongodb的配置

   xhgui/config/config.default.php

// Can be either mongodb or file. /* 'save.handler' => 'file', 'save.handler.filename' => dirname(__DIR__) . '/cache/' . 'xhgui.data.' . microtime(true) . '_' . substr(md5($url), 0, 6), */ 'save.handler' => 'mongodb', // Needed for file save handler. Beware of file locking. You can adujst this file path // to reduce locking problems (eg uniqid, time ...) //'save.handler.filename' => __DIR__.'/../data/xhgui_'.date('Ymd').'.dat', 'db.host' => 'mongodb://127.0.0.1:27017', 'db.db' => 'xhprof',
Copy after login

mongo服务器的配置

mongo > use xhprof > db.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 } )
Copy after login

最后展示几张xhgui的效果图

相关学习推荐:PHP编程从入门到精通

The above is the detailed content of How to use xhprof to test php performance in php7? (Method introduction). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!