vendor/symfony/stopwatch/StopwatchEvent.php line 19

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Stopwatch;
  11. /**
  12.  * Represents an Event managed by Stopwatch.
  13.  *
  14.  * @author Fabien Potencier <fabien@symfony.com>
  15.  */
  16. class StopwatchEvent
  17. {
  18.     /**
  19.      * @var StopwatchPeriod[]
  20.      */
  21.     private array $periods = [];
  22.     private float $origin;
  23.     private string $category;
  24.     private bool $morePrecision;
  25.     /**
  26.      * @var float[]
  27.      */
  28.     private array $started = [];
  29.     private string $name;
  30.     /**
  31.      * @param float       $origin        The origin time in milliseconds
  32.      * @param string|null $category      The event category or null to use the default
  33.      * @param bool        $morePrecision If true, time is stored as float to keep the original microsecond precision
  34.      * @param string|null $name          The event name or null to define the name as default
  35.      *
  36.      * @throws \InvalidArgumentException When the raw time is not valid
  37.      */
  38.     public function __construct(float $originstring $category nullbool $morePrecision falsestring $name null)
  39.     {
  40.         $this->origin $this->formatTime($origin);
  41.         $this->category \is_string($category) ? $category 'default';
  42.         $this->morePrecision $morePrecision;
  43.         $this->name $name ?? 'default';
  44.     }
  45.     /**
  46.      * Gets the category.
  47.      */
  48.     public function getCategory(): string
  49.     {
  50.         return $this->category;
  51.     }
  52.     /**
  53.      * Gets the origin in milliseconds.
  54.      */
  55.     public function getOrigin(): float
  56.     {
  57.         return $this->origin;
  58.     }
  59.     /**
  60.      * Starts a new event period.
  61.      *
  62.      * @return $this
  63.      */
  64.     public function start(): static
  65.     {
  66.         $this->started[] = $this->getNow();
  67.         return $this;
  68.     }
  69.     /**
  70.      * Stops the last started event period.
  71.      *
  72.      * @return $this
  73.      *
  74.      * @throws \LogicException When stop() is called without a matching call to start()
  75.      */
  76.     public function stop(): static
  77.     {
  78.         if (!\count($this->started)) {
  79.             throw new \LogicException('stop() called but start() has not been called before.');
  80.         }
  81.         $this->periods[] = new StopwatchPeriod(array_pop($this->started), $this->getNow(), $this->morePrecision);
  82.         return $this;
  83.     }
  84.     /**
  85.      * Checks if the event was started.
  86.      */
  87.     public function isStarted(): bool
  88.     {
  89.         return !empty($this->started);
  90.     }
  91.     /**
  92.      * Stops the current period and then starts a new one.
  93.      *
  94.      * @return $this
  95.      */
  96.     public function lap(): static
  97.     {
  98.         return $this->stop()->start();
  99.     }
  100.     /**
  101.      * Stops all non already stopped periods.
  102.      */
  103.     public function ensureStopped()
  104.     {
  105.         while (\count($this->started)) {
  106.             $this->stop();
  107.         }
  108.     }
  109.     /**
  110.      * Gets all event periods.
  111.      *
  112.      * @return StopwatchPeriod[]
  113.      */
  114.     public function getPeriods(): array
  115.     {
  116.         return $this->periods;
  117.     }
  118.     /**
  119.      * Gets the relative time of the start of the first period in milliseconds.
  120.      */
  121.     public function getStartTime(): int|float
  122.     {
  123.         if (isset($this->periods[0])) {
  124.             return $this->periods[0]->getStartTime();
  125.         }
  126.         if ($this->started) {
  127.             return $this->started[0];
  128.         }
  129.         return 0;
  130.     }
  131.     /**
  132.      * Gets the relative time of the end of the last period in milliseconds.
  133.      */
  134.     public function getEndTime(): int|float
  135.     {
  136.         $count \count($this->periods);
  137.         return $count $this->periods[$count 1]->getEndTime() : 0;
  138.     }
  139.     /**
  140.      * Gets the duration of the events in milliseconds (including all periods).
  141.      */
  142.     public function getDuration(): int|float
  143.     {
  144.         $periods $this->periods;
  145.         $left \count($this->started);
  146.         for ($i $left 1$i >= 0; --$i) {
  147.             $periods[] = new StopwatchPeriod($this->started[$i], $this->getNow(), $this->morePrecision);
  148.         }
  149.         $total 0;
  150.         foreach ($periods as $period) {
  151.             $total += $period->getDuration();
  152.         }
  153.         return $total;
  154.     }
  155.     /**
  156.      * Gets the max memory usage of all periods in bytes.
  157.      */
  158.     public function getMemory(): int
  159.     {
  160.         $memory 0;
  161.         foreach ($this->periods as $period) {
  162.             if ($period->getMemory() > $memory) {
  163.                 $memory $period->getMemory();
  164.             }
  165.         }
  166.         return $memory;
  167.     }
  168.     /**
  169.      * Return the current time relative to origin in milliseconds.
  170.      */
  171.     protected function getNow(): float
  172.     {
  173.         return $this->formatTime(microtime(true) * 1000 $this->origin);
  174.     }
  175.     /**
  176.      * Formats a time.
  177.      *
  178.      * @throws \InvalidArgumentException When the raw time is not valid
  179.      */
  180.     private function formatTime(float $time): float
  181.     {
  182.         return round($time1);
  183.     }
  184.     /**
  185.      * Gets the event name.
  186.      */
  187.     public function getName(): string
  188.     {
  189.         return $this->name;
  190.     }
  191.     public function __toString(): string
  192.     {
  193.         return sprintf('%s/%s: %.2F MiB - %d ms'$this->getCategory(), $this->getName(), $this->getMemory() / 1024 1024$this->getDuration());
  194.     }
  195. }