OutputBamboo.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Class that used for output Magento Profiler results in format compatible with Bamboo Jmeter plugin
  8. */
  9. namespace Magento\TestFramework\Profiler;
  10. class OutputBamboo extends \Magento\Framework\Profiler\Driver\Standard\Output\Csvfile
  11. {
  12. /**
  13. * @var array
  14. */
  15. protected $_metrics;
  16. /**
  17. * Constructor
  18. *
  19. * @param array|null $config
  20. */
  21. public function __construct(array $config = null)
  22. {
  23. parent::__construct($config);
  24. $this->_metrics = isset($config['metrics']) ? (array)$config['metrics'] : [];
  25. }
  26. /**
  27. * Calculate metric value from set of timer names
  28. *
  29. * @param \Magento\Framework\Profiler\Driver\Standard\Stat $stat
  30. * @param array $timerNames
  31. * @param string $fetchKey
  32. * @return int
  33. */
  34. protected function _aggregateTimerValues(
  35. \Magento\Framework\Profiler\Driver\Standard\Stat $stat,
  36. array $timerNames,
  37. $fetchKey = \Magento\Framework\Profiler\Driver\Standard\Stat::AVG
  38. ) {
  39. /* Prepare pattern that matches timers with deepest nesting level only */
  40. $nestingSep = preg_quote(\Magento\Framework\Profiler::NESTING_SEPARATOR, '/');
  41. array_map('preg_quote', $timerNames, ['/']);
  42. $pattern = '/(?<=' . $nestingSep . '|^)(?:' . implode('|', $timerNames) . ')$/';
  43. /* Sum profiler values for matched timers */
  44. $result = 0;
  45. foreach ($this->_getTimerIds($stat) as $timerId) {
  46. if (preg_match($pattern, $timerId)) {
  47. $result += $stat->fetch($timerId, $fetchKey);
  48. }
  49. }
  50. /* Convert seconds -> milliseconds */
  51. $result = round($result * 1000);
  52. return $result;
  53. }
  54. /**
  55. * Write content into an opened file handle
  56. *
  57. * @param resource $fileHandle
  58. * @param \Magento\Framework\Profiler\Driver\Standard\Stat $stat
  59. */
  60. protected function _writeFileContent($fileHandle, \Magento\Framework\Profiler\Driver\Standard\Stat $stat)
  61. {
  62. /* First column must be a timestamp */
  63. $result = ['Timestamp' => time()];
  64. foreach ($this->_metrics as $metricName => $timerNames) {
  65. $result[$metricName] = $this->_aggregateTimerValues($stat, $timerNames);
  66. }
  67. fputcsv($fileHandle, array_keys($result), $this->_delimiter, $this->_enclosure);
  68. fputcsv($fileHandle, array_values($result), $this->_delimiter, $this->_enclosure);
  69. }
  70. }