📜 ⬆️ ⬇️

PHP code profiling

PHP code profiling

Sooner or later, each of us is faced with legacy code and its optimization. Debagger and profiler in this situation are the best programmer assistants. Those who work with PHP, thanks to Derick Rethans, have a good tool - xDebug. There is a lot of information about xDebug even in runet, so this article is not about it.

Having stumbled upon the mention of a profiler for PHP, I immediately thought about xDebug (I had already forgotten about Zend's proprietary tools for a long time), but this time I was mistaken - it will be about XHProf.
XHProf
')
This profiler was developed specifically for Facebook, and its source code was opened in March 2009.

The installation went fairly quickly and smoothly.
wget pecl.php.net/get/xhprof-0.9.2.tgz
tar xvf xhprof-0.9.2.tgz
cd xhprof-0.9.2/extension/
phpize
./configure && make && make install
cd /usr/local/etc/php.d/
vim xhprof.ini
cd /usr/local/
vim header.php
vim footer.php
vim etc/php.ini
/etc/init.d/php-fpm restart
cp vhost.conf.template prof.my.conf
sed -is/site/prof/ prof.my.conf
vim prof.my.conf
/etc/init.d/nginx restart


Let's sort the mentioned configs

xhprof.ini
[xhprof]
extension=/usr/local/lib/php/extensions/no-debug-non-zts-20090626/xhprof.so
xhprof.output_dir="/home/max/www/profile/"


I added 2 lines in php.ini, which will be discussed below
auto_prepend_file = /usr/local/header.php
auto_append_file = /usr/local/footer.php


prof.my.conf - nginks config - the most standard.

server {
listen 80;
server_name prof.my;
charset utf8;

root /usr/local/src/xhprof-0.9.2/xhprof_html ;
location / {
index index.php;
}

location ~ \.php$ {
fastcgi_pass 127.0.0.1:12000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/src/xhprof-0.9.2/xhprof_html/$fastcgi_script_name;
include fastcgi_params;

}
}


The /usr/local/src/xhprof-0.9.2/xhprof_html contains PHP sources that create a nice WEBGUI for the profiler.

So about two main files:

header.php
<?php
if(isset($_COOKIE['xhprof'])){

if (extension_loaded('xhprof')) {
include_once '/usr/local/src/xhprof-0.9.2/xhprof_lib/utils/xhprof_lib.php';
include_once '/usr/local/src/xhprof-0.9.2/xhprof_lib/utils/xhprof_runs.php';
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
}
}


footer.php
<?php
if(isset($_COOKIE['xhprof'])){
if (extension_loaded('xhprof')) {
$profiler_namespace = 'myapp'; // namespace for your application
$xhprof_data = xhprof_disable();
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, $profiler_namespace);

// url to the XHProf UI libraries (change the host name and path)
$profiler_url = sprintf('http://prof.my/index.php?run=%s&source=%s', $run_id, $profiler_namespace);
echo <<<OUT
Profiler output
OUT;
}
}


Now we run any PHP script through the web and see the link to the profiler output in the upper left corner - this is exactly what the prof.my host was created for

Please note - I use the check on COOKIE! With such a check, you can safely use the profiler on a production server — on real data and real load.

The profiler’s web interface displays a label with information about each function and reports the following information:


It is possible to sort the table by any of the parameters.

Information on each function is divided into two types Inclusive and Exclusive. Inclusive includes the numbers used by the child calls, and Exclusive does not include them. It is also possible by clicking on the function name to see information only on it and the functions from which it was called and which were called to it.

If GraphViz is installed in the system, the profiler will draw you a call graph.

PS Without breaking traditions: this is my first post on Habré.

UPD: reposted in PHP.

Source: https://habr.com/ru/post/78210/


All Articles