2011-10-17 09:25:52 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony framework.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\HttpKernel\Debug;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stopwatch provides a way to profile code.
|
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
|
*/
|
|
|
|
class Stopwatch
|
|
|
|
{
|
|
|
|
private $waiting;
|
|
|
|
private $sections;
|
|
|
|
private $events;
|
|
|
|
private $origin;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts a new section.
|
|
|
|
*/
|
|
|
|
public function startSection()
|
|
|
|
{
|
|
|
|
if ($this->events) {
|
2012-01-31 06:48:19 +00:00
|
|
|
$this->start('__section__.child', 'section');
|
2011-10-17 09:25:52 +01:00
|
|
|
$this->waiting[] = array($this->events, $this->origin);
|
|
|
|
$this->events = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->origin = microtime(true) * 1000;
|
|
|
|
|
2012-01-31 06:48:19 +00:00
|
|
|
$this->start('__section__');
|
2011-10-17 09:25:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the last started section.
|
|
|
|
*
|
2012-01-10 21:23:16 +00:00
|
|
|
* The id parameter is used to retrieve the events from this section.
|
|
|
|
*
|
|
|
|
* @see getSectionEvents
|
|
|
|
*
|
2011-10-17 09:25:52 +01:00
|
|
|
* @param string $id The identifier of the section
|
|
|
|
*/
|
|
|
|
public function stopSection($id)
|
|
|
|
{
|
2012-01-31 06:48:19 +00:00
|
|
|
$this->stop('__section__');
|
2011-10-17 09:25:52 +01:00
|
|
|
|
2012-01-10 21:23:16 +00:00
|
|
|
if (null !== $id) {
|
2011-10-17 09:25:52 +01:00
|
|
|
$this->sections[$id] = $this->events;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->waiting) {
|
|
|
|
list($this->events, $this->origin) = array_pop($this->waiting);
|
2012-01-31 06:48:19 +00:00
|
|
|
$this->stop('__section__.child');
|
2011-10-17 09:25:52 +01:00
|
|
|
} else {
|
|
|
|
$this->origin = null;
|
|
|
|
$this->events = array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts an event.
|
|
|
|
*
|
|
|
|
* @param string $name The event name
|
|
|
|
* @param string $category The event category
|
|
|
|
*
|
|
|
|
* @return StopwatchEvent A StopwatchEvent instance
|
|
|
|
*/
|
|
|
|
public function start($name, $category = null)
|
|
|
|
{
|
|
|
|
if (!isset($this->events[$name])) {
|
2012-01-25 13:27:59 +00:00
|
|
|
$this->events[$name] = new StopwatchEvent($this->origin ?: microtime(true) * 1000, $category);
|
2011-10-17 09:25:52 +01:00
|
|
|
}
|
|
|
|
|
2012-01-10 21:23:16 +00:00
|
|
|
return $this->events[$name]->start();
|
2011-10-17 09:25:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops an event.
|
|
|
|
*
|
|
|
|
* @param string $name The event name
|
|
|
|
*
|
|
|
|
* @return StopwatchEvent A StopwatchEvent instance
|
|
|
|
*/
|
|
|
|
public function stop($name)
|
|
|
|
{
|
|
|
|
if (!isset($this->events[$name])) {
|
|
|
|
throw new \LogicException(sprintf('Event "%s" is not started.', $name));
|
|
|
|
}
|
|
|
|
|
2012-01-10 21:23:16 +00:00
|
|
|
return $this->events[$name]->stop();
|
2011-10-17 09:25:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops then restart an event.
|
|
|
|
*
|
|
|
|
* @param string $name The event name
|
|
|
|
*
|
|
|
|
* @return StopwatchEvent A StopwatchEvent instance
|
|
|
|
*/
|
|
|
|
public function lap($name)
|
|
|
|
{
|
2012-01-10 21:23:16 +00:00
|
|
|
return $this->stop($name)->start();
|
2011-10-17 09:25:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all events for a given section.
|
|
|
|
*
|
|
|
|
* @param string $id A section identifier
|
|
|
|
*
|
|
|
|
* @return StopwatchEvent[] An array of StopwatchEvent instances
|
|
|
|
*/
|
|
|
|
public function getSectionEvents($id)
|
|
|
|
{
|
|
|
|
return isset($this->sections[$id]) ? $this->sections[$id] : array();
|
|
|
|
}
|
|
|
|
}
|