MemoryDataCollector.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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\HttpKernel\DataCollector;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. /**
  14. * MemoryDataCollector.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. *
  18. * @final since Symfony 4.4
  19. */
  20. class MemoryDataCollector extends DataCollector implements LateDataCollectorInterface
  21. {
  22. public function __construct()
  23. {
  24. $this->reset();
  25. }
  26. /**
  27. * {@inheritdoc}
  28. *
  29. * @param \Throwable|null $exception
  30. */
  31. public function collect(Request $request, Response $response/*, \Throwable $exception = null*/)
  32. {
  33. $this->updateMemoryUsage();
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function reset()
  39. {
  40. $this->data = [
  41. 'memory' => 0,
  42. 'memory_limit' => $this->convertToBytes(ini_get('memory_limit')),
  43. ];
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function lateCollect()
  49. {
  50. $this->updateMemoryUsage();
  51. }
  52. /**
  53. * Gets the memory.
  54. *
  55. * @return int The memory
  56. */
  57. public function getMemory()
  58. {
  59. return $this->data['memory'];
  60. }
  61. /**
  62. * Gets the PHP memory limit.
  63. *
  64. * @return int The memory limit
  65. */
  66. public function getMemoryLimit()
  67. {
  68. return $this->data['memory_limit'];
  69. }
  70. /**
  71. * Updates the memory usage data.
  72. */
  73. public function updateMemoryUsage()
  74. {
  75. $this->data['memory'] = memory_get_peak_usage(true);
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function getName()
  81. {
  82. return 'memory';
  83. }
  84. /**
  85. * @return int|float
  86. */
  87. private function convertToBytes(string $memoryLimit)
  88. {
  89. if ('-1' === $memoryLimit) {
  90. return -1;
  91. }
  92. $memoryLimit = strtolower($memoryLimit);
  93. $max = strtolower(ltrim($memoryLimit, '+'));
  94. if (0 === strpos($max, '0x')) {
  95. $max = \intval($max, 16);
  96. } elseif (0 === strpos($max, '0')) {
  97. $max = \intval($max, 8);
  98. } else {
  99. $max = (int) $max;
  100. }
  101. switch (substr($memoryLimit, -1)) {
  102. case 't': $max *= 1024;
  103. // no break
  104. case 'g': $max *= 1024;
  105. // no break
  106. case 'm': $max *= 1024;
  107. // no break
  108. case 'k': $max *= 1024;
  109. }
  110. return $max;
  111. }
  112. }