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

Normalize profiler record, fill missing keys #362

Merged
merged 5 commits into from
Oct 26, 2020
Merged
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
6 changes: 5 additions & 1 deletion external/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
$line = fgets($fp);
$data = json_decode($line, true);
if ($data) {
$saver->save($data);
try {
$saver->save($data);
} catch (Throwable $e) {
error_log($e);
}
}
}
fclose($fp);
8 changes: 8 additions & 0 deletions src/Xhgui/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ protected function _process()
$result = [];
foreach ($this->_data['profile'] as $name => $values) {
list($parent, $func) = $this->splitName($name);
// normalize, fill all missing keys
$values += [
'ct' => 0,
'wt' => 0,
'cpu' => 0,
'mu' => 0,
'pmu' => 0,
];

// Generate collapsed data.
if (isset($result[$func])) {
Expand Down
42 changes: 42 additions & 0 deletions src/Xhgui/Saver/NormalizingSaver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace XHGui\Saver;

use RuntimeException;

class NormalizingSaver implements SaverInterface
{
private $saver;

public function __construct(SaverInterface $saver)
{
$this->saver = $saver;
}

public function save(array $data, string $id = null): string
{
foreach ($data['profile'] as $index => &$profile) {
// skip empty profilings
if (!$profile) {
unset($data['profile'][$index]);
continue;
}

// normalize, fill all missing keys
$profile += [
'ct' => 0,
'wt' => 0,
'cpu' => 0,
'mu' => 0,
'pmu' => 0,
];
}
unset($profile);

if (!$data['profile']) {
throw new RuntimeException("Skipping to save empty profiling");
}

return $this->saver->save($data, $id);
}
}
3 changes: 2 additions & 1 deletion src/Xhgui/ServiceContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Slim\Views\Twig;
use XHGui\Db\PdoRepository;
use XHGui\Middleware\RenderMiddleware;
use XHGui\Saver\NormalizingSaver;
use XHGui\Searcher\MongoSearcher;
use XHGui\Searcher\PdoSearcher;
use XHGui\Twig\TwigExtension;
Expand Down Expand Up @@ -168,7 +169,7 @@ protected function _services()
$this['saver'] = static function ($c) {
$saver = $c['config']['save.handler'];

return $c["saver.$saver"];
return new NormalizingSaver($c["saver.$saver"]);
};
}

Expand Down