No Description

Timer.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * PHP_Timer
  4. *
  5. * Copyright (c) 2010-2013, Sebastian Bergmann <sebastian@phpunit.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Sebastian Bergmann nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @package PHP
  38. * @subpackage Timer
  39. * @author Sebastian Bergmann <sebastian@phpunit.de>
  40. * @copyright 2010-2013 Sebastian Bergmann <sebastian@phpunit.de>
  41. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  42. * @link http://github.com/sebastianbergmann/php-timer
  43. * @since File available since Release 1.0.0
  44. */
  45. /**
  46. * Utility class for timing.
  47. *
  48. * @package PHP
  49. * @subpackage Timer
  50. * @author Sebastian Bergmann <sebastian@phpunit.de>
  51. * @copyright 2010-2013 Sebastian Bergmann <sebastian@phpunit.de>
  52. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  53. * @version Release: @package_version@
  54. * @link http://github.com/sebastianbergmann/php-timer
  55. * @since Class available since Release 1.0.0
  56. */
  57. class PHP_Timer
  58. {
  59. /**
  60. * @var array
  61. */
  62. private static $times = array(
  63. 'hour' => 3600000,
  64. 'minute' => 60000,
  65. 'second' => 1000
  66. );
  67. /**
  68. * @var array
  69. */
  70. private static $startTimes = array();
  71. /**
  72. * @var float
  73. */
  74. public static $requestTime;
  75. /**
  76. * Starts the timer.
  77. */
  78. public static function start()
  79. {
  80. array_push(self::$startTimes, microtime(TRUE));
  81. }
  82. /**
  83. * Stops the timer and returns the elapsed time.
  84. *
  85. * @return float
  86. */
  87. public static function stop()
  88. {
  89. return microtime(TRUE) - array_pop(self::$startTimes);
  90. }
  91. /**
  92. * Formats the elapsed time as a string.
  93. *
  94. * @param float $time
  95. * @return string
  96. */
  97. public static function secondsToTimeString($time)
  98. {
  99. $ms = round($time * 1000);
  100. foreach (self::$times as $unit => $value) {
  101. if ($ms >= $value) {
  102. $time = floor($ms / $value * 100.0) / 100.0;
  103. return $time . ' ' . ($time == 1 ? $unit : $unit . 's');
  104. }
  105. }
  106. return $ms . ' ms';
  107. }
  108. /**
  109. * Formats the elapsed time since the start of the request as a string.
  110. *
  111. * @return string
  112. */
  113. public static function timeSinceStartOfRequest()
  114. {
  115. return self::secondsToTimeString(microtime(TRUE) - self::$requestTime);
  116. }
  117. /**
  118. * Returns the resources (time, memory) of the request as a string.
  119. *
  120. * @return string
  121. */
  122. public static function resourceUsage()
  123. {
  124. return sprintf(
  125. 'Time: %s, Memory: %4.2fMb',
  126. self::timeSinceStartOfRequest(),
  127. memory_get_peak_usage(TRUE) / 1048576
  128. );
  129. }
  130. }
  131. if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
  132. PHP_Timer::$requestTime = $_SERVER['REQUEST_TIME_FLOAT'];
  133. }
  134. else {
  135. PHP_Timer::$requestTime = microtime(TRUE);
  136. }