-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Add base for profiler (#27)
- Loading branch information
Showing
22 changed files
with
694 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
use Sensiolabs\GotenbergBundle\DataCollector\GotenbergDataCollector; | ||
use Sensiolabs\GotenbergBundle\Debug\TraceableGotenberg; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use function Symfony\Component\DependencyInjection\Loader\Configurator\abstract_arg; | ||
use function Symfony\Component\DependencyInjection\Loader\Configurator\service; | ||
use function Symfony\Component\DependencyInjection\Loader\Configurator\tagged_locator; | ||
|
||
return static function (ContainerConfigurator $container): void { | ||
$services = $container->services(); | ||
|
||
$services->set('sensiolabs_gotenberg.traceable', TraceableGotenberg::class) | ||
->decorate('sensiolabs_gotenberg') | ||
->args([ | ||
new Reference('.inner'), | ||
]) | ||
; | ||
|
||
$services->set('sensiolabs_gotenberg.data_collector', GotenbergDataCollector::class) | ||
->args([ | ||
service('sensiolabs_gotenberg'), | ||
tagged_locator('sensiolabs_gotenberg.builder'), | ||
abstract_arg('All default options will be set through the configuration.'), | ||
]) | ||
->tag('data_collector', ['template' => '@SensiolabsGotenberg/Collector/sensiolabs_gotenberg.html.twig', 'id' => 'sensiolabs_gotenberg']) | ||
; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
<?php | ||
|
||
namespace Sensiolabs\GotenbergBundle\DataCollector; | ||
|
||
use Sensiolabs\GotenbergBundle\Builder\PdfBuilderInterface; | ||
use Sensiolabs\GotenbergBundle\Debug\Builder\TraceablePdfBuilder; | ||
use Sensiolabs\GotenbergBundle\Debug\TraceableGotenberg; | ||
use Symfony\Component\DependencyInjection\ServiceLocator; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\DataCollector\DataCollector; | ||
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface; | ||
use Symfony\Component\VarDumper\Caster\ArgsStub; | ||
use Symfony\Component\VarDumper\Cloner\Data; | ||
|
||
final class GotenbergDataCollector extends DataCollector implements LateDataCollectorInterface | ||
{ | ||
/** | ||
* @param ServiceLocator<PdfBuilderInterface> $builders | ||
* @param array<mixed> $defaultOptions | ||
*/ | ||
public function __construct( | ||
private readonly TraceableGotenberg $traceableGotenberg, | ||
private readonly ServiceLocator $builders, | ||
private readonly array $defaultOptions, | ||
) { | ||
} | ||
|
||
public function collect(Request $request, Response $response, \Throwable|null $exception = null): void | ||
{ | ||
$this->data['request_total_memory'] = 0; | ||
$this->data['request_total_size'] = 0; | ||
$this->data['request_total_time'] = 0; | ||
$this->data['request_count'] = 0; | ||
$this->data['builders'] = []; | ||
|
||
foreach ($this->builders->getProvidedServices() as $id => $type) { | ||
$builder = $this->builders->get($id); | ||
|
||
if ($builder instanceof TraceablePdfBuilder) { | ||
$builder = $builder->getInner(); | ||
} | ||
|
||
if (str_starts_with($id, '.sensiolabs_gotenberg.builder.')) { | ||
[$id] = sscanf($id, '.sensiolabs_gotenberg.builder.%s'); | ||
} | ||
|
||
$this->data['builders'][$id] = [ | ||
'class' => $builder::class, | ||
'default_options' => $this->defaultOptions[$id] ?? [], | ||
'pdfs' => [], | ||
]; | ||
} | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return 'sensiolabs_gotenberg'; | ||
} | ||
|
||
public function lateCollect(): void | ||
{ | ||
/** | ||
* @var string $id | ||
* @var TraceablePdfBuilder $builder | ||
*/ | ||
foreach ($this->traceableGotenberg->getBuilders() as [$id, $builder]) { | ||
$this->data['builders'][$id]['pdfs'] = array_merge( | ||
$this->data['builders'][$id]['pdfs'], | ||
array_map(function (array $request): array { | ||
$this->data['request_total_time'] += $request['time']; | ||
$this->data['request_total_memory'] += $request['memory']; | ||
$this->data['request_total_size'] += $request['size'] ?? 0; | ||
|
||
return [ | ||
'time' => $request['time'], | ||
'memory' => $request['memory'], | ||
'size' => $this->formatSize($request['size'] ?? 0), | ||
'fileName' => $request['fileName'], | ||
'calls' => array_map(function (array $call): array { | ||
return [ | ||
'method' => $call['method'], | ||
'stub' => $this->cloneVar(new ArgsStub($call['arguments'], $call['method'], $call['class'])), | ||
]; | ||
}, $request['calls']), | ||
]; | ||
}, $builder->getPdfs()), | ||
); | ||
|
||
$this->data['request_count'] += \count($builder->getPdfs()); | ||
} | ||
} | ||
|
||
/** | ||
* @param int<0, max> $size | ||
* | ||
* @return array{float, string} | ||
*/ | ||
private function formatSize(int $size): array | ||
{ | ||
return match (true) { | ||
($size / 1024 < 1) => [$size, 'B'], | ||
($size / (1024 ** 2) < 1) => [round($size / 1024, 2), 'kB'], | ||
($size / (1024 ** 3) < 1) => [round($size / (1024 ** 2), 2), 'MB'], | ||
($size / (1024 ** 4) < 1) => [round($size / (1024 ** 3), 2), 'GB'], | ||
($size / (1024 ** 5) < 1) => [round($size / (1024 ** 4), 2), 'TB'], | ||
default => throw new \LogicException('File too big'), | ||
}; | ||
} | ||
|
||
/** | ||
* @return array<string, array{ | ||
* 'class': string, | ||
* 'default_options': array<mixed>, | ||
* 'pdfs': list<array{ | ||
* 'time': float, | ||
* 'memory': int, | ||
* 'size': int<0, max>|null, | ||
* 'fileName': string, | ||
* 'calls': list<array{ | ||
* 'method': string, | ||
* 'stub': Data | ||
* }> | ||
* }> | ||
* }> | ||
*/ | ||
public function getBuilders(): array | ||
{ | ||
return $this->data['builders'] ?? []; | ||
} | ||
|
||
public function getRequestCount(): int | ||
{ | ||
return $this->data['request_count'] ?? 0; | ||
} | ||
|
||
public function getRequestTotalTime(): int|float | ||
{ | ||
return $this->data['request_total_time'] ?? 0.0; | ||
} | ||
|
||
public function getRequestTotalMemory(): int | ||
{ | ||
return $this->data['request_total_memory'] ?? 0; | ||
} | ||
|
||
/** | ||
* @return array{float, string} | ||
*/ | ||
public function getRequestTotalSize(): array | ||
{ | ||
return $this->formatSize($this->data['request_total_size'] ?? 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
namespace Sensiolabs\GotenbergBundle\Debug\Builder; | ||
|
||
use Sensiolabs\GotenbergBundle\Builder\PdfBuilderInterface; | ||
use Sensiolabs\GotenbergBundle\Client\PdfResponse; | ||
use Symfony\Component\Stopwatch\Stopwatch; | ||
|
||
final class TraceablePdfBuilder implements PdfBuilderInterface | ||
{ | ||
/** | ||
* @var list<array{'time': float, 'memory': int, 'size': int<0, max>|null, 'fileName': string, 'calls': list<array{'method': string, 'class': class-string<PdfBuilderInterface>, 'arguments': array<mixed>}>}> | ||
*/ | ||
private array $pdfs = []; | ||
|
||
/** | ||
* @var list<array{'class': class-string<PdfBuilderInterface>, 'method': string, 'arguments': array<mixed>}> | ||
*/ | ||
private array $calls = []; | ||
|
||
private int $totalGenerated = 0; | ||
|
||
private static int $count = 0; | ||
|
||
public function __construct( | ||
private readonly PdfBuilderInterface $inner, | ||
private readonly Stopwatch $stopwatch, | ||
) { | ||
} | ||
|
||
public function generate(): PdfResponse | ||
{ | ||
$name = self::$count.'.'.$this->inner::class.'::'.__FUNCTION__; | ||
++self::$count; | ||
|
||
$swEvent = $this->stopwatch->start($name, 'gotenberg.generate_pdf'); | ||
$response = $this->inner->generate(); | ||
$swEvent->stop(); | ||
|
||
$fileName = 'Unknown.pdf'; | ||
if ($response->headers->has('Content-Disposition')) { | ||
$matches = []; | ||
|
||
/* @see https://onlinephp.io/c/c2606 */ | ||
\preg_match('#[^;]*;\sfilename="?(?P<fileName>[^"]*)"?#', $response->headers->get('Content-Disposition', ''), $matches); | ||
$fileName = $matches['fileName']; | ||
} | ||
|
||
$lengthInBytes = null; | ||
if ($response->headers->has('Content-Length')) { | ||
$lengthInBytes = \abs((int) $response->headers->get('Content-Length')); | ||
} | ||
|
||
$this->pdfs[] = [ | ||
'calls' => $this->calls, | ||
'time' => $swEvent->getDuration(), | ||
'memory' => $swEvent->getMemory(), | ||
'size' => $lengthInBytes, | ||
'fileName' => $fileName, | ||
]; | ||
|
||
++$this->totalGenerated; | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* @param array<mixed> $arguments | ||
*/ | ||
public function __call(string $name, array $arguments): mixed | ||
{ | ||
$result = $this->inner->$name(...$arguments); | ||
|
||
$this->calls[] = [ | ||
'class' => $this->inner::class, | ||
'method' => $name, | ||
'arguments' => $arguments, | ||
]; | ||
|
||
if ($result === $this->inner) { | ||
return $this; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* @return list<array{'time': float, 'memory': int, 'size': int<0, max>|null, 'fileName': string, 'calls': list<array{'class': class-string<PdfBuilderInterface>, 'method': string, 'arguments': array<mixed>}>}> | ||
*/ | ||
public function getPdfs(): array | ||
{ | ||
return $this->pdfs; | ||
} | ||
|
||
public function getInner(): PdfBuilderInterface | ||
{ | ||
return $this->inner; | ||
} | ||
} |
Oops, something went wrong.