Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.19]: Add export to cachegrind format support #302

Draft
wants to merge 4 commits into
base: 0.19.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"description": "A web based interface for viewing profile data collected by XHProf.",
"license": "MIT",
"autoload": {
"psr-4": {
"Tideways\\Xhprof\\": "src/"
},
"psr-0": {
"Xhgui_": "src/"
}
Expand Down
35 changes: 35 additions & 0 deletions src/Xhgui/Controller/Export.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Slim\Slim;
use Tideways\Xhprof\CachegrindConverter;

class Xhgui_Controller_Export extends Xhgui_Controller
{
/** @var CachegrindConverter */
private $converter;

/** @var Xhgui_Searcher_Interface */
private $searcher;

public function __construct(Slim $app, Xhgui_Searcher_Interface $searcher)
{
parent::__construct($app);
$this->searcher = $searcher;
$this->converter = new CachegrindConverter();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we give a helpful error message when this class doesn't exist? Perhaps with installation instructions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My hope was that @beberlei replies to my request to make it separate composer package:

}

public function cachegrind()
{
$request = $this->app->request();
$id = $request->get('id');

$profile = $this->searcher->get($id);
$output = $this->converter->convertToCachegrind($profile->toArray()['profile']);

$response = $this->app->response();
$response['Content-Type'] = 'application/octet-stream';
$response['Cache-Control'] = 'public, max-age=60, must-revalidate';
$response['Content-Disposition'] = sprintf('attachment; filename=cachegrind-%s.out', $id);
$response->body($output);
}
}
5 changes: 4 additions & 1 deletion src/Xhgui/ServiceContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ protected function _controllers()
$this['importController'] = function ($c) {
return new Xhgui_Controller_Import($c['app'], $c['saver'], $c['config']['upload.token']);
};
}

$this['exportController'] = function ($c) {
return new Xhgui_Controller_Export($c['app'], $c['searcher']);
};
}
}
7 changes: 7 additions & 0 deletions src/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@
$controller->import();
})->name('run.import');

// Export
$app->get('/export/cachegrind', function () use ($di) {
/** @var Xhgui_Controller_Export $controller */
$controller = $di['exportController'];
$controller->cachegrind();
})->name('export.cachegrind');

// Watch function routes.
$app->get('/watch', function () use ($di, $app) {
$app->controller = $di['watchController'];
Expand Down
1 change: 1 addition & 0 deletions src/templates/runs/view.twig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<li><strong>CPU Time</strong> {{ result.get('main()', 'cpu')|as_time }}</li>
<li><strong>Memory Usage</strong> {{ result.get('main()', 'mu')|as_bytes }}</li>
<li><strong>Peak Memory Usage</strong> {{ result.get('main()', 'pmu')|as_bytes }}</li>
<li><a href="{{ url('export.cachegrind', {id: result.id|trim }) }}"><strong>Download as Cachegrind</strong></a></li>

<li class="nav-header">GET</li>
<li>{{ helpers.property_list('GET', result.meta('get')) }}</li>
Expand Down