PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/phpunit/php-timer/src/Timer.php

https://gitlab.com/Pasantias/pasantiasASLG
PHP | 107 lines | 49 code | 12 blank | 46 comment | 4 complexity | 16ac1873540ca928b9922751204042f8 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the PHP_Timer package.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  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. * Utility class for timing.
  12. *
  13. * @since Class available since Release 1.0.0
  14. */
  15. class PHP_Timer
  16. {
  17. /**
  18. * @var array
  19. */
  20. private static $times = array(
  21. 'hour' => 3600000,
  22. 'minute' => 60000,
  23. 'second' => 1000
  24. );
  25. /**
  26. * @var array
  27. */
  28. private static $startTimes = array();
  29. /**
  30. * @var float
  31. */
  32. public static $requestTime;
  33. /**
  34. * Starts the timer.
  35. */
  36. public static function start()
  37. {
  38. array_push(self::$startTimes, microtime(true));
  39. }
  40. /**
  41. * Stops the timer and returns the elapsed time.
  42. *
  43. * @return float
  44. */
  45. public static function stop()
  46. {
  47. return microtime(true) - array_pop(self::$startTimes);
  48. }
  49. /**
  50. * Formats the elapsed time as a string.
  51. *
  52. * @param float $time
  53. * @return string
  54. */
  55. public static function secondsToTimeString($time)
  56. {
  57. $ms = round($time * 1000);
  58. foreach (self::$times as $unit => $value) {
  59. if ($ms >= $value) {
  60. $time = floor($ms / $value * 100.0) / 100.0;
  61. return $time . ' ' . ($time == 1 ? $unit : $unit . 's');
  62. }
  63. }
  64. return $ms . ' ms';
  65. }
  66. /**
  67. * Formats the elapsed time since the start of the request as a string.
  68. *
  69. * @return string
  70. */
  71. public static function timeSinceStartOfRequest()
  72. {
  73. return self::secondsToTimeString(microtime(true) - self::$requestTime);
  74. }
  75. /**
  76. * Returns the resources (time, memory) of the request as a string.
  77. *
  78. * @return string
  79. */
  80. public static function resourceUsage()
  81. {
  82. return sprintf(
  83. 'Time: %s, Memory: %4.2fMb',
  84. self::timeSinceStartOfRequest(),
  85. memory_get_peak_usage(true) / 1048576
  86. );
  87. }
  88. }
  89. if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
  90. PHP_Timer::$requestTime = $_SERVER['REQUEST_TIME_FLOAT'];
  91. } elseif (isset($_SERVER['REQUEST_TIME'])) {
  92. PHP_Timer::$requestTime = $_SERVER['REQUEST_TIME'];
  93. } else {
  94. PHP_Timer::$requestTime = microtime(true);
  95. }