Skip to content

Commit

Permalink
[HttpKernel] added memory information in the Stopwatch
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Nov 9, 2012
1 parent 4dcb0bf commit 8bb110e
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 5 deletions.
27 changes: 22 additions & 5 deletions Debug/StopwatchEvent.php
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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()
{
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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.
*
Expand Down
77 changes: 77 additions & 0 deletions Debug/StopwatchPeriod.php
@@ -0,0 +1,77 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <fabien@symfony.com>
*/
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;
}
}

0 comments on commit 8bb110e

Please sign in to comment.