<?php
use Rarst\Profiler\Handler;
global $wp;
if ( Handler::$profiling && empty( $wp ) ) {
Handler::close();
}
view raw append.php hosted with ❤ by GitHub
<?php
namespace Rarst\Profiler;
class Handler {
public static $profiling = false;
public static $mode;
public static $includes = [
'xhprof' => [
'../xhprof/xhprof_lib/utils/xhprof_lib.php',
'../xhprof/xhprof_lib/utils/xhprof_runs.php',
],
'uprofiler' => [
'../uprofiler/uprofiler_lib/utils/uprofiler_lib.php',
'../uprofiler/uprofiler_lib/utils/uprofiler_runs.php',
],
];
public static $hosts = [
'xhprof' => 'http://xhprof.rarst.net/',
'uprofiler' => 'http://uprofiler.rarst.net/',
];
static function run() {
self::$profiling = isset( $_REQUEST['XHPROF'] ) || isset( $_REQUEST['UPROFILER'] ) || isset( $_REQUEST['XDEBUG_PROFILE'] );
define( 'PROFILING', self::$profiling );
if ( ! self::$profiling ) {
return;
}
if ( isset( $_REQUEST['XDEBUG_PROFILE'] ) ) {
self::$mode = 'xdebug';
return;
}
self::$mode = isset( $_REQUEST['XHPROF'] ) ? 'xhprof' : 'uprofiler';
foreach ( self::$includes[ self::$mode ] as $file ) {
require_once $file;
}
global $wp_filter;
if ( ! isset( $wp_filter ) ) {
$wp_filter = array();
}
$callback = __CLASS__ . '::close';
$wp_filter['shutdown'][100] = array(
$callback => array(
'function' => $callback,
'accepted_args' => 1,
)
);
self::open();
}
static function open( $name = '' ) {
if ( ! self::$profiling || ! self::is_capturing( $name ) ) {
return;
}
switch ( self::$mode ) {
case 'xhprof' :
$flags = version_compare( PHP_VERSION, '5.5', '>=' ) ? XHPROF_FLAGS_NO_BUILTINS : XHPROF_FLAGS_MEMORY;
xhprof_enable( $flags );
break;
case 'uprofiler':
$flags = UPROFILER_FLAGS_MEMORY;
uprofiler_enable( $flags );
break;
}
}
static function close( $name = '' ) {
if ( ! self::$profiling || ! self::is_capturing( $name ) ) {
return;
}
switch ( self::$mode ) {
case 'xhprof':
$data = xhprof_disable();
$runs = new \XHProfRuns_Default();
break;
case 'uprofiler':
$data = uprofiler_disable();
$runs = new \UprofilerRuns_Default();
break;
}
$namespace = $_SERVER['SERVER_NAME'];
$run_id = $runs->save_run( $data, $namespace );
$notification = <<<NOTIFICATION
<div style="position:fixed; right: 5px; bottom: 5px;">
<a href="%s" target="_blank" class="btn btn-success" style="margin-right: 5px;">Profiler output</a>
<a href="%s" target="_blank" class="btn btn-info">Callgraph</a>
</div>
NOTIFICATION;
$profiler_url = self::get_url( 'index.php?run=%s&source=%s', $run_id, $namespace );
$callgraph_url = self::get_url( 'callgraph.php?run=%s&source=%s', $run_id, $namespace );
printf( $notification, $profiler_url, $callgraph_url );
}
static function is_capturing( $name ) {
return $_REQUEST[ strtoupper( self::$mode ) ] === $name;
}
static function get_url( $path = '', $run_id = '', $namespace = '' ) {
return self::$hosts[ self::$mode ] . sprintf( $path, $run_id, $namespace );
}
}
view raw class-handler.php hosted with ❤ by GitHub
<?php
require '/server/ext/vendor/autoload.php';
require __DIR__ . '/class-handler.php';
\Rarst\Profiler\Handler::run();
if ( defined( 'PROFILING' ) && ! PROFILING ) {
}
view raw prepend.php hosted with ❤ by GitHub
javascript:if (document.URL.indexOf('UPROFILER') < 1) {
var sep = document.URL.indexOf('?');
sep = (sep < 1) ? '?' : '&';
window.location.href = document.URL + sep + 'UPROFILER';
}