ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Profile.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of Twig.
5  *
6  * (c) 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 
18 {
19  const ROOT = 'ROOT';
20  const BLOCK = 'block';
21  const TEMPLATE = 'template';
22  const MACRO = 'macro';
23 
24  private $template;
25  private $name;
26  private $type;
27  private $starts = array();
28  private $ends = array();
29  private $profiles = array();
30 
31  public function __construct($template = 'main', $type = self::ROOT, $name = 'main')
32  {
33  $this->template = $template;
34  $this->type = $type;
35  $this->name = 0 === strpos($name, '__internal_') ? 'INTERNAL' : $name;
36  $this->enter();
37  }
38 
39  public function getTemplate()
40  {
41  return $this->template;
42  }
43 
44  public function getType()
45  {
46  return $this->type;
47  }
48 
49  public function getName()
50  {
51  return $this->name;
52  }
53 
54  public function isRoot()
55  {
56  return self::ROOT === $this->type;
57  }
58 
59  public function isTemplate()
60  {
61  return self::TEMPLATE === $this->type;
62  }
63 
64  public function isBlock()
65  {
66  return self::BLOCK === $this->type;
67  }
68 
69  public function isMacro()
70  {
71  return self::MACRO === $this->type;
72  }
73 
74  public function getProfiles()
75  {
76  return $this->profiles;
77  }
78 
79  public function addProfile(Twig_Profiler_Profile $profile)
80  {
81  $this->profiles[] = $profile;
82  }
83 
89  public function getDuration()
90  {
91  if ($this->isRoot() && $this->profiles) {
92  // for the root node with children, duration is the sum of all child durations
93  $duration = 0;
94  foreach ($this->profiles as $profile) {
95  $duration += $profile->getDuration();
96  }
97 
98  return $duration;
99  }
100 
101  return isset($this->ends['wt']) && isset($this->starts['wt']) ? $this->ends['wt'] - $this->starts['wt'] : 0;
102  }
103 
109  public function getMemoryUsage()
110  {
111  return isset($this->ends['mu']) && isset($this->starts['mu']) ? $this->ends['mu'] - $this->starts['mu'] : 0;
112  }
113 
119  public function getPeakMemoryUsage()
120  {
121  return isset($this->ends['pmu']) && isset($this->starts['pmu']) ? $this->ends['pmu'] - $this->starts['pmu'] : 0;
122  }
123 
127  public function enter()
128  {
129  $this->starts = array(
130  'wt' => microtime(true),
131  'mu' => memory_get_usage(),
132  'pmu' => memory_get_peak_usage(),
133  );
134  }
135 
139  public function leave()
140  {
141  $this->ends = array(
142  'wt' => microtime(true),
143  'mu' => memory_get_usage(),
144  'pmu' => memory_get_peak_usage(),
145  );
146  }
147 
148  public function reset()
149  {
150  $this->starts = $this->ends = $this->profiles = array();
151  $this->enter();
152  }
153 
154  public function getIterator()
155  {
156  return new ArrayIterator($this->profiles);
157  }
158 
159  public function serialize()
160  {
161  return serialize(array($this->template, $this->name, $this->type, $this->starts, $this->ends, $this->profiles));
162  }
163 
164  public function unserialize($data)
165  {
166  list($this->template, $this->name, $this->type, $this->starts, $this->ends, $this->profiles) = unserialize($data);
167  }
168 }
169 
170 class_alias('Twig_Profiler_Profile', 'Twig\Profiler\Profile', false);
getPeakMemoryUsage()
Returns the peak memory usage in bytes.
Definition: Profile.php:119
getMemoryUsage()
Returns the memory usage in bytes.
Definition: Profile.php:109
__construct($template='main', $type=self::ROOT, $name='main')
Definition: Profile.php:31
enter()
Starts the profiling.
Definition: Profile.php:127
getDuration()
Returns the duration in microseconds.
Definition: Profile.php:89
leave()
Stops the profiling.
Definition: Profile.php:139
addProfile(Twig_Profiler_Profile $profile)
Definition: Profile.php:79
$data
Definition: bench.php:6