diff --git a/Debug/StopwatchEvent.php b/Debug/StopwatchEvent.php index 200c465bac..82c463906f 100644 --- a/Debug/StopwatchEvent.php +++ b/Debug/StopwatchEvent.php @@ -82,7 +82,7 @@ public function stop() throw new \LogicException('stop() called but start() has not been called before.'); } - $this->periods[] = array(array_pop($this->started), $this->getNow()); + $this->periods[] = new StopwatchPeriod(array_pop($this->started), $this->getNow()); return $this; } @@ -110,7 +110,7 @@ public function ensureStopped() /** * Gets all event periods. * - * @return array An array of periods + * @return StopwatchPeriod[] An array of StopwatchPeriod instances */ public function getPeriods() { @@ -124,7 +124,7 @@ public function getPeriods() */ public function getStartTime() { - return isset($this->periods[0]) ? $this->periods[0][0] : 0; + return isset($this->periods[0]) ? $this->periods[0]->getStartTime() : 0; } /** @@ -134,7 +134,7 @@ public function getStartTime() */ public function getEndTime() { - return ($count = count($this->periods)) ? $this->periods[$count - 1][1] : 0; + return ($count = count($this->periods)) ? $this->periods[$count - 1]->getEndTime() : 0; } /** @@ -146,12 +146,29 @@ public function getTotalTime() { $total = 0; foreach ($this->periods as $period) { - $total += $period[1] - $period[0]; + $total += $period->getTime(); } return $this->formatTime($total); } + /** + * Gets the max memory usage of all periods. + * + * @return integer The memory usage (in bytes) + */ + public function getMemory() + { + $memory = 0; + foreach ($this->periods as $period) { + if ($period->getMemory() > $memory) { + $memory = $period->getMemory(); + } + } + + return $memory; + } + /** * Return the current time relative to origin. * diff --git a/Debug/StopwatchPeriod.php b/Debug/StopwatchPeriod.php new file mode 100644 index 0000000000..f920e8dedb --- /dev/null +++ b/Debug/StopwatchPeriod.php @@ -0,0 +1,77 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpKernel\Debug; + +/** + * Represents an Period for an Event. + * + * @author Fabien Potencier + */ +class StopwatchPeriod +{ + private $start; + private $end; + private $memory; + + /** + * Constructor + * + * @param integer $start The relative time of the start of the period + * @param integer $end The relative time of the end of the period + */ + public function __construct($start, $end) + { + $this->start = $start; + $this->end = $end; + $this->memory = memory_get_usage(); + } + + /** + * Gets the relative time of the start of the period. + * + * @return integer The time (in milliseconds) + */ + public function getStartTime() + { + return $this->start; + } + + /** + * Gets the relative time of the end of the period. + * + * @return integer The time (in milliseconds) + */ + public function getEndTime() + { + return $this->end; + } + + /** + * Gets the time spent in this period. + * + * @return integer The time (in milliseconds) + */ + public function getTime() + { + return $this->end - $this->start; + } + + /** + * Gets the memory usage. + * + * @return integer The memory usage (in bytes) + */ + public function getMemory() + { + return $this->memory; + } +}