/XSLTBenchmarking/TestsRunner/TestRunner.php

https://github.com/masicek/XSLT-Benchmarking-XSLTMark-II · PHP · 215 lines · 119 code · 31 blank · 65 comment · 6 complexity · 584c0bbdbd6a43ead553fcd622bee382 MD5 · raw file

  1. <?php
  2. /**
  3. * XSLT Benchmarking
  4. * @link https://github.com/masicek/XSLT-Benchmarking
  5. * @author Viktor Mašíček <viktor@masicek.net>
  6. * @license "New" BSD License
  7. */
  8. namespace XSLTBenchmarking\TestsRunner;
  9. require_once ROOT . '/Microtime.php';
  10. require_once ROOT . '/Exceptions.php';
  11. require_once LIBS . '/PhpPath/PhpPath.min.php';
  12. require_once ROOT . '/Printer.php';
  13. use PhpPath\P;
  14. use XSLTBenchmarking\Printer;
  15. /**
  16. * Class for run one test
  17. *
  18. * @author Viktor Mašíček <viktor@masicek.net>
  19. */
  20. class TestRunner
  21. {
  22. /**
  23. * Factory class for making new objects
  24. *
  25. * @var \XSLTBenchmarking\Factory
  26. */
  27. private $factory;
  28. /**
  29. * Object for runnig XSLT transformation
  30. *
  31. * @var \XSLTBenchmarking\TestsRunner\Processor
  32. */
  33. private $processor;
  34. /**
  35. * List of processors selected for testing
  36. *
  37. * @var array
  38. */
  39. private $processorsNames;
  40. /**
  41. * Number of repeatig for each test and processor
  42. *
  43. * @var int
  44. */
  45. private $repeating;
  46. /**
  47. * Object for control that generated file is same as expected
  48. *
  49. * @var \XSLTBenchmarking\TestsRunner\Controlor
  50. */
  51. private $controlor;
  52. /**
  53. * Path of temporary directory for generating output files by processors
  54. *
  55. * @var string
  56. */
  57. private $tmpDir;
  58. /**
  59. * Object configuration
  60. *
  61. * @param \XSLTBenchmarking\Factory $factory Factory class for making new objects
  62. * @param \XSLTBenchmarking\TestsRunner\Processor $processor Class for parse one template in one processor
  63. * @param array|TRUE $processorsSelected List of tested processors
  64. * @param array $processors Exclude List of tested processors, that we want exclude form tested processors
  65. * @param int $repeating Number of repeatig for each test and processor
  66. * @param string $tmpDir Path of temporary directory
  67. */
  68. public function __construct(
  69. \XSLTBenchmarking\Factory $factory,
  70. \XSLTBenchmarking\TestsRunner\Processor $processor,
  71. $processorsSelected,
  72. array $processorsExclude,
  73. $repeating,
  74. \XSLTBenchmarking\TestsRunner\Controlor $controlor,
  75. $tmpDir
  76. )
  77. {
  78. $tmpDir = P::mcd($tmpDir);
  79. $processorsAvailable = array_keys($processor->getAvailable());
  80. if ($processorsSelected === TRUE)
  81. {
  82. $processorsSelected = $processorsAvailable;
  83. }
  84. $processorsFinal = $processorsSelected;
  85. foreach ($processorsExclude as $processorExclude)
  86. {
  87. $key = array_search($processorExclude, $processorsFinal);
  88. if ($key !== FALSE)
  89. {
  90. unset($processorsFinal[$key]);
  91. }
  92. }
  93. $processorsFinal = array_values($processorsFinal);
  94. // check correct set of processors
  95. foreach ($processorsSelected as $processorSelected)
  96. {
  97. if (!in_array($processorSelected, $processorsAvailable))
  98. {
  99. throw new \XSLTBenchmarking\InvalidArgumentException('Unknown processor name "' . $processorSelected . '"');
  100. }
  101. }
  102. $this->factory = $factory;
  103. $this->processor = $processor;
  104. $this->processorsNames = $processorsFinal;
  105. $this->repeating = $repeating;
  106. $this->controlor = $controlor;
  107. $this->tmpDir = $tmpDir;
  108. }
  109. /**
  110. * Run one test
  111. *
  112. * @param \XSLTBenchmarking\TestsRunner\Test $test
  113. * @param bool $verbose Print information about each run test
  114. *
  115. * @return \XSLTBenchmarking\Reports\Report
  116. */
  117. public function run(\XSLTBenchmarking\TestsRunner\Test $test, $verbose = FALSE)
  118. {
  119. $templatePath = $test->getTemplatePath();
  120. $couplesPaths = $test->getCouplesPaths();
  121. $report = $this->factory->getReport(
  122. $test->getName(),
  123. $templatePath
  124. );
  125. foreach ($this->processorsNames as $processorName)
  126. {
  127. if ($verbose)
  128. {
  129. Printer::info(' ' . $processorName);
  130. }
  131. foreach ($couplesPaths as $xmlInputPath => $expectedOutputPath)
  132. {
  133. // unique filename
  134. $pathInfo = pathinfo($expectedOutputPath);
  135. $microtime = \XSLTBenchmarking\Microtime::now();
  136. $microtime = str_replace('.', '-', $microtime);
  137. $filename = isset($pathInfo['filename']) ? ($pathInfo['filename'] . '-') : '';
  138. $extension = isset($pathInfo['extension']) ? ('.' . $pathInfo['extension']) : '';
  139. $filename = $filename . $microtime . $extension;
  140. $outputPath = P::m($this->tmpDir, $filename);
  141. // run transformations
  142. $result = $this->processor->run(
  143. $processorName,
  144. $templatePath,
  145. $xmlInputPath,
  146. $outputPath,
  147. $this->repeating
  148. );
  149. // set times and success
  150. if (is_array($result))
  151. {
  152. $success = TRUE;
  153. $spendTimes = $result['times'];
  154. $memoryUsage = $result['memory'];
  155. }
  156. else
  157. {
  158. $success = $result;
  159. $spendTimes = array();
  160. $memoryUsage = array();
  161. }
  162. // set correctness
  163. $correctness = FALSE;
  164. if ($success === TRUE)
  165. {
  166. $correctness = $this->controlor->isSame($outputPath, $expectedOutputPath);
  167. }
  168. // reported results
  169. $report->addRecord(
  170. $processorName,
  171. $xmlInputPath,
  172. $expectedOutputPath,
  173. $outputPath,
  174. $success,
  175. $correctness,
  176. $spendTimes,
  177. $memoryUsage,
  178. $this->repeating
  179. );
  180. }
  181. }
  182. return $report;
  183. }
  184. }