StopwatchPeriod.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Stopwatch;
  11. /**
  12. * Represents an Period for an Event.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class StopwatchPeriod
  17. {
  18. private int|float $start;
  19. private int|float $end;
  20. private int $memory;
  21. /**
  22. * @param int|float $start The relative time of the start of the period (in milliseconds)
  23. * @param int|float $end The relative time of the end of the period (in milliseconds)
  24. * @param bool $morePrecision If true, time is stored as float to keep the original microsecond precision
  25. */
  26. public function __construct(int|float $start, int|float $end, bool $morePrecision = false)
  27. {
  28. $this->start = $morePrecision ? (float) $start : (int) $start;
  29. $this->end = $morePrecision ? (float) $end : (int) $end;
  30. $this->memory = memory_get_usage(true);
  31. }
  32. /**
  33. * Gets the relative time of the start of the period in milliseconds.
  34. */
  35. public function getStartTime(): int|float
  36. {
  37. return $this->start;
  38. }
  39. /**
  40. * Gets the relative time of the end of the period in milliseconds.
  41. */
  42. public function getEndTime(): int|float
  43. {
  44. return $this->end;
  45. }
  46. /**
  47. * Gets the time spent in this period in milliseconds.
  48. */
  49. public function getDuration(): int|float
  50. {
  51. return $this->end - $this->start;
  52. }
  53. /**
  54. * Gets the memory usage in bytes.
  55. */
  56. public function getMemory(): int
  57. {
  58. return $this->memory;
  59. }
  60. public function __toString(): string
  61. {
  62. return sprintf('%.2F MiB - %d ms', $this->getMemory() / 1024 / 1024, $this->getDuration());
  63. }
  64. }