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