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 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; + } +}