Fine-tune Drupal performance using XHProf profiling
Is your Drupal site running slow? Discover how XHProf profiling can help you pinpoint performance bottlenecks, optimize your code, and speed up your website.
seen from United Kingdom
seen from China
seen from United States
seen from United States
seen from Belgium
seen from Saudi Arabia

seen from Russia
seen from Türkiye
seen from United Kingdom

seen from United States
seen from Canada
seen from Germany

seen from United States

seen from United States
seen from Italy
seen from Germany
seen from Portugal
seen from Japan
seen from Türkiye

seen from Malaysia
Fine-tune Drupal performance using XHProf profiling
Is your Drupal site running slow? Discover how XHProf profiling can help you pinpoint performance bottlenecks, optimize your code, and speed up your website.

Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
Free to watch • No registration required • HD streaming
Setting up XHProf
1. Download the XHProf from Github and place inside html folder.
https://github.com/longxinH/xhprof
2. Create a xhprof-caller.php file inside XHProf folder, then paste the code below. Feel free to modify the code.
3. Prepend the created file in php.ini or .user.ini
auto_prepend_file = /var/www/html/xhprof/xhprof-caller.php
4. Go to xhprof/xhprof_html folder then run the ff. command
php -S localhost:8080
5. Now, run your PHP project that you want to profile.
php -S localhost:8000
Additional resources
https://www.php.net/manual/en/xhprof.examples.php https://github.com/tideways/php-xhprof-extension https://github.com/patrickallaert/xhprof
XHProf安装使用笔记
因为导数据效率很低,想查下为什马,百度下发现有XHProf这么个东西. XHProf是Facebook开源的一个PHP性能检测程序,大致看了一下,很棒,里面有很多直观的数据来说明问题,比如调用次数、执行时间、内存使用、CPU占用等.所以安装试下。
开始安装:
获取源代码包
sky@sky-PC:~$wget http://pecl.php.net/get/xhprof-0.9.2.tgz 解压 sky@sky-PC:~$tar zxf xhprof-0.9.2.tgz sky@sky-PC:~$cd xhprof-0.9.2 复制web访问目录到web应用目录 sky@sky-PC:~$cp -r xhprof_html xhprof_lib /var/www/html/ #复制xhprof的展示页面目录和库目录到web目录下,可以为xhprof_html建个虚拟目录来访问,也可以把这两个目录拷贝到应用的根目录下。 sky@sky-PC:~$cd extension/ 编译插件 sky@sky-PC:~$/usr/local/webserver/php/bin/phpize sky@sky-PC:~$./configure --with-php-config=/usr/local/webserver/php/bin/php-config sky@sky-PC:~$make
sky@sky-PC:~$sudo make install
配置 php.ini 文件
echo -e "[xhprof]\nextension=xhprof.so;\nxhprof.output_dir=/tmp" >> /usr/local/php/lib/php.ini
重启web服务器
为了更加清晰显示程序执行、调用结构,安装Graphviz,如果安装了Graphviz,XHProf会用比较牛的图形方式展现统计数据。 获取源码包 sky@sky-PC:~$wget http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.24.0.tar.gz sky@sky-PC:~$tar zxf graphviz-2.24.0.tar.gz sky@sky-PC:~$cd graphviz-2.24.0 编译安装 sky@sky-PC:~$./configure sky@sky-PC:~$make sky@sky-PC:~$make install
接下来在要统计的php应用中加入以下语句: xhprof_enable(); //统计的代码部分之前加,如果要显示CPU占用 可以加入XHPROF_FLAGS_CPU参数,内存是XHPROF_FLAGS_MEMORY,如果两个一起:XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY,如:xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
xhprof_disable();//统计的代码部分之后加
例子如下 :
<?php function a(){ echo 'a'; } xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY); //XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY // run program a(); // stop profiler $xhprof_data = xhprof_disable(); //返回运行数据
// 下面是保存运行数据 include_once "xhprof_lib/utils/xhprof_lib.php"; include_once "xhprof_lib/utils/xhprof_runs.php"; $xhprof_runs = new XHProfRuns_Default(); $run_id = $xhprof_runs->save_run($xhprof_data, "xhprof"); //第一个参数是 xhprof_disable()函数返回的运行信息,第二个参数是自定义的命名空间字符串(任意字符串),返回运行ID。
echo "http://127.0.0.1/xhprof_html/index.php?run=$run_id&source=xhprof\n";
执行成功后直接复制到浏览器即可!
Ce tutoriel indique comment installer xhprof sur un serveur debian wheezy pour un projet Symfony. XHPROF est un profiler permettant de diagnostiquer plus facilement des problèmes de performances sur un site PHP.
Finding bottleneck codes using XDebug
Previously, I used XHProf to profile my website. Now I found out that XDebug is just as awesome as XHProf.
So basically, I have this problem with my site. The web is just too slow for my taste. Using XDebug, I found out that the translation system function sucks BIG time. So here's the culprit:
$page_output = strtr($page_output, $this->translations);
$this->translations is the translation keys and vals, with thousands of array pairs inside. This function took 130 seconds!
So, I have to find a replacement for strtr, because it's infamous of being very slow in large array. I got this:
$page_output = str_replace(array_keys($this->translations), array_values($this->translations), $page_output);
Surprisingly, the previous strtr code that took 130 seconds, now only take 2 seconds!
But I am not satisfied. I search for more better code, and I got this!
function replace ($text, $replace) { $keys = array_keys($replace); $length = array_combine($keys, array_map('strlen', $keys)); arsort($length); $array[] = $text; $count = 1; reset($length); while ($key = key($length)) { if (strpos($text, $key) !== false) { for ($i = 0; $i < $count; $i += 2) { if (($pos = strpos($array[$i], $key)) === false) continue; array_splice($array, $i, 1, array(substr($array[$i], 0, $pos), $replace[$key], substr($array[$i], $pos + strlen($key)))); $count += 2; } } next($length); } return implode($array); } $output_text = replace($output_text, $this->translations);
It's a whopping 1 second!
Now the question is, how do I use XDebug to profile my codes? Well if you use XAMPP, you're in luck! It's already there. You just need to change these three line of configuration in php.ini and you are ready to go!
xdebug.profiler_enable = 1 xdebug.profiler_output_dir = "C:\xampp\tmp" xdebug.profiler_output_name = "cachegrind.out.%H_%s_%R"
It will generate files in C:\xampp\tmp. After that, you can open the file using WinCacheGrind. And that's it!
Note: My computer is sloooow, which is good so that I can see optimization at it's best. But on my Linux server, it's super fast! Here's what my Webmaster Tools had to say:
Performance overview
On average, pages in your site take 2.2 seconds to load (updated on May 21, 2012). This is faster than 63% of sites.
On the right is the Site Performance. The lower the better, and we are almost there!

Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
Free to watch • No registration required • HD streaming
Profiling a PHP application
There are quite some ways to profile PHP applications, as you can for example read in Eric Hogue's Blog. We've been using Xdebug with KCachegrind at Yahoo! after rolling out the new Movies page for Europe. Compared to XHProf (check documentation here) with XHGui that's quite complicated and you can't use it in a production environment - at least not on a regular basis (aka just keep it running).
If you want to give it a try you don't have to download and manually install (as Eric suggests) because it's a pecl package, so you can simply install it with:
sudo pecl install xhprof
Then you should follow the git installation as Eric writes under XHProf. I installed it under "/usr/local", so changed to that directory and issued the "git clone" command there having the XHGui stuff under "/usr/local/xhprof" now.
If you want to have nice call graphs (see Eric's blog entry for a sample image) you'll have to install Graphviz which on Ubuntu/Debian is nothing more than a:
sudo apt-get install graphviz
I recommend to follow Paul Reinheimer's blog about using XHGui - he's the author of that package.
Here are some configuration hints for the "config.php" file, as Paul's blog entry and the comments in the file don't make everything clear:
$_xhprof['display'] = false; $_xhprof['doprofile'] = false;
The display param only needs to be true if you want a link to the profiling result page in the XHGui which imho only makes sense if you "manually" profile with ?_profile=1. If the doprofile param is true than every request will be profiled - not good for a production environment. Use the weight param to profile only some requests and you should be fine.
Only one thing does not yet work as expected in my setup - only page requests via browser seem to be profiled (or logged by the XHGui). API like request (via Zend_Http_Client in my case) don't show up. I'll update this post once I find the answer.
Read more about PHP profiling in those slides from Justin Carmony.
PHP の処理速度のボトルネックを解析できる XHProf。Graphbiz と連携して UML 図を生成できる