Skip to content

Commit

Permalink
[Hotfix] Fixed optional dependency to Stopwatch (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
maelanleborgne authored Sep 9, 2024
1 parent 51ea807 commit 61b13e0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
14 changes: 7 additions & 7 deletions src/Debug/Builder/TraceableScreenshotBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
final class TraceableScreenshotBuilder implements ScreenshotBuilderInterface
{
/**
* @var list<array{'time': float, 'memory': int, 'size': int<0, max>|null, 'fileName': string|null, 'calls': list<array{'method': string, 'class': class-string<ScreenshotBuilderInterface>, 'arguments': array<mixed>}>}>
* @var list<array{'time': float|null, 'memory': int|null, 'size': int<0, max>|null, 'fileName': string|null, 'calls': list<array{'method': string, 'class': class-string<ScreenshotBuilderInterface>, 'arguments': array<mixed>}>}>
*/
private array $screenshots = [];

Expand All @@ -24,7 +24,7 @@ final class TraceableScreenshotBuilder implements ScreenshotBuilderInterface

public function __construct(
private readonly ScreenshotBuilderInterface $inner,
private readonly Stopwatch $stopwatch,
private readonly Stopwatch|null $stopwatch,
) {
}

Expand All @@ -33,14 +33,14 @@ public function generate(): GotenbergFileResult
$name = self::$count.'.'.$this->inner::class.'::'.__FUNCTION__;
++self::$count;

$swEvent = $this->stopwatch->start($name, 'gotenberg.generate_screenshot');
$swEvent = $this->stopwatch?->start($name, 'gotenberg.generate_screenshot');
$response = $this->inner->generate();
$swEvent->stop();
$swEvent?->stop();

$this->screenshots[] = [
'calls' => $this->calls,
'time' => $swEvent->getDuration(),
'memory' => $swEvent->getMemory(),
'time' => $swEvent?->getDuration(),
'memory' => $swEvent?->getMemory(),
'status' => $response->getStatusCode(),
'size' => $response->getContentLength(),
'fileName' => $response->getFileName(),
Expand Down Expand Up @@ -72,7 +72,7 @@ public function __call(string $name, array $arguments): mixed
}

/**
* @return list<array{'time': float, 'memory': int, 'size': int<0, max>|null, 'fileName': string|null, 'calls': list<array{'class': class-string<ScreenshotBuilderInterface>, 'method': string, 'arguments': array<mixed>}>}>
* @return list<array{'time': float|null, 'memory': int|null, 'size': int<0, max>|null, 'fileName': string|null, 'calls': list<array{'class': class-string<ScreenshotBuilderInterface>, 'method': string, 'arguments': array<mixed>}>}>
*/
public function getFiles(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/CompilerPass/GotenbergPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private function makeScreenshotBuildersTraceable(ContainerBuilder $container): v
->setShared(false)
->setArguments([
'$inner' => new Reference('.inner'),
'$stopwatch' => new Reference('debug.stopwatch'),
'$stopwatch' => new Reference('debug.stopwatch', ContainerInterface::NULL_ON_INVALID_REFERENCE),
])
;
}
Expand Down
32 changes: 21 additions & 11 deletions templates/Collector/sensiolabs_gotenberg.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,20 @@
<span class="label">Files generated</span>
</div>
<div class="metric">
<span class="value">{{ '%.0f'|format(collector.requestTotalTime) }} <span class="unit">ms</span></span>
{% if collector.requestTotalTime is null %}
<span class="value" title="To enable elapsed time tracking, the Stopwatch component is required. Try running 'composer require symfony/stopwatch'.">n/a</span>
{% else %}
<span class="value">{{ '%.0f'|format(collector.requestTotalTime) }} <span class="unit">ms</span></span>
{% endif %}
<span class="label">Total time</span>
</div>
<div class="metric">
<span class="value">{{ '%.1f'|format(collector.requestTotalMemory / 1024 / 1024) }} <span
class="unit">MiB</span></span>
{% if collector.requestTotalMemory is null %}
<span class="value" title="To enable memory usage tracking, the Stopwatch component is required. Try running 'composer require symfony/stopwatch'.">n/a</span>
{% else %}
<span class="value">{{ '%.1f'|format(collector.requestTotalMemory / 1024 / 1024) }} <span
class="unit">MiB</span></span>
{% endif %}
<span class="label">Total memory</span>
</div>
<div class="metric">
Expand Down Expand Up @@ -113,16 +121,18 @@
</div>
</td>
<td class="font-normal">
<span class="value">
{{ '%.0f'|format(file.time) }}
<span class="unit">ms</span>
</span>
{% if file.time is null %}
<span class="value" title="To enable elapsed time tracking, the Stopwatch component is required. Try running 'composer require symfony/stopwatch'.">n/a</span>
{% else %}
<span class="value">{{ '%.0f'|format(file.time) }} <span class="unit">ms</span></span>
{% endif %}
</td>
<td class="font-normal">
<span class="value">
{{ '%.1f'|format(file.memory / 1024 / 1024) }}
<span class="unit">MiB</span>
</span>
{% if file.memory is null %}
<span class="value" title="To enable memory usage tracking, the Stopwatch component is required. Try running 'composer require symfony/stopwatch'.">n/a</span>
{% else %}
<span class="value">{{ '%.1f'|format(file.memory / 1024 / 1024) }} <span class="unit">MiB</span></span>
{% endif %}
</td>
<td class="font-normal">
{% set size = file.size %}
Expand Down

0 comments on commit 61b13e0

Please sign in to comment.