Skip to content

PHP

Terminal window
echo 'memory_limit = 256M' >> /usr/local/etc/php/conf.d/docker-php-memlimit.ini;

Reset FrankenPHP (supervisord will relaunch the service)

Section titled “Reset FrankenPHP (supervisord will relaunch the service)”
Terminal window
pkill -9 -f /usr/local/bin/frankenphp
$pattern = '/\[(?P<datetime>\S+)]\s+(?P<channel>\S+)\.(?P<level_name>\S+):\s+\[(?P<log_id>\S+)\s+(?P<action>.+?)\s+(?P<class>\S+)]\s+(?P<data>\S+)/';
$matches = [];
preg_match($pattern, $log, $matches);
dump($matches); // [
// 'datetime' => '2022-01-01',
// 'channel' => 'app',
// 'level_name' => 'INFO',
// 'log_id' => '123',
// 'action' => 'Hello world',
// 'class' => 'App\Hello',
// 'data' => 'Hello world!'
//]
debug_backtrace(2);
class MemoryUsageService
{
private int $startMemory;
private float $startTime;
public function startMonitoring(): void
{
$this->startMemory = memory_get_usage();
$this->startTime = microtime(true);
}
public function endMonitoring(): void
{
echo 'Execution time: '.$this->bytesToMegabytes(microtime(true) - $this->startTime)." seconds\n";
echo 'Memory used: '.$this->bytesToMegabytes(memory_get_usage() - $this->startMemory)." MB\n";
echo 'Peak memory usage: '.$this->bytesToMegabytes(memory_get_peak_usage())." MB\n";
}
private function bytesToMegabytes(int $bytes, int $precision = 2): float
{
return round($bytes / 1048576, $precision);
}
}
class MyClassToMonitor
{
public function __construct(
private MemoryUsageService $memoryUsageService
) {
}
public function myMethod(): void
{
$this->memoryUsageService->startMonitoring();
// Your code here
$this->memoryUsageService->endMonitoring();
}
}