AbstractOutput.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * Abstract class that represents profiler standard driver output
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Profiler\Driver\Standard;
  9. abstract class AbstractOutput implements OutputInterface
  10. {
  11. /**
  12. * PCRE Regular Expression for filter timer by id
  13. *
  14. * @var null|string
  15. */
  16. protected $_filterPattern;
  17. /**
  18. * List of threshold (minimal allowed) values for profiler data
  19. *
  20. * @var array
  21. */
  22. protected $_thresholds = [Stat::TIME => 0.001, Stat::COUNT => 10, Stat::EMALLOC => 10000];
  23. /**
  24. * List of columns to output
  25. *
  26. * @var array
  27. */
  28. protected $_columns = [
  29. 'Timer Id' => Stat::ID,
  30. 'Time' => Stat::TIME,
  31. 'Avg' => Stat::AVG,
  32. 'Cnt' => Stat::COUNT,
  33. 'Emalloc' => Stat::EMALLOC,
  34. 'RealMem' => Stat::REALMEM,
  35. ];
  36. /**
  37. * Constructor
  38. *
  39. * @param array|null $config
  40. */
  41. public function __construct(array $config = null)
  42. {
  43. if (!empty($config['filterPattern'])) {
  44. $this->setFilterPattern($config['filterPattern']);
  45. }
  46. if (!empty($config['thresholds']) && is_array($config['thresholds'])) {
  47. foreach ($config['thresholds'] as $fetchKey => $minAllowedValue) {
  48. $this->setThreshold($fetchKey, (int)$minAllowedValue);
  49. }
  50. }
  51. }
  52. /**
  53. * Set profiler output with timer identifiers filter.
  54. *
  55. * @param string $filterPattern PCRE pattern to filter timers by their identifiers
  56. * @return void
  57. */
  58. public function setFilterPattern($filterPattern)
  59. {
  60. $this->_filterPattern = $filterPattern;
  61. }
  62. /**
  63. * Get profiler output timer identifiers filter.
  64. *
  65. * @return string|null
  66. */
  67. public function getFilterPattern()
  68. {
  69. return $this->_filterPattern;
  70. }
  71. /**
  72. * Set threshold (minimal allowed) value for timer column.
  73. *
  74. * Timer is being rendered if at least one of its columns is not less than the minimal allowed value.
  75. *
  76. * @param string $fetchKey
  77. * @param int|float|null $minAllowedValue
  78. * @return void
  79. */
  80. public function setThreshold($fetchKey, $minAllowedValue)
  81. {
  82. if ($minAllowedValue === null) {
  83. unset($this->_thresholds[$fetchKey]);
  84. } else {
  85. $this->_thresholds[$fetchKey] = $minAllowedValue;
  86. }
  87. }
  88. /**
  89. * Get list of thresholds.
  90. *
  91. * @return array
  92. */
  93. public function getThresholds()
  94. {
  95. return $this->_thresholds;
  96. }
  97. /**
  98. * Render statistics column value for specified timer
  99. *
  100. * @param string|float $value
  101. * @param string $columnKey
  102. * @return string
  103. */
  104. protected function _renderColumnValue($value, $columnKey)
  105. {
  106. switch ($columnKey) {
  107. case Stat::ID:
  108. $result = $this->_renderTimerId($value);
  109. break;
  110. case Stat::TIME:
  111. case Stat::AVG:
  112. $result = number_format($value, 6);
  113. break;
  114. default:
  115. $result = number_format((string)$value);
  116. }
  117. return $result;
  118. }
  119. /**
  120. * Render timer id
  121. *
  122. * @param string $timerId
  123. * @return string
  124. */
  125. protected function _renderTimerId($timerId)
  126. {
  127. return $timerId;
  128. }
  129. /**
  130. * Render a caption for the profiling results
  131. *
  132. * @return string
  133. */
  134. protected function _renderCaption()
  135. {
  136. return sprintf(
  137. 'Code Profiler (Memory usage: real - %s, emalloc - %s)',
  138. memory_get_usage(true),
  139. memory_get_usage()
  140. );
  141. }
  142. /**
  143. * Retrieve the list of timer ids from timer statistics object.
  144. *
  145. * Timer ids will be ordered and filtered by thresholds and filter pattern.
  146. *
  147. * @param Stat $stat
  148. * @return string[]
  149. */
  150. protected function _getTimerIds(Stat $stat)
  151. {
  152. return $stat->getFilteredTimerIds($this->_thresholds, $this->_filterPattern);
  153. }
  154. }