PageRenderTime 60ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/dev/tests/performance/framework/Magento/Performance/Scenario/Handler/Php.php

https://bitbucket.org/sunil_nextbits/magento2
PHP | 188 lines | 100 code | 10 blank | 78 comment | 5 complexity | b71ed6f49e01a691538b9e8d43eaf373 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Magento
  22. * @package performance_tests
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Handler for performance testing scenarios in format of PHP console scripts
  28. */
  29. class Magento_Performance_Scenario_Handler_Php implements Magento_Performance_Scenario_HandlerInterface
  30. {
  31. /**
  32. * @var Magento_Shell
  33. */
  34. protected $_shell;
  35. /**
  36. * @var bool
  37. */
  38. protected $_validateExecutable;
  39. /**
  40. * Constructor
  41. *
  42. * @param Magento_Shell $shell
  43. * @param bool $validateExecutable
  44. */
  45. public function __construct(Magento_Shell $shell, $validateExecutable = true)
  46. {
  47. $this->_shell = $shell;
  48. $this->_validateExecutable = $validateExecutable;
  49. }
  50. /**
  51. * Validate whether scenario executable is available in the environment
  52. */
  53. protected function _validateScenarioExecutable()
  54. {
  55. if ($this->_validateExecutable) {
  56. $this->_validateExecutable = false; // validate only once
  57. $this->_shell->execute('php --version');
  58. }
  59. }
  60. /**
  61. * Run scenario and optionally write results to report file
  62. *
  63. * @param Magento_Performance_Scenario $scenario
  64. * @param string|null $reportFile Report file to write results to, NULL disables report creation
  65. * @throws Magento_Exception
  66. * @throws Magento_Performance_Scenario_FailureException
  67. *
  68. * @todo Implement execution in concurrent threads defined by the "users" scenario argument
  69. */
  70. public function run(Magento_Performance_Scenario $scenario, $reportFile = null)
  71. {
  72. $this->_validateScenarioExecutable();
  73. $scenarioArguments = $scenario->getArguments();
  74. $reportRows = array();
  75. for ($i = 0; $i < $scenarioArguments[Magento_Performance_Scenario::ARG_LOOPS]; $i++) {
  76. $oneReportRow = $this->_executeScenario($scenario);
  77. $reportRows[] = $oneReportRow;
  78. }
  79. if ($reportFile) {
  80. $this->_writeReport($reportRows, $reportFile);
  81. }
  82. $reportErrors = $this->_getReportErrors($reportRows);
  83. if ($reportErrors) {
  84. throw new Magento_Performance_Scenario_FailureException($scenario, implode(PHP_EOL, $reportErrors));
  85. }
  86. }
  87. /**
  88. * Execute scenario and return measurement results
  89. *
  90. * @param Magento_Performance_Scenario $scenario
  91. * @return array
  92. */
  93. protected function _executeScenario(Magento_Performance_Scenario $scenario)
  94. {
  95. list($scenarioCmd, $scenarioCmdArgs) = $this->_buildScenarioCmd($scenario);
  96. $result = array(
  97. 'title' => $scenario->getTitle(),
  98. 'timestamp' => time(),
  99. 'success' => true,
  100. 'time' => null,
  101. 'exit_code' => 0,
  102. 'output' => '',
  103. );
  104. $executionTime = microtime(true);
  105. try {
  106. $result['output'] = $this->_shell->execute($scenarioCmd, $scenarioCmdArgs);
  107. } catch (Magento_Exception $e) {
  108. $result['success'] = false;
  109. $result['exit_code'] = $e->getPrevious()->getCode();
  110. $result['output'] = $e->getPrevious()->getMessage();
  111. }
  112. $executionTime = (microtime(true) - $executionTime);
  113. $executionTime *= 1000; // second -> millisecond
  114. $result['time'] = (int)round($executionTime);
  115. return $result;
  116. }
  117. /**
  118. * Build and return scenario execution command and arguments for it, compatible with the getopt() "long options"
  119. * @link http://www.php.net/getopt
  120. *
  121. * @param Magento_Performance_Scenario $scenario
  122. * @return array
  123. */
  124. protected function _buildScenarioCmd(Magento_Performance_Scenario $scenario)
  125. {
  126. $command = 'php -f %s --';
  127. $arguments = array($scenario->getFile());
  128. foreach ($scenario->getArguments() as $paramName => $paramValue) {
  129. $command .= " --$paramName %s";
  130. $arguments[] = $paramValue;
  131. }
  132. return array($command, $arguments);
  133. }
  134. /**
  135. * Write report into file in Apache JMeter's JTL format
  136. * @link http://wiki.apache.org/jmeter/JtlTestLog
  137. *
  138. * @param array $reportRows
  139. * @param string $reportFile
  140. */
  141. protected function _writeReport(array $reportRows, $reportFile)
  142. {
  143. $xml = array();
  144. $xml[] = '<?xml version="1.0" encoding="UTF-8"?>';
  145. $xml[] = '<testResults version="1.2">';
  146. foreach ($reportRows as $index => $oneReportRow) {
  147. $xml[] = '<httpSample'
  148. . ' t="' . $oneReportRow['time'] . '"'
  149. . ' lt="0"'
  150. . ' ts="' . $oneReportRow['timestamp'] . '"'
  151. . ' s="' . ($oneReportRow['success'] ? 'true' : 'false') . '"'
  152. . ' lb="' . $oneReportRow['title'] . '"'
  153. . ' rc="' . $oneReportRow['exit_code'] . '"'
  154. . ' rm=""'
  155. . ' tn="Sample ' . ($index + 1) . '"'
  156. . ' dt="text"'
  157. . '/>';
  158. }
  159. $xml[] = '</testResults>';
  160. file_put_contents($reportFile, implode(PHP_EOL, $xml));
  161. }
  162. /**
  163. * Retrieve error messages from the report
  164. *
  165. * @param array $reportRows
  166. * @return array
  167. */
  168. protected function _getReportErrors(array $reportRows)
  169. {
  170. $result = array();
  171. foreach ($reportRows as $oneReportRow) {
  172. if (!$oneReportRow['success']) {
  173. $result[] = $oneReportRow['output'];
  174. }
  175. }
  176. return $result;
  177. }
  178. }