/vendor/phing/phing/classes/phing/tasks/ext/StopwatchTask.php

https://gitlab.com/Isaki/le331.fr · PHP · 215 lines · 84 code · 21 blank · 110 comment · 4 complexity · ca4fae60d37739b8872794d14d2513c4 MD5 · raw file

  1. <?php
  2. /**
  3. * $Id: 46a07222af7bd1fbfd0e39169d5f80a83c6525e1 $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. require_once 'phing/Task.php';
  22. /**
  23. * Stopwatch.
  24. *
  25. * @author Siad Ardroumli <siad.ardroumli@gmail.com>
  26. * @version $Id: 46a07222af7bd1fbfd0e39169d5f80a83c6525e1 $
  27. * @package phing.tasks.ext.stopwatch
  28. */
  29. class StopwatchTask extends Task
  30. {
  31. /**
  32. * Name of the timer.
  33. *
  34. * @var string $name
  35. */
  36. private $name = '';
  37. /**
  38. * Category of the timer.
  39. *
  40. * @var string $category optional
  41. */
  42. private $category = '';
  43. /**
  44. * Timer action.
  45. *
  46. * @var string $action
  47. */
  48. private $action = 'start';
  49. /**
  50. * Holds an instance of Stopwatch.
  51. *
  52. * @var Stopwatch $timer
  53. */
  54. private static $timer = null;
  55. /**
  56. * Initialize Task.
  57. *
  58. * @return void
  59. */
  60. public function init()
  61. {
  62. }
  63. /**
  64. * Load stopwatch.
  65. *
  66. * @return void
  67. *
  68. * @throws BuildException
  69. */
  70. private function loadStopwatch()
  71. {
  72. if (version_compare(PHP_VERSION, '5.3.3', '<')) {
  73. throw new BuildException("StopwatchTask requires at least PHP 5.3.3 installed.");
  74. }
  75. @include_once 'Symfony/Component/Stopwatch/autoload.php';
  76. @include_once 'vendor/autoload.php';
  77. if (!class_exists('\\Symfony\\Component\\Stopwatch\\Stopwatch')) {
  78. throw new BuildException("StopwatchTask requires Stopwatch to be installed");
  79. }
  80. }
  81. /**
  82. * Get the stopwatch instance.
  83. *
  84. * @return \Symfony\Component\Stopwatch\Stopwatch
  85. */
  86. private function getStopwatchInstance()
  87. {
  88. if (self::$timer === null) {
  89. $stopwatch = '\\Symfony\\Component\\Stopwatch\\Stopwatch';
  90. self::$timer = new $stopwatch;
  91. }
  92. return self::$timer;
  93. }
  94. /**
  95. * Start timer.
  96. *
  97. * @return void
  98. */
  99. private function start()
  100. {
  101. $timer = $this->getStopwatchInstance();
  102. $timer->start($this->name, $this->category);
  103. }
  104. /**
  105. * Stop timer.
  106. *
  107. * @return void
  108. */
  109. private function stop()
  110. {
  111. $timer = $this->getStopwatchInstance();
  112. $event = $timer->stop($this->name);
  113. foreach ($event->getPeriods() as $period) {
  114. $this->log('Starttime: ' . $period->getStartTime() . ' - Endtime: ' . $period->getEndTime() . ' - Duration: ' . $period->getDuration() . ' - Memory: ' . $period->getMemory(), Project::MSG_INFO);
  115. }
  116. $this->log('Category: ' . $event->getCategory(), Project::MSG_INFO);
  117. $this->log('Origin: ' . $event->getOrigin(), Project::MSG_INFO);
  118. $this->log('Start time: ' . $event->getStartTime(), Project::MSG_INFO);
  119. $this->log('End time: ' . $event->getEndTime(), Project::MSG_INFO);
  120. $this->log('Duration: ' . $event->getDuration(), Project::MSG_INFO);
  121. $this->log('Memory: ' . $event->getMemory(), Project::MSG_INFO);
  122. }
  123. /**
  124. * Measure lap time.
  125. *
  126. * @return void
  127. */
  128. private function lap()
  129. {
  130. $timer = $this->getStopwatchInstance();
  131. $timer->lap($this->name);
  132. }
  133. /**
  134. * Set the name of the stopwatch.
  135. *
  136. * @param string $name the name of the stopwatch timer
  137. *
  138. * @return void
  139. */
  140. public function setName($name)
  141. {
  142. $this->name = $name;
  143. }
  144. /**
  145. * Set the category of the stopwatch.
  146. *
  147. * @param string $category
  148. *
  149. * @return void
  150. */
  151. public function setCategory($category)
  152. {
  153. $this->category = $category;
  154. }
  155. /**
  156. * Set the action.
  157. * Action could be one of
  158. * - start
  159. * - lap
  160. * - stop
  161. *
  162. * @param string $action
  163. *
  164. * @return void
  165. */
  166. public function setAction($action)
  167. {
  168. $this->action = $action;
  169. }
  170. /**
  171. * The main entry point
  172. *
  173. * @return void
  174. *
  175. * @throws BuildException
  176. */
  177. public function main()
  178. {
  179. $this->loadStopwatch();
  180. switch ($this->action) {
  181. case "start":
  182. $this->start();
  183. break;
  184. case "stop":
  185. $this->stop();
  186. break;
  187. case "lap":
  188. $this->lap();
  189. break;
  190. default:
  191. throw new BuildException('action should be one of start, stop, lap.');
  192. }
  193. }
  194. }