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