Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction, installation and use of hierarchical performance Analyzer in PHP

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "the introduction, installation and use of hierarchical performance analyzer in PHP", in daily operation, I believe many people in PHP hierarchical performance analyzer introduction, installation and use of problems there are doubts, small series consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "PHP hierarchical performance analyzer introduction, installation and use" doubts helpful! Next, please follow the small series to learn together!

Simple learning Hierarchical Performance Analyzer in PHP

In PHP, when we need to debug, we usually use memory_get_usage() to see how memory is used. But if you want to see the CPU usage of the current script, there are no ready-made functions. PHP does, however, provide us with an extension: XHProf, an open-source extension from FaceBook that lets us see some of the performance of the current script runtime.

What is XHProf

XHProf is a lightweight layered performance measurement analyzer. During the data collection phase, it tracks the number of calls versus the measurement data, showing an arc graph of the program's dynamic calls. It calculates exclusive performance metrics such as elapsed runtime, CPU computation time, and memory overhead during the reporting, post-processing phases. Function performance reports can be terminated by the caller and callee. XHProf detects recursive functions by calling loops of graphs during the data gathering phase, and avoids recursively called loops by giving them unique depth names.

In short, XHProf is able to collect a lot of runtime system state for us, and it comes with a set of online charting tools that can provide us with detailed chart information.

installation

Download the extension package directly from PECL. Just like any other extension installation, this extension is always in an updated maintenance state, so it is perfectly supported for PHP 7.

Command line uses xhprof_enable(XHPROF_FLAGS_NO_BUILTINS| XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY);

//xhprof_enable( XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY);

//xhprof_enable( XHPROF_FLAGS_MEMORY);

//xhprof_enable();

for ($i = 0; $i Array

// (

// [ct] => 1

// [wt] => 16

// [cpu] => 21

// [mu] => 848

// [pmu] => 0

// )

// [main()] => Array

// (

// [ct] => 1

// [wt] => 115

// [cpu] => 115

// [mu] => 1416

// [pmu] => 0

// )

// )

Use xhprof_enable() to enable the analyzer, its parameters are several constants, roughly meaning to display CPU information, memory information, etc. If there are no parameters, it will only return ct, wt these two parameters.

Call xhprof_disable() to finish the analysis and return the analysis result. The content of the return value includes the operation of the main function, which is the case of our current page test code. There is also the performance of the function called test_application(). Specifically:

ct: number of calls wt: wait run time, ms cpu: cpu run time, ms mu: memory usage, bytes pmu: peak memory usage

Yes, it was as simple as that. By calling two functions, we can see the running state of the current script program, how much memory is occupied, and how much CPU time is consumed. Of course, more conveniently, it can also display more detailed information via web pages.

web View Results Report

First of all, we need to do some preparatory work. One is to install a graphviz for graphics rendering, two is to modify the php.ini file results storage directory, three is to copy the source code analyzer code and modify our previous test code.

yum install graphviz

In CentOS we can install graphviz directly using yum. Then we specify the output directory for xhprof in the php.ini file.

xhprof.output_dir=/tmp

Next, we need to copy the xhporn_html directory and xhporn_lib directory from the source file into the project file. Then modify the code to save the results of the performance analysis.

// ..... the above code

$xhprof_data = xhprof_disable();

require 'xhprof_lib/utils/xhprof_lib.php';

require 'xhprof_lib/utils/xhprof_runs.php';

$xhprofRuns = new XHProfRuns_Default();

$runId = $xhprofRuns->save_run($xhprof_data, 'xhprof_test');

echo 'http://192.168.56.102/index.php? run=' . $runId . '&source=xhprof_test';

Then we need to set up the server in xhporn_html, where I'll run it with a simple php -S command.

cd xhprof_html/

php -S 0.0.0.0:80

Finally, visit this page with the link output above, you can see the relevant information.

At this point, the study of "Introduction, Installation and Use of Hierarchical Performance Analyzer in PHP" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report