Fixed performance regression in HTTP API

This commit is contained in:
ArabCoders
2025-01-31 18:24:47 +03:00
parent 1d2a1da913
commit 44d3b32f3c
7 changed files with 56 additions and 9 deletions

View File

@@ -3,12 +3,14 @@
declare(strict_types=1);
use App\Libs\Middlewares\{AddCorsMiddleware,
AddTimingMiddleware,
APIKeyRequiredMiddleware,
NoAccessLogMiddleware,
ParseJsonBodyMiddleware,
ProfilerMiddleware};
return static fn(): array => [
fn() => new AddTimingMiddleware(),
fn() => new ProfilerMiddleware(),
fn() => new APIKeyRequiredMiddleware(),
fn() => new ParseJsonBodyMiddleware(),

View File

@@ -155,7 +155,7 @@ return (function () {
'save' => (bool)env('WS_PROFILER_SAVE', true),
'path' => env('WS_PROFILER_PATH', fn() => ag($config, 'tmpDir') . '/profiler'),
'collector' => env('WS_PROFILER_COLLECTOR', null),
'flags' => ['PROFILER_CPU_PROFILING', 'PROFILER_MEMORY_PROFILING', 'NO_BUILTINS'],
'flags' => ['PROFILER_CPU_PROFILING', 'PROFILER_MEMORY_PROFILING'],
'config' => []
];

View File

@@ -2,6 +2,10 @@
declare(strict_types=1);
if (!defined('APP_START')) {
define('APP_START', microtime(true));
}
if (!defined('BASE_MEMORY')) {
define('BASE_MEMORY', memory_get_usage());
}

View File

@@ -20,6 +20,7 @@ use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Yaml;
use Throwable;
class Command extends BaseCommand
{
@@ -82,7 +83,12 @@ class Command extends BaseCommand
$profiler = new \Xhgui\Profiler\Profiler(Config::get('profiler.config', []));
$profiler->enable(Config::get('profiler.flags', null));
$status = $this->runCommand($input, $output);
$data = $profiler->disable();
try {
$data = $profiler->disable();
} catch (Throwable) {
$data = [];
}
if (empty($data)) {
throw new RuntimeException('The profiler run was unsuccessful. No data was returned.');

View File

@@ -22,13 +22,11 @@ class RouterStrategy extends ApplicationStrategy implements OptionsHandlerInterf
'Allow' => implode(', ', $methods),
];
$response = api_response(Status::NO_CONTENT, headers: $headers);
if ('cors' === ag($_SERVER, 'HTTP_SEC_FETCH_MODE')) {
return fn(): iResponse => addCors($response, $headers, $methods);
return fn(): iResponse => addCors(api_response(Status::NO_CONTENT, headers: $headers), $headers, $methods);
}
return fn(): iResponse => $response;
return fn(): iResponse => api_response(Status::NO_CONTENT, headers: $headers);
}
/**

View File

@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace App\Libs\Middlewares;
use Psr\Http\Message\ResponseInterface as iResponse;
use Psr\Http\Message\ServerRequestInterface as iRequest;
use Psr\Http\Server\MiddlewareInterface as iMiddleware;
use Psr\Http\Server\RequestHandlerInterface as iHandler;
final class AddTimingMiddleware implements iMiddleware
{
public function process(iRequest $request, iHandler $handler): iResponse
{
return $handler->handle($request)->withHeader(
'X-Application-Finished-In',
round(microtime(true) - APP_START, 6) . 's'
);
}
}

View File

@@ -12,6 +12,7 @@ use Psr\Http\Message\ServerRequestInterface as iRequest;
use Psr\Http\Server\MiddlewareInterface as iMiddleware;
use Psr\Http\Server\RequestHandlerInterface as iHandler;
use Random\RandomException;
use Throwable;
readonly final class ProfilerMiddleware implements iMiddleware
{
@@ -34,12 +35,27 @@ readonly final class ProfilerMiddleware implements iMiddleware
return $handler->handle($request->withHeader('X-Profiled', 'No'));
}
$profiler = new \Xhgui\Profiler\Profiler(ag($config, 'config', []));
$profiler->enable(ag($config, 'flags', null));
try {
$profiler = new \Xhgui\Profiler\Profiler(ag($config, 'config', []));
$profiler->enable(ag($config, 'flags', []));
} catch (Throwable) {
return $handler->handle($request);
}
$response = $handler->handle($request);
try {
$data = $profiler->disable();
} catch (Throwable) {
$data = [];
}
if (empty($data)) {
return $response;
}
$data = ag_set(
$profiler->disable(),
$data,
'meta.id',
ag($request->getServerParams(), 'X_REQUEST_ID', fn() => generateUUID())
);