Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Setting up xhprof on Moodle: Difference between revisions

From MoodleDocs
(24 intermediate revisions by 11 users not shown)
Line 1: Line 1:
The following instructions are for setting up xhprof for Moodle under a Ubuntu/Debian environment. The process should be similar for other linux enviroments, but will need some tweaking if you wish to do this under windows. Please update this document if you find any major problems.
The following instructions are for setting up xhprof for Moodle under a Ubuntu/Debian environment. The process should be similar for other linux enviroments, but will need some tweaking if you wish to do this under windows. Please update this document if you find any major problems.


==Installing xhprof==
==Installing xhprof with Linux==


=== Debian/Ubuntu package ===
apt-get install php5-xhprof
The current LTS Ubuntu package is version 0.9.4-1build1
=== Tideways ===
apt-get install tideways-php
===Build manually===
<code bash>
<code bash>
mkdir ~/src/
mkdir ~/src/
Line 14: Line 25:
sudo make install
sudo make install
</code>
</code>
Note: The version downloaded via these instructions is not compatible with PHP 5.4 - instead you should download the latest version of the code from https://github.com/facebook/xhprof (before following the instructions from 'cd xhprof-0.9.2/extension/' onwards).
Note: It is available now, the 0.9.3 version from the pecl website. You can run wget http://pecl.php.net/get/xhprof-0.9.3.tgz, if you have PHP 5.4 version.


Add the following to the apache version of you php.ini file
Add the following to the apache version of you php.ini file
Line 21: Line 36:
extension=xhprof.so
extension=xhprof.so
xhprof.output_dir="/var/tmp/xhprof"
xhprof.output_dir="/var/tmp/xhprof"
</code>
On some systems this may get created for you under <code>mods-available/xhprof.ini</code> in which case you simply need to enable it:
<code bash>
php5enmod xhprof
</code>
Or for php7 / tideways:
<code bash>
phpenmod tideways
</code>
</code>


Restart Apache
Restart Apache
<code bash>
<code bash>
sudo service apache2 retart
sudo service apache2 restart
</code>
</code>


Create a file in your web root that makes a call to phpinfo(); and then view the result in your browser to make sure that xhprof is enabled in PHP. Checking the output of '''php -m''' would also work if you are sure that the command line version of PHP uses the same php.ini file as your web server.
Create a file in your web root that makes a call to phpinfo(); and then view the result in your browser to make sure that xhprof is enabled in PHP. Checking the output of '''php -m''' would also work if you are sure that the command line version of PHP uses the same php.ini file as your web server.
===Troubleshooting===
==== Graph isn't generated ====
If you see a '''failed to shell execute cmd=" dot -Tpng''' error when you follow the '''View Full Callgraph''' link you may need to install graphviz ('''sudo apt-get install graphviz''').
Into Windows environment this error can be caused by a path hardcoded into "\lib\xhprof\xhprof_lib\utils\callgraph_utils.php", you must replace one parameter of the proc_open function which is a Linux path "/tmp" by a Windows one, like "c:/temp".
==== Call to undefined function tideways_enable ====
Verion 5 of tideways was renamed which broke things:
<code php>
Exception - Call to undefined function tideways_enable()
Debug info:
Error code: generalexceptionmessage
Stack trace:
line 168 of /lib/xhprof/xhprof_moodle.php: Error thrown
line 819 of /lib/setup.php: call to profiling_start()
line 53 of /config.php: call to require_once()
line 37 of /my/index.php: call to require_once()</code>
One solution is to update everything to the latest, or you can force downgrade tideways to v4:
sudo apt install tideways-php=4.1.6
See also for more details
https://tideways.com/profiler/blog/tideways-no-5
https://tracker.moodle.org/browse/MDL-62280
==Installing xhprof with Windows==
You will need to download PHP extensions compiled for your version of PHP. [http://dev.freshsite.pl/php-extensions/xhprof.html Here is one source...]
Extract the contained dll file to the ext directory of your server. With XAMPP this is in xampp\php\ext
Add a line to your php.ini file that matches dll filename you are using. This goes in the Dynamic Extensions section.
<code>
extension=xhprof_0.10.3_php54_vc9.dll
</code>
Stop and restart Apache through your server control interface. If it complains, you probably don't have the correct extension for your version of PHP, or the extension entry in your php.ini file doesn't match the dll file.
To test that the extension is installed, log in to Moodle as an administrator and navigate to Administration > Site administration > Server > PHP info. Search for information about the xhprof extension. It should be present and enabled.


==Configuring Moodle to use xhprof==
==Configuring Moodle to use xhprof==
[[Image:profilingOption.png|thumb|right|The profiling option is displayed when the php xhprof extension is installed]]
[[Image:profilingOption.png|thumb|left|The profiling option is displayed when the php xhprof extension is installed]]
[[Image:profilingOutput.png|thumb|right|Tabular profiling output produced by xhprof]]
[[Image:callgraph.png|thumb|right|An example call graph showing slow parts of the profiled run]]
Once the xhprof php extension is correctly installed you will find a new "Profiling" option available under Settings > Site administration > Development
 
When you load the profiling settings page you are confronted with several options. In order to get xhprof up and running with minimal fuss you merely need to check the "Enable profiling" box and set a path that you wish to be profiled. You can simply set this to a wildcard using the asterisk symbol ('''*''') while you are testing profiling. Afterwards you can come back and set this to a more restrictive setting once you know profiling is working. Note the paths need to be specified relative to your Moodle directory and not the web root.
 
After you have enabled profiling you should load some pages within the "Profile these" filter that you set in the step above. Once you have done these go to Settings > Site administration > Development > Profiling runs and you should see a list of the pages you visited. Clicking on one of these and then clicking on the "View profiling details" will take you to a profiling table full of helpful stats for tracking down slow pages and functions. You can get an even more interesting breakdown of this by then clicking on the "View Full Callgraph" link. This callgraph makes it easy to see where the slow parts of each particular run are and functions which individually may not take a long time to run but which are run thousands of times and thus resulting in a great deal of slowdown.
[[Image:profilingSettings.png|frame|center|Enabling profiling and setting it to run on all pages via a wildcard]]
[[Image:profilingSettings.png|frame|center|Enabling profiling and setting it to run on all pages via a wildcard]]
[[Image:profilingRuns.png|frame|center|A profiling runs option can be seen after enabling profiling]]
[[Image:profilingRuns.png|frame|left|A profiling runs option can be seen after enabling profiling]]
[[Image:profilingOutput.png|thumb|center|Tabular profiling output produced by xhprof]]
 
[[Image:callgraph.png|thumb|center|An example call graph showing slow parts of the profiled run]]
==Using XHProf==
 
MDL-39443 shows the kind of thing that can be done using XHProf.
 
[http://tjhunt.blogspot.co.uk/2013/05/performance-testing-moodle.html This blog post] talks about the process.
 
Since Moodle 2.5.1 it's possible to export and import any profiling run. That enables some interesting uses like, comparisons along the time, between different systems and sharing or runs in other systems like the Tracker when optimization issues are being fixed.

Revision as of 00:25, 31 January 2019

The following instructions are for setting up xhprof for Moodle under a Ubuntu/Debian environment. The process should be similar for other linux enviroments, but will need some tweaking if you wish to do this under windows. Please update this document if you find any major problems.

Installing xhprof with Linux

Debian/Ubuntu package

apt-get install php5-xhprof

The current LTS Ubuntu package is version 0.9.4-1build1

Tideways

apt-get install tideways-php

Build manually

mkdir ~/src/ cd ~/src/ wget http://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 sudo make install

Note: The version downloaded via these instructions is not compatible with PHP 5.4 - instead you should download the latest version of the code from https://github.com/facebook/xhprof (before following the instructions from 'cd xhprof-0.9.2/extension/' onwards).

Note: It is available now, the 0.9.3 version from the pecl website. You can run wget http://pecl.php.net/get/xhprof-0.9.3.tgz, if you have PHP 5.4 version.

Add the following to the apache version of you php.ini file

[xhprof] extension=xhprof.so xhprof.output_dir="/var/tmp/xhprof"

On some systems this may get created for you under mods-available/xhprof.ini in which case you simply need to enable it:

php5enmod xhprof

Or for php7 / tideways:

phpenmod tideways

Restart Apache sudo service apache2 restart

Create a file in your web root that makes a call to phpinfo(); and then view the result in your browser to make sure that xhprof is enabled in PHP. Checking the output of php -m would also work if you are sure that the command line version of PHP uses the same php.ini file as your web server.

Troubleshooting

Graph isn't generated

If you see a failed to shell execute cmd=" dot -Tpng error when you follow the View Full Callgraph link you may need to install graphviz (sudo apt-get install graphviz).

Into Windows environment this error can be caused by a path hardcoded into "\lib\xhprof\xhprof_lib\utils\callgraph_utils.php", you must replace one parameter of the proc_open function which is a Linux path "/tmp" by a Windows one, like "c:/temp".

Call to undefined function tideways_enable

Verion 5 of tideways was renamed which broke things:

Exception - Call to undefined function tideways_enable() Debug info: Error code: generalexceptionmessage Stack trace: line 168 of /lib/xhprof/xhprof_moodle.php: Error thrown line 819 of /lib/setup.php: call to profiling_start() line 53 of /config.php: call to require_once() line 37 of /my/index.php: call to require_once()

One solution is to update everything to the latest, or you can force downgrade tideways to v4:

sudo apt install tideways-php=4.1.6

See also for more details

https://tideways.com/profiler/blog/tideways-no-5

https://tracker.moodle.org/browse/MDL-62280

Installing xhprof with Windows

You will need to download PHP extensions compiled for your version of PHP. Here is one source...

Extract the contained dll file to the ext directory of your server. With XAMPP this is in xampp\php\ext

Add a line to your php.ini file that matches dll filename you are using. This goes in the Dynamic Extensions section.

extension=xhprof_0.10.3_php54_vc9.dll

Stop and restart Apache through your server control interface. If it complains, you probably don't have the correct extension for your version of PHP, or the extension entry in your php.ini file doesn't match the dll file.

To test that the extension is installed, log in to Moodle as an administrator and navigate to Administration > Site administration > Server > PHP info. Search for information about the xhprof extension. It should be present and enabled.

Configuring Moodle to use xhprof

The profiling option is displayed when the php xhprof extension is installed
Tabular profiling output produced by xhprof
An example call graph showing slow parts of the profiled run

Once the xhprof php extension is correctly installed you will find a new "Profiling" option available under Settings > Site administration > Development

When you load the profiling settings page you are confronted with several options. In order to get xhprof up and running with minimal fuss you merely need to check the "Enable profiling" box and set a path that you wish to be profiled. You can simply set this to a wildcard using the asterisk symbol (*) while you are testing profiling. Afterwards you can come back and set this to a more restrictive setting once you know profiling is working. Note the paths need to be specified relative to your Moodle directory and not the web root.

After you have enabled profiling you should load some pages within the "Profile these" filter that you set in the step above. Once you have done these go to Settings > Site administration > Development > Profiling runs and you should see a list of the pages you visited. Clicking on one of these and then clicking on the "View profiling details" will take you to a profiling table full of helpful stats for tracking down slow pages and functions. You can get an even more interesting breakdown of this by then clicking on the "View Full Callgraph" link. This callgraph makes it easy to see where the slow parts of each particular run are and functions which individually may not take a long time to run but which are run thousands of times and thus resulting in a great deal of slowdown.

Enabling profiling and setting it to run on all pages via a wildcard
A profiling runs option can be seen after enabling profiling

Using XHProf

MDL-39443 shows the kind of thing that can be done using XHProf.

This blog post talks about the process.

Since Moodle 2.5.1 it's possible to export and import any profiling run. That enables some interesting uses like, comparisons along the time, between different systems and sharing or runs in other systems like the Tracker when optimization issues are being fixed.