From 9bfb8ff1494d1087fc9cc147582fb588fd9b72ae Mon Sep 17 00:00:00 2001 From: pana1990 Date: Thu, 10 Nov 2022 19:55:41 +0100 Subject: [PATCH 1/2] add allow memory max size --- examples/slim/index.php | 15 ++++++++++---- src/Bridge/Slim/SlimPhpWebProfilerBuilder.php | 17 ++++++++++++++++ src/Traceables/LoggerTraceable.php | 9 ++++++++- src/Traceables/PdoTraceable.php | 7 +++++++ src/Traits/BufferSize.php | 20 +++++++++++++++++++ 5 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 src/Traits/BufferSize.php diff --git a/examples/slim/index.php b/examples/slim/index.php index d48e9fa..26271c7 100644 --- a/examples/slim/index.php +++ b/examples/slim/index.php @@ -20,17 +20,24 @@ $traceableLogger = new LoggerTraceable($log); SlimPhpWebProfilerBuilder::fromApp($app) + ->withBufferSizeInMb(1) ->withPdo($pdoTraceable) ->withLogger($traceableLogger) ->build(); $app->get('/', function (Request $request, Response $response) use ($traceableLogger, $pdoTraceable) { - $response->getBody()->write('Hello world!'); - - $traceableLogger->error('This is an error message'); + ini_set('memory_limit', '2048M'); + $response->getBody()->write(ini_get('memory_limit')); $pdoTraceable->exec('INSERT INTO test (title) VALUES ("test");'); - $pdoTraceable->exec('SELECT * FROM test;'); + + foreach (range(0, 10000) as $item) { + $pdoTraceable->exec('SELECT * FROM test WHERE title="test" limit 1 OFFSET 0;'); + $traceableLogger->error('This is an error message'); + $traceableLogger->info('This is an error message'); + $traceableLogger->warning('This is an error message'); + $traceableLogger->error('This is an error message'); + } return $response; }); diff --git a/src/Bridge/Slim/SlimPhpWebProfilerBuilder.php b/src/Bridge/Slim/SlimPhpWebProfilerBuilder.php index 66b185a..1a5985e 100644 --- a/src/Bridge/Slim/SlimPhpWebProfilerBuilder.php +++ b/src/Bridge/Slim/SlimPhpWebProfilerBuilder.php @@ -18,6 +18,7 @@ use WebProfiler\PhpWebProfilerBuilder; use WebProfiler\Traceables\LoggerTraceable; use WebProfiler\Traceables\RequestTraceable; +use WebProfiler\Traits\BufferSize; final class SlimPhpWebProfilerBuilder implements PhpWebProfilerBuilder { @@ -25,6 +26,7 @@ final class SlimPhpWebProfilerBuilder implements PhpWebProfilerBuilder private ?LoggerTraceable $logger = null; private ?PdoTraceableInterface $pdo = null; private ?StorageInterface $storage = null; + private int $bufferSizeInBytes = 6144; private function __construct( private App $app, @@ -44,6 +46,10 @@ public function withPdo(PdoTraceableInterface $pdo): self { $this->pdo = $pdo; + if (in_array(BufferSize::class, class_uses($pdo), true)) { + $pdo->setBufferSize($this->bufferSizeInBytes); + } + return $this; } @@ -58,6 +64,10 @@ public function withLogger(LoggerTraceable $logger): self { $this->logger = $logger; + if (in_array(BufferSize::class, class_uses($logger), true)) { + $logger->setBufferSize($this->bufferSizeInBytes); + } + return $this; } @@ -68,6 +78,13 @@ public function withStorage(StorageInterface $storage): self return $this; } + public function withBufferSizeInMb(int $bufferSizeInMb): self + { + $this->bufferSizeInBytes = $bufferSizeInMb * (1024 * 1024); + + return $this; + } + public function build(): PhpWebProfiler { $this->setStorage(); diff --git a/src/Traceables/LoggerTraceable.php b/src/Traceables/LoggerTraceable.php index a4a7a9e..1ffe7d2 100644 --- a/src/Traceables/LoggerTraceable.php +++ b/src/Traceables/LoggerTraceable.php @@ -6,9 +6,12 @@ use Psr\Log\LoggerInterface; use WebProfiler\Contracts\LoggerTraceableInterface; +use WebProfiler\Traits\BufferSize; final class LoggerTraceable implements LoggerInterface, LoggerTraceableInterface { + use BufferSize; + private array $logs = []; public function __construct(private LoggerInterface $logger) @@ -20,8 +23,12 @@ public function logs(): array return $this->logs; } - public function addLog(string $message, string $type, array $context = []) + public function addLog(string $message, string $type, array $context = []): void { + if ($this->isBufferSizeExceeded()) { + return; + } + $this->logs[] = [ $type, $message, diff --git a/src/Traceables/PdoTraceable.php b/src/Traceables/PdoTraceable.php index 1a04598..e210e88 100644 --- a/src/Traceables/PdoTraceable.php +++ b/src/Traceables/PdoTraceable.php @@ -5,9 +5,12 @@ namespace WebProfiler\Traceables; use WebProfiler\Contracts\PdoTraceableInterface; +use WebProfiler\Traits\BufferSize; final class PdoTraceable extends \PDO implements PdoTraceableInterface { + use BufferSize; + private array $statements = []; public function exec(string $statement) @@ -19,6 +22,10 @@ public function exec(string $statement) $duration = microtime(true) - $timeStart; + if ($this->isBufferSizeExceeded()) { + return; + } + $this->addStatement([ 'time' => $time, 'duration' => $duration, diff --git a/src/Traits/BufferSize.php b/src/Traits/BufferSize.php new file mode 100644 index 0000000..f6738a4 --- /dev/null +++ b/src/Traits/BufferSize.php @@ -0,0 +1,20 @@ +bufferSize < memory_get_usage(); + } + + public function setBufferSize(int $bufferSize): void + { + $this->bufferSize = $bufferSize; + } +} From 487192d537eecba4a8ccbec9109cc9352e3e9cd6 Mon Sep 17 00:00:00 2001 From: pana1990 Date: Thu, 10 Nov 2022 19:58:15 +0100 Subject: [PATCH 2/2] fix workflow --- .github/workflows/lint.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 45fa080..b067a52 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -1,4 +1,8 @@ -on: [push, pull_request] +on: + push: + branches: + - main + pull_request: name: Lint