vendor/symfony/stopwatch/StopwatchEvent.php line 105

Open in your IDE?
  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 $periods = [];
  22.     /**
  23.      * @var float
  24.      */
  25.     private $origin;
  26.     /**
  27.      * @var string
  28.      */
  29.     private $category;
  30.     /**
  31.      * @var bool
  32.      */
  33.     private $morePrecision;
  34.     /**
  35.      * @var float[]
  36.      */
  37.     private $started = [];
  38.     /**
  39.      * @param float       $origin        The origin time in milliseconds
  40.      * @param string|null $category      The event category or null to use the default
  41.      * @param bool        $morePrecision If true, time is stored as float to keep the original microsecond precision
  42.      *
  43.      * @throws \InvalidArgumentException When the raw time is not valid
  44.      */
  45.     public function __construct(float $originstring $category nullbool $morePrecision false)
  46.     {
  47.         $this->origin $this->formatTime($origin);
  48.         $this->category \is_string($category) ? $category 'default';
  49.         $this->morePrecision $morePrecision;
  50.     }
  51.     /**
  52.      * Gets the category.
  53.      *
  54.      * @return string The category
  55.      */
  56.     public function getCategory()
  57.     {
  58.         return $this->category;
  59.     }
  60.     /**
  61.      * Gets the origin.
  62.      *
  63.      * @return float The origin in milliseconds
  64.      */
  65.     public function getOrigin()
  66.     {
  67.         return $this->origin;
  68.     }
  69.     /**
  70.      * Starts a new event period.
  71.      *
  72.      * @return $this
  73.      */
  74.     public function start()
  75.     {
  76.         $this->started[] = $this->getNow();
  77.         return $this;
  78.     }
  79.     /**
  80.      * Stops the last started event period.
  81.      *
  82.      * @return $this
  83.      *
  84.      * @throws \LogicException When stop() is called without a matching call to start()
  85.      */
  86.     public function stop()
  87.     {
  88.         if (!\count($this->started)) {
  89.             throw new \LogicException('stop() called but start() has not been called before.');
  90.         }
  91.         $this->periods[] = new StopwatchPeriod(array_pop($this->started), $this->getNow(), $this->morePrecision);
  92.         return $this;
  93.     }
  94.     /**
  95.      * Checks if the event was started.
  96.      *
  97.      * @return bool
  98.      */
  99.     public function isStarted()
  100.     {
  101.         return !empty($this->started);
  102.     }
  103.     /**
  104.      * Stops the current period and then starts a new one.
  105.      *
  106.      * @return $this
  107.      */
  108.     public function lap()
  109.     {
  110.         return $this->stop()->start();
  111.     }
  112.     /**
  113.      * Stops all non already stopped periods.
  114.      */
  115.     public function ensureStopped()
  116.     {
  117.         while (\count($this->started)) {
  118.             $this->stop();
  119.         }
  120.     }
  121.     /**
  122.      * Gets all event periods.
  123.      *
  124.      * @return StopwatchPeriod[] An array of StopwatchPeriod instances
  125.      */
  126.     public function getPeriods()
  127.     {
  128.         return $this->periods;
  129.     }
  130.     /**
  131.      * Gets the relative time of the start of the first period.
  132.      *
  133.      * @return int|float The time (in milliseconds)
  134.      */
  135.     public function getStartTime()
  136.     {
  137.         if (isset($this->periods[0])) {
  138.             return $this->periods[0]->getStartTime();
  139.         }
  140.         if ($this->started) {
  141.             return $this->started[0];
  142.         }
  143.         return 0;
  144.     }
  145.     /**
  146.      * Gets the relative time of the end of the last period.
  147.      *
  148.      * @return int|float The time (in milliseconds)
  149.      */
  150.     public function getEndTime()
  151.     {
  152.         $count \count($this->periods);
  153.         return $count $this->periods[$count 1]->getEndTime() : 0;
  154.     }
  155.     /**
  156.      * Gets the duration of the events (including all periods).
  157.      *
  158.      * @return int|float The duration (in milliseconds)
  159.      */
  160.     public function getDuration()
  161.     {
  162.         $periods $this->periods;
  163.         $left \count($this->started);
  164.         for ($i $left 1$i >= 0; --$i) {
  165.             $periods[] = new StopwatchPeriod($this->started[$i], $this->getNow(), $this->morePrecision);
  166.         }
  167.         $total 0;
  168.         foreach ($periods as $period) {
  169.             $total += $period->getDuration();
  170.         }
  171.         return $total;
  172.     }
  173.     /**
  174.      * Gets the max memory usage of all periods.
  175.      *
  176.      * @return int The memory usage (in bytes)
  177.      */
  178.     public function getMemory()
  179.     {
  180.         $memory 0;
  181.         foreach ($this->periods as $period) {
  182.             if ($period->getMemory() > $memory) {
  183.                 $memory $period->getMemory();
  184.             }
  185.         }
  186.         return $memory;
  187.     }
  188.     /**
  189.      * Return the current time relative to origin.
  190.      *
  191.      * @return float Time in ms
  192.      */
  193.     protected function getNow()
  194.     {
  195.         return $this->formatTime(microtime(true) * 1000 $this->origin);
  196.     }
  197.     /**
  198.      * Formats a time.
  199.      *
  200.      * @throws \InvalidArgumentException When the raw time is not valid
  201.      */
  202.     private function formatTime(float $time): float
  203.     {
  204.         return round($time1);
  205.     }
  206.     /**
  207.      * @return string
  208.      */
  209.     public function __toString()
  210.     {
  211.         return sprintf('%s: %.2F MiB - %d ms'$this->getCategory(), $this->getMemory() / 1024 1024$this->getDuration());
  212.     }
  213. }