-
-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathXmlDecoder.php
More file actions
249 lines (209 loc) · 7.93 KB
/
Copy pathXmlDecoder.php
File metadata and controls
249 lines (209 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
/*
* This file is part of the PHPBench package
*
* (c) Daniel Leech <daniel@dantleech.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace PhpBench\Serializer;
use PhpBench\Dom\Document;
use PhpBench\Environment\Information;
use PhpBench\Model\Benchmark;
use PhpBench\Model\Error;
use PhpBench\Model\ParameterSet;
use PhpBench\Model\Subject;
use PhpBench\Model\Suite;
use PhpBench\Model\SuiteCollection;
use PhpBench\Model\Variant;
use PhpBench\PhpBench;
/**
* Encodes the Suite object graph into an XML document.
*/
class XmlDecoder
{
/**
* Decode a PHPBench XML document into a SuiteCollection.
*
* @param Document $document
*
* @return SuiteCollection
*/
public function decode(Document $document)
{
$suites = [];
foreach ($document->query('//suite') as $suiteEl) {
$suites[] = $this->processSuite($suiteEl);
}
return new SuiteCollection($suites);
}
/**
* Return a SuiteCollection from a number of PHPBench xml files.
*
* @param string[] $files
*
* @return SuiteCollection
*/
public function decodeFiles(array $files)
{
// combine into one document.
//
$suiteDocument = new Document('phpbench');
$rootEl = $suiteDocument->createRoot('phpbench');
foreach ($files as $file) {
$fileDom = new Document();
$fileDom->load($file);
foreach ($fileDom->query('./suite') as $suiteEl) {
$importedEl = $suiteDocument->importNode($suiteEl, true);
$rootEl->appendChild($importedEl);
}
}
return $this->decode($suiteDocument);
}
private function processSuite(\DOMElement $suiteEl)
{
$suite = new Suite(
$suiteEl->getAttribute('context'),
new \DateTime($suiteEl->getAttribute('date')),
$suiteEl->getAttribute('config-path'),
[],
[],
$suiteEl->getAttribute('uuid')
);
$informations = [];
foreach ($suiteEl->query('./env/*') as $envEl) {
$name = $envEl->nodeName;
$info = [];
foreach ($envEl->attributes as $iName => $valueAttr) {
$info[$iName] = $valueAttr->nodeValue;
}
$informations[$name] = new Information($name, $info);
}
$resultClasses = [];
foreach ($suiteEl->query('//result') as $resultEl) {
$class = $resultEl->getAttribute('class');
if (!class_exists($class)) {
throw new \RuntimeException(sprintf(
'XML file defines a non-existing result class "%s" - maybe you are missing an extension?',
$class
));
}
$resultClasses[$resultEl->getAttribute('key')] = $class;
}
$suite->setEnvInformations($informations);
foreach ($suiteEl->query('./benchmark') as $benchmarkEl) {
$benchmark = $suite->createBenchmark(
$benchmarkEl->getAttribute('class')
);
$this->processBenchmark($benchmark, $benchmarkEl, $resultClasses);
}
return $suite;
}
private function processBenchmark(Benchmark $benchmark, \DOMElement $benchmarkEl, array $resultClasses)
{
foreach ($benchmarkEl->query('./subject') as $subjectEl) {
$subject = $benchmark->createSubject($subjectEl->getAttribute('name'));
$this->processSubject($subject, $subjectEl, $resultClasses);
}
}
private function processSubject(Subject $subject, \DOMElement $subjectEl, array $resultClasses)
{
$groups = [];
foreach ($subjectEl->query('./group') as $groupEl) {
$groups[] = $groupEl->getAttribute('name');
}
$subject->setGroups($groups);
// TODO: These attributes should be on the subject, see
// https://github.com/phpbench/phpbench/issues/307
foreach ($subjectEl->query('./variant') as $variantEl) {
$subject->setSleep($variantEl->getAttribute('sleep'));
$subject->setOutputTimeUnit($variantEl->getAttribute('output-time-unit'));
$subject->setOutputTimePrecision($variantEl->getAttribute('output-time-precision'));
$subject->setOutputMode($variantEl->getAttribute('output-mode'));
$subject->setRetryThreshold($variantEl->getAttribute('retry-threshold'));
break;
}
foreach ($subjectEl->query('./variant') as $index => $variantEl) {
$parameters = $this->getParameters($variantEl);
$parameterSet = new ParameterSet($index, $parameters);
$stats = $this->getComputedStats($variantEl);
$variant = $subject->createVariant($parameterSet, $variantEl->getAttribute('revs'), $variantEl->getAttribute('warmup'), $stats);
$this->processVariant($variant, $variantEl, $resultClasses);
}
}
private function getComputedStats(\DOMElement $element)
{
$stats = [];
foreach ($element->query('./stats') as $statsEl) {
foreach ($statsEl->attributes as $key => $attribute) {
$stats[$key] = $attribute->nodeValue;
}
}
return $stats;
}
private function getParameters(\DOMElement $element)
{
$parameters = [];
foreach ($element->query('./parameter') as $parameterEl) {
$name = $parameterEl->getAttribute('name');
if ($parameterEl->getAttribute('type') === 'collection') {
$parameters[$name] = $this->getParameters($parameterEl);
continue;
}
$parameters[$name] = $parameterEl->getAttribute('value');
}
return $parameters;
}
private function processVariant(Variant $variant, \DOMElement $variantEl, array $resultClasses)
{
$errorEls = $variantEl->query('.//error');
if ($errorEls->length) {
$errors = [];
foreach ($errorEls as $errorEl) {
$error = new Error(
$errorEl->nodeValue,
$errorEl->getAttribute('exception-class'),
$errorEl->getAttribute('code'),
$errorEl->getAttribute('file'),
$errorEl->getAttribute('line'),
'' // we don't serialize the trace..
);
$errors[] = $error;
}
$variant->createErrorStack($errors);
return;
}
foreach ($variantEl->query('./iteration') as $iterationEl) {
$results = [];
foreach ($iterationEl->attributes as $attributeEl) {
$name = $attributeEl->name;
if (false === strpos($name, '-')) {
throw new \RuntimeException(sprintf(
'Expected attribute name to have a result key prefix, got "%s".',
$name
));
}
$prefix = substr($name, 0, strpos($name, '-'));
if (!isset($resultClasses[$prefix])) {
throw new \RuntimeException(sprintf(
'No result class was provided with key "%s" for attribute "%s"',
$prefix, $name
));
}
$suffix = substr($name, strpos($name, '-') + 1);
$results[$prefix][str_replace('-', '_', $suffix)] = $attributeEl->value;
}
$iteration = $variant->createIteration();
foreach ($results as $resultKey => $resultData) {
$iteration->setResult(call_user_func_array([
$resultClasses[$resultKey],
'fromArray',
], [$resultData]));
}
}
// TODO: Serialize statistics ..
$variant->computeStats();
}
}