Profile.php 3.6 KB

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