2. Xhprof -
• "We should forget about small efficiencies,
say about 97% of the time: premature
optimization is the root of all evil” - Knuth –
err, Hoare – err, no idea.
3. History
• Developed at “the facebook”
• Code still maintained:
https://github.com/facebook/xhprof
• Docs, not so much:
http://web.archive.org/web/20110514095512
/http://mirror.facebook.net/facebook/xhprof/
doc.html
5. Enable in PHP
● To use xhprof, you need to tell your php code
that you want to profile it:
php.ini is the easiest:
php_value auto_prepend_file
"../../xhprof/external/header.php"
php_value auto_append_file
"../../xhprof/external/footer.php"
6. Or...
you can enable on a case by case basis using:
<?php
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
for ($i = 0; $i <= 1000; $i++) {
$a = $i * $i;
}
$xhprof_data = xhprof_disable();
$XHPROF_ROOT = "/tools/xhprof/";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_testing");
echo "http://localhost/xhprof/xhprof_html/index.php?
run={$run_id}&source=xhprof_testingn";
7. Basic Usage
• Just load a page
• Small link will appear on bottom
• Click!
8. What the output means
• Inclusive Time: Time spent in this function + called
functions
• Exclusive Time: Time spent in this function only
• Wall Time: Time between when something started +
finished as measured by the clock on the wall
• CPU Time: Time spent in user space + kernel space
(doesn't include IO)
9. What the output means
• Inclusive Time: Time spent in this function + called
functions
• Exclusive Time: Time spent in this function only
• Wall Time: Time between when something started +
finished as measured by the clock on the wall
• CPU Time: Time spent in user space + kernel space
(doesn't include IO)
14. Caveats
• Can get resource intensive, especially on VM.
• Don't use with XDEBUG
• OK to use in production, but maybe not all the
time... flag to enable, or enable on a per
server basis to collect data sometimes.