Activate Xhprof only in slow pages

169 Views Asked by At

Is it possible to activate xhprof only after a -tot- of time after a page/url is called?

Normally I have pages that respond under 1 second. Sometimes they responde in 10 seconds (no, it's not the session on file). I want to activate xhprof after 1 second from the beginning of the request. (It's ok to lose first second of profiling by now)

If it was javascript, we have the setTimeout, but PHP is not threaded.

Any Idea?

1

There are 1 best solutions below

1
On

You can use something like this. xhprofon.php:

function my_xhprof_disable()
{
    $xhprof_data = xhprof_disable();

    if(defined('MY_DEBUG_START') && (microtime(true) - MY_DEBUG_START) > 1)
    {
        $XHPROF_ROOT = "/src/to/xprof/";
        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, "mysitename");
    }
}

define('MY_DEBUG_START', microtime(true));
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

register_shutdown_function('my_xhprof_disable');

Then use auto_prepend_file for include this file on all pages.