Skip to content

Instantly share code, notes, and snippets.

@paunin
Last active September 28, 2018 07:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paunin/aed4f15a4b03b6dba6bf to your computer and use it in GitHub Desktop.
Save paunin/aed4f15a4b03b6dba6bf to your computer and use it in GitHub Desktop.
xhprof usages
<?php
require_once(__DIR__.'/vendor/XhProf/xhprof_html/callgraph.php');
<?php
$enableXhprof = rand(1,100) == 1;
if ($enableXhprof) {
define("XHPROF_ROOT", __DIR__ . '/vendor/XhProf');
require_once(XHPROF_ROOT . '/xhprof_lib/utils/xhprof_lib.php');
require_once(XHPROF_ROOT . '/xhprof_lib/utils/xhprof_runs.php');
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
}
// ===========================SOME==============CODE==============================
if ($enableXhprof) {
$xhprofData = xhprof_disable();
$xhprofRuns = new XHProfRuns_Default();
$xhName = "xhprof_testing";
$xhUrl = 'xh_' . base64_encode('http' . (['HTTPS'] == 'on' ? 's' : '') . '://' .$_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI']);
$runId = $xhprofRuns->save_run($xhprofData, $xhUrl);
//Show links to profilers
$profilerUrl = 'http' . (['HTTPS'] == 'on' ? 's' : '') . '://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] .
'/%s?run=' . $runId . '&source=' . $xhUrl . '&' . http_build_query($_GET);
echo '<script>
console.log("XHPROF PROFILER: ' . sprintf($profilerUrl, 'profiler.php') . '")
console.log("XHPROF CALLGRAPH: ' . sprintf($profilerUrl, 'callgraph.php') . '")
console.log("XHPROF TOC: ' . sprintf($profilerUrl, 'xhproftoc.php') . '")
</script>';
}
<?php
require_once(__DIR__.'/vendor/XhProf/xhprof_html/index.php');
<?php
$dir = ini_get('xhprof.output_dir') ?: '/tmp';
$files = scandir($dir);
$runs = '';
$get = http_build_query($_GET);
foreach ($files as $file) {
if (is_file($dir . '/' . $file) && preg_match('#[a-zA-Z0-9]{13}\.xh_#', $file)) {
//54749cdcb0b40.xh_bW9sb2Rvc3QuYnovdGhlZmlyc3Rfbm
$run = explode('.', $file);
$runId = $run[0];
$runSource = $run[1];
$runUrl = base64_decode(str_replace('xh_', '', $runSource));
if (empty($runs[$runUrl])) {
$runs[$runUrl] = [];
}
$runs[$runUrl][] = ['id' => $runId, 'source' => $runSource, 'url' => $runUrl];
}
}
ksort($runs);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Toc</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<table class="table table-condensed table-hover">
<?php foreach ($runs as $url => $run): ?>
<tr>
<th><a style="color: #ff6944" href="<?= $url ?>"><?= $url ?></a></th>
</tr>
<?php foreach ($run as $r):
$profilerStr = './%s?run=' . $r['id'] . '&source=' . $r['source'] .'&sort=%s'. '&' . $get;
?>
<tr>
<td>id=<?= $r['id'] ?> ::
Profiler [
<a target="_blank" href="<?= sprintf($profilerStr,'profiler.php','ct')?>">calls</a> |
<a target="_blank" href="<?= sprintf($profilerStr,'profiler.php','wt')?>">Incl. Wall Time</a> |
<a target="_blank" href="<?= sprintf($profilerStr,'profiler.php','excl_wt')?>">Excl. Wall Time</a> |
<a target="_blank" href="<?= sprintf($profilerStr,'profiler.php','cpu')?>">Incl. CPU</a> |
<a target="_blank" href="<?= sprintf($profilerStr,'profiler.php','excl_cpu')?>">Excl. CPU</a> |
<a target="_blank" href="<?= sprintf($profilerStr,'profiler.php','mu')?>">Incl. MemUse</a> |
<a target="_blank" href="<?= sprintf($profilerStr,'profiler.php','excl_mu')?>">Excl. MemUse</a> |
<a target="_blank" href="<?= sprintf($profilerStr,'profiler.php','pmu')?>">Incl. PeakMemUse</a> |
<a target="_blank" href="<?= sprintf($profilerStr,'profiler.php','excl_pmu')?>">Excl. PeakMemUse</a>] ::
<a target="_blank" href="<?= sprintf($profilerStr,'callgraph.php','no')?>">Callgraph</a>
</td>
</tr>
<?php endforeach; ?>
<tr>
<td style="background-color: #e5e4e3"></td>
</tr>
<?php endforeach; ?>
</table>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment