first commit
Some checks failed
Build / run (push) Has been cancelled

This commit is contained in:
maher
2025-10-29 11:42:25 +01:00
commit 703f50a09d
4595 changed files with 385164 additions and 0 deletions

View File

@@ -0,0 +1,90 @@
<?php
namespace Common\ServerTiming;
use Symfony\Component\Stopwatch\Stopwatch;
class ServerTiming
{
protected array $finishedEvents = [];
protected array $startedEvents = [];
public function __construct(protected Stopwatch $stopwatch)
{
}
public function addMetric(string $metric)
{
$this->finishedEvents[$metric] = null;
return $this;
}
public function hasStartedEvent(string $key): bool
{
return array_key_exists($key, $this->startedEvents);
}
public function measure(string $key): static
{
if (!$this->hasStartedEvent($key)) {
return $this->start($key);
}
return $this->stop($key);
}
public function start(string $key): static
{
$this->stopwatch->start($key);
$this->startedEvents[$key] = true;
return $this;
}
public function stop(string $key): static
{
if ($this->stopwatch->isStarted($key)) {
$event = $this->stopwatch->stop($key);
$this->setDuration($key, $event->getDuration());
unset($this->startedEvents[$key]);
}
return $this;
}
public function stopAllUnfinishedEvents(): void
{
foreach (array_keys($this->startedEvents) as $startedEventName) {
$this->stop($startedEventName);
}
}
public function setDuration(string $key, $duration): static
{
if (is_callable($duration)) {
$this->start($key);
call_user_func($duration);
$this->stop($key);
} else {
$this->finishedEvents[$key] = $duration;
}
return $this;
}
public function getDuration(string $key)
{
return $this->finishedEvents[$key] ?? null;
}
public function events(): array
{
return $this->finishedEvents;
}
}

View File

@@ -0,0 +1,68 @@
<?php
namespace Common\ServerTiming;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class ServerTimingMiddleware
{
protected mixed $start;
public function __construct(protected ServerTiming $timing)
{
$this->start = $this->getRequestStartTime();
}
public function handle(Request $request, Closure $next)
{
$this->timing->setDuration('Bootstrap', $this->getElapsedTimeInMs());
$this->timing->start('App');
$response = $next($request);
$this->timing->stop('App');
$this->timing->stopAllUnfinishedEvents();
$this->timing->setDuration('Total', $this->getElapsedTimeInMs());
$response->headers->set('Server-Timing', $this->generateHeaders());
return $response;
}
protected function getElapsedTimeInMs(): float|int
{
return (microtime(true) - $this->start) * 1000;
}
protected function getRequestStartTime()
{
if (defined('LARAVEL_START')) {
return LARAVEL_START;
}
return $_SERVER['REQUEST_TIME_FLOAT'] ?? microtime(true);
}
protected function generateHeaders(): string
{
$header = '';
foreach ($this->timing->events() as $eventName => $duration) {
$eventNameSlug = Str::slug($eventName);
$header .= "{$eventNameSlug};desc=\"{$eventName}\";";
if (!is_null($duration)) {
$header .= "dur={$duration}";
}
$header .= ', ';
}
return $header;
}
}