*/ private array $stageNanoseconds = []; public function __construct() { $this->startedAtNanoseconds = hrtime(true); } /** * @template TValue * * @param Closure(): TValue $operation * @return TValue */ public function measure(ProcessingStage $stage, Closure $operation): mixed { $startedAt = hrtime(true); try { return $operation(); } finally { $elapsed = hrtime(true) - $startedAt; $this->stageNanoseconds[$stage->value] = ($this->stageNanoseconds[$stage->value] ?? 0) + $elapsed; } } public function elapsedSeconds(): float { return $this->nanosecondsToSeconds(hrtime(true) - $this->startedAtNanoseconds); } /** * @return array */ public function stageDurations(): array { return array_map($this->nanosecondsToSeconds(...), $this->stageNanoseconds); } private function nanosecondsToSeconds(int $nanoseconds): float { return $nanoseconds / 1_000_000_000; } }