Fixed performance regression in HTTP API
This commit is contained in:
@@ -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(),
|
||||
|
||||
@@ -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' => []
|
||||
];
|
||||
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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.');
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
21
src/Libs/Middlewares/AddTimingMiddleware.php
Normal file
21
src/Libs/Middlewares/AddTimingMiddleware.php
Normal 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'
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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())
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user