4 * This file is part of Twig.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
13 * @author Fabien Potencier <fabien@symfony.com>
17 class Twig_Profiler_Profile implements IteratorAggregate, Serializable
20 const BLOCK = 'block';
21 const TEMPLATE = 'template';
22 const MACRO = 'macro';
27 private $starts = array();
28 private $ends = array();
29 private $profiles = array();
31 public function __construct($template = 'main', $type = self::ROOT, $name = 'main')
33 $this->template = $template;
35 $this->name = 0 === strpos($name, '__internal_') ? 'INTERNAL' : $name;
39 public function getTemplate()
41 return $this->template;
44 public function getType()
49 public function getName()
54 public function isRoot()
56 return self::ROOT === $this->type;
59 public function isTemplate()
61 return self::TEMPLATE === $this->type;
64 public function isBlock()
66 return self::BLOCK === $this->type;
69 public function isMacro()
71 return self::MACRO === $this->type;
74 public function getProfiles()
76 return $this->profiles;
79 public function addProfile(Twig_Profiler_Profile $profile)
81 $this->profiles[] = $profile;
85 * Returns the duration in microseconds.
89 public function getDuration()
91 if ($this->isRoot() && $this->profiles) {
92 // for the root node with children, duration is the sum of all child durations
94 foreach ($this->profiles as $profile) {
95 $duration += $profile->getDuration();
101 return isset($this->ends['wt']) && isset($this->starts['wt']) ? $this->ends['wt'] - $this->starts['wt'] : 0;
105 * Returns the memory usage in bytes.
109 public function getMemoryUsage()
111 return isset($this->ends['mu']) && isset($this->starts['mu']) ? $this->ends['mu'] - $this->starts['mu'] : 0;
115 * Returns the peak memory usage in bytes.
119 public function getPeakMemoryUsage()
121 return isset($this->ends['pmu']) && isset($this->starts['pmu']) ? $this->ends['pmu'] - $this->starts['pmu'] : 0;
125 * Starts the profiling.
127 public function enter()
129 $this->starts = array(
130 'wt' => microtime(true),
131 'mu' => memory_get_usage(),
132 'pmu' => memory_get_peak_usage(),
137 * Stops the profiling.
139 public function leave()
142 'wt' => microtime(true),
143 'mu' => memory_get_usage(),
144 'pmu' => memory_get_peak_usage(),
148 public function getIterator()
150 return new ArrayIterator($this->profiles);
153 public function serialize()
155 return serialize(array($this->template, $this->name, $this->type, $this->starts, $this->ends, $this->profiles));
158 public function unserialize($data)
160 list($this->template, $this->name, $this->type, $this->starts, $this->ends, $this->profiles) = unserialize($data);