Skip to content

Instantly share code, notes, and snippets.

@hussainweb
Last active July 15, 2021 09:49
Show Gist options
  • Save hussainweb/96c6df5980a068d40cf78b1feaee97a0 to your computer and use it in GitHub Desktop.
Save hussainweb/96c6df5980a068d40cf78b1feaee97a0 to your computer and use it in GitHub Desktop.
Import XHProf runs into xhgui
<?php
// Run this as follows:
// php import-xhprof-to-xhgui.php -d /dir/to/xhprof/runs/
//
// Based on https://github.com/perftools/xhgui/blob/master/external/import.php
if (!defined('XHGUI_ROOT_DIR')) {
require __DIR__ . '/src/bootstrap.php';
}
$options = getopt('d:');
if (!isset($options['d'])) {
throw new InvalidArgumentException('You should define a directory to be loaded');
} else {
$directory = $options['d'];
}
if (!is_dir($directory)) {
throw new InvalidArgumentException($directory.' isn\'t a directory');
}
$container = Xhgui_ServiceContainer::instance();
$saver = $container['saver.mongo'];
$iterator = new GlobIterator($directory . '/*.xhprof');
while ($iterator->valid()) {
$fileinfo = $iterator->current();
$file = $fileinfo->getPathname();
echo $file . PHP_EOL;
$data = unserialize(file_get_contents($file));
if ($data) {
$saver->save([
'_id' => new MongoId(),
'meta' => ['request_ts' => ['sec' => $fileinfo->getMTime()]],
'profile' => $data,
]);
}
$iterator->next();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment