Skip to content

Commit

Permalink
chore(profiler): optimize profiling loadings; add class Measure to hu…
Browse files Browse the repository at this point in the history
…manize different measures
  • Loading branch information
roxblnfk committed Jun 13, 2024
1 parent e777831 commit 344094d
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/Module/Profiler/XHProf/ProfileBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ private function dataToPayload(array $data): array
/** @var Tree<Edge> $tree */
$tree = new Tree();

!\array_key_exists('main()', $data) && \array_key_exists('value', $data) and $data['main()'] = $data['value'];
unset($data['value']);

foreach (\array_reverse($data, true) as $key => $value) {
Expand Down
4 changes: 3 additions & 1 deletion src/Proto/Frame/Profiler/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/
final class Payload implements \JsonSerializable
{
private ?Profile $profile = null;

/**
* @param PayloadType $type
* @param \Closure(): Profile $callsProvider
Expand Down Expand Up @@ -58,7 +60,7 @@ public static function fromArray(array $data, ?PayloadType $type = null): static

public function getProfile(): Profile
{
return ($this->callsProvider)();
return $this->profile ??= ($this->callsProvider)();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Sender/Console/Renderer/Binary.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Buggregator\Trap\ProtoType;
use Buggregator\Trap\Sender\Console\Renderer;
use Buggregator\Trap\Sender\Console\Support\Common;
use Buggregator\Trap\Sender\Console\Support\Files;
use Buggregator\Trap\Support\Measure;
use Symfony\Component\Console\Output\OutputInterface;

/**
Expand Down Expand Up @@ -70,7 +70,7 @@ public function render(OutputInterface $output, Frame $frame): void
$size = $frame->getSize();
Common::renderMetadata($output, [
'Time' => $frame->time,
'Size' => Files::normalizeSize($size) . ($size > 1024 ? \sprintf(' (%d bytes)', $size) : ''),
'Size' => Measure::memory($size) . ($size > 1024 ? \sprintf(' (%d bytes)', $size) : ''),
]);

if ($size === 0) {
Expand Down
24 changes: 22 additions & 2 deletions src/Sender/Console/Renderer/Profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Buggregator\Trap\ProtoType;
use Buggregator\Trap\Sender\Console\Renderer;
use Buggregator\Trap\Sender\Console\Support\Common;
use Buggregator\Trap\Sender\Console\Support\Tables;
use Buggregator\Trap\Support\Measure;
use Symfony\Component\Console\Output\OutputInterface;

/**
Expand Down Expand Up @@ -37,10 +39,28 @@ public function render(OutputInterface $output, Frame $frame): void
and $data['Time'] = new \DateTimeImmutable('@' . $metadata['date']);
isset($metadata['app_name']) and $data['App name'] = $metadata['app_name'];
isset($metadata['hostname']) and $data['Hostname'] = $metadata['hostname'];
isset($metadata['filename']) and $data['File name'] = $metadata['filename'];
isset($metadata['filename']) and $data['File name'] = $metadata['filename'] . (
isset($metadata['filesize']) && \is_int($metadata['filesize'])
? ' (' . Measure::memory($metadata['filesize']) . ')'
: ''
);
$data['Num edges'] = $profile->calls->count();

Common::renderMetadata($output, $data);
Common::renderTags($output, $profile->tags);
if ($profile->tags !== []) {
$output->writeln('');
Common::renderTags($output, $profile->tags);
$output->writeln('');
}

// Render peaks
$peaks = $profile->peaks;
Tables::renderKeyValueTable($output, 'Peak values', [
'Memory usage' => Measure::memory($peaks->mu),
'Peak memory usage' => Measure::memory($peaks->pmu),
'Wall time' => $peaks->wt,
'CPU time' => $peaks->cpu,
'Calls count' => $peaks->ct,
]);
}
}
3 changes: 2 additions & 1 deletion src/Sender/Console/Renderer/Smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Buggregator\Trap\Sender\Console\Renderer;
use Buggregator\Trap\Sender\Console\Support\Common;
use Buggregator\Trap\Sender\Console\Support\Files;
use Buggregator\Trap\Support\Measure;
use Symfony\Component\Console\Output\OutputInterface;

/**
Expand Down Expand Up @@ -88,7 +89,7 @@ public function render(OutputInterface $output, Frame $frame): void
\array_map(static fn($attach) => [
'CID' => $attach->getEmbeddingId(),
'Name' => $attach->getClientFilename(),
'Size' => Files::normalizeSize($attach->getSize()),
'Size' => Measure::memory($attach->getSize()),
'MIME' => $attach->getClientMediaType(),
], $embeddings),
'compact',
Expand Down
21 changes: 2 additions & 19 deletions src/Sender/Console/Support/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Buggregator\Trap\Sender\Console\Support;

use Buggregator\Trap\Support\Measure;
use Symfony\Component\Console\Output\OutputInterface;

/**
Expand Down Expand Up @@ -41,7 +42,7 @@ public static function renderFile(
: \str_pad(\substr($fileName, $dotPos + 1), 3, ' ', \STR_PAD_BOTH);

// File size
$sizeStr = self::normalizeSize($size) ?? 'unknown size';
$sizeStr = Measure::memory($size) ?? 'unknown size';

// Header with top border
$output->writeln("<bg=black;fg=magenta> ┌───┐</> <info>$fileName</info>");
Expand All @@ -58,22 +59,4 @@ public static function renderFile(
// MIME type
$output->writeln("<bg=black;fg=magenta> └───┘</> <fg=gray>$type</>");
}

/**
* @param int<0, max>|null $size
*/
public static function normalizeSize(?int $size): ?string
{
if ($size === null) {
return null;
}

$units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];
$power = (int) \floor(\log($size, 1024));
$float = $power > 0 ? \round($size / (1024 ** $power), 2) : $size;

\assert($power >= 0 && $power <= 5);

return \sprintf('%s %s', \number_format($float, 2), $units[$power]);
}
}
1 change: 1 addition & 0 deletions src/Service/FilesObserver/Converter/XHProf.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function convert(FileInfo $file): \Traversable
metadata: [
'hostname' => \gethostname(),
'filename' => $file->getName(),
'filesize' => $file->size,
],
calls: $data,
);
Expand Down
31 changes: 31 additions & 0 deletions src/Support/Measure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Buggregator\Trap\Support;

/**
* @internal
* @psalm-internal Buggregator\Trap
*/
final class Measure
{
/**
* @param int<0, max>|null $size
* @return non-empty-string|null
*/
public static function memory(?int $size): ?string
{
if ($size === null) {
return null;
}

$units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];
$power = (int) \floor(\log($size, 1024));
$float = $power > 0 ? \round($size / (1024 ** $power), 2) : $size;

\assert($power >= 0 && $power <= 5);

return \sprintf('%s %s', \number_format($float, 2), $units[$power]);
}
}

0 comments on commit 344094d

Please sign in to comment.