Profile.php (3836B)
1 <?php 2 3 /* 4 * This file is part of Twig. 5 * 6 * (c) 2015 Fabien Potencier 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12 /** 13 * @author Fabien Potencier <fabien@symfony.com> 14 */ 15 class Twig_Profiler_Profile implements IteratorAggregate, Serializable 16 { 17 const ROOT = 'ROOT'; 18 const BLOCK = 'block'; 19 const TEMPLATE = 'template'; 20 const MACRO = 'macro'; 21 22 private $template; 23 private $name; 24 private $type; 25 private $starts = array(); 26 private $ends = array(); 27 private $profiles = array(); 28 29 public function __construct($template = 'main', $type = self::ROOT, $name = 'main') 30 { 31 $this->template = $template; 32 $this->type = $type; 33 $this->name = 0 === strpos($name, '__internal_') ? 'INTERNAL' : $name; 34 $this->enter(); 35 } 36 37 public function getTemplate() 38 { 39 return $this->template; 40 } 41 42 public function getType() 43 { 44 return $this->type; 45 } 46 47 public function getName() 48 { 49 return $this->name; 50 } 51 52 public function isRoot() 53 { 54 return self::ROOT === $this->type; 55 } 56 57 public function isTemplate() 58 { 59 return self::TEMPLATE === $this->type; 60 } 61 62 public function isBlock() 63 { 64 return self::BLOCK === $this->type; 65 } 66 67 public function isMacro() 68 { 69 return self::MACRO === $this->type; 70 } 71 72 public function getProfiles() 73 { 74 return $this->profiles; 75 } 76 77 public function addProfile(Twig_Profiler_Profile $profile) 78 { 79 $this->profiles[] = $profile; 80 } 81 82 /** 83 * Returns the duration in microseconds. 84 * 85 * @return int 86 */ 87 public function getDuration() 88 { 89 if ($this->isRoot() && $this->profiles) { 90 // for the root node with children, duration is the sum of all child durations 91 $duration = 0; 92 foreach ($this->profiles as $profile) { 93 $duration += $profile->getDuration(); 94 } 95 96 return $duration; 97 } 98 99 return isset($this->ends['wt']) && isset($this->starts['wt']) ? $this->ends['wt'] - $this->starts['wt'] : 0; 100 } 101 102 /** 103 * Returns the memory usage in bytes. 104 * 105 * @return int 106 */ 107 public function getMemoryUsage() 108 { 109 return isset($this->ends['mu']) && isset($this->starts['mu']) ? $this->ends['mu'] - $this->starts['mu'] : 0; 110 } 111 112 /** 113 * Returns the peak memory usage in bytes. 114 * 115 * @return int 116 */ 117 public function getPeakMemoryUsage() 118 { 119 return isset($this->ends['pmu']) && isset($this->starts['pmu']) ? $this->ends['pmu'] - $this->starts['pmu'] : 0; 120 } 121 122 /** 123 * Starts the profiling. 124 */ 125 public function enter() 126 { 127 $this->starts = array( 128 'wt' => microtime(true), 129 'mu' => memory_get_usage(), 130 'pmu' => memory_get_peak_usage(), 131 ); 132 } 133 134 /** 135 * Stops the profiling. 136 */ 137 public function leave() 138 { 139 $this->ends = array( 140 'wt' => microtime(true), 141 'mu' => memory_get_usage(), 142 'pmu' => memory_get_peak_usage(), 143 ); 144 } 145 146 public function getIterator() 147 { 148 return new ArrayIterator($this->profiles); 149 } 150 151 public function serialize() 152 { 153 return serialize(array($this->template, $this->name, $this->type, $this->starts, $this->ends, $this->profiles)); 154 } 155 156 public function unserialize($data) 157 { 158 list($this->template, $this->name, $this->type, $this->starts, $this->ends, $this->profiles) = unserialize($data); 159 } 160 }