Код: Выделить всё
class ApiLoggerListener
{
#[AsEventListener(event: KernelEvents::REQUEST)]
public function onRequest(RequestEvent $event): void
{
$request->attributes->set('_timestamp', microtime(true));
}
#[AsEventListener(event: KernelEvents::RESPONSE)]
public function onResponse(ResponseEvent $event): void
{
$now = microtime(true);
$response = $event->getResponse();
$request = $event->getRequest();
$route = $request->attributes->get('_route');
$dt = $request->attributes->get('_timestamp');
//...
$executionStart = $request->attributes->get('start-execution', null);
$executionEnd = $request->attributes->get('end-execution', null);
if ($executionStart && $executionEnd && $dt) {
$inputTime = (int)(($executionStart - $dt) * 1000); // ms took to reach controller after onRequest()
$outputTime = (int)(($now - $executionEnd) * 1000); // ms took to reach onResponse() after controller
$executionTime = (int)(($executionEnd - $executionStart) * 1000); // ms contoller took to handle request and produce response
$timing = "input=$inputTime;ouput=$outputTime;execution=$executionTime;";
$response->headers->set('Server-Timing', $timing);
}
}
}
< /code>
и я добавил это в Controller: < /p>
#[Route('/foo', name: 'foo', methods: [Request::METHOD_POST)]
public function myGloriousAction(
Request $request,
// ...
): ApiResponse {
$request->attributes->set('start-execution', microtime(true));
// $result = ...
$request->attributes->set('end-execution', microtime(true));
return new ApiResponse($result);
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... le-request