/
JtlAbstractSampleReader.java
303 lines (243 loc) · 9.84 KB
/
JtlAbstractSampleReader.java
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* JMeter Report Server
* Copyright (C) 2010 eXcentia
* dev@sonar.codehaus.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package es.excentia.jmeter.report.server.testresults;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.apache.xmlbeans.XmlValidationError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import es.excentia.jmeter.report.client.serialization.StreamReader;
import es.excentia.jmeter.report.client.util.JavaUtil;
import es.excentia.jmeter.report.server.testresults.xmlbeans.AbstractSample;
import es.excentia.jmeter.report.server.testresults.xmlbeans.TestResultsDocument;
/**
* Reader to get first level Samples and HttpSamples from jtl xml file.
* This reader is at the most lower level and his function is to
* translate xml into objects using xmlbeans.
*
* Read method returns only one first level Sample or HttpSample
* for each call, but each returned object will contain
* descendant elements info inside.
*
* Read process with stax and xmlbeans extracted from
* http://www.devx.com/xml/Article/34037/1954 by Michael Pilone.
*
* @author cfillol
*/
public class JtlAbstractSampleReader extends StreamReader<AbstractSample> {
public static final String NAMESPACE = "http://xmlbeans.testresults.server.report.jmeter.excentia.es";
// It will be rare to have one first level sample bigger than 5MB ...
private static final int FIRST_LEVEL_SAMPLE_MAX_SIZE = 5 * 1024 * 1024;
private static final String HTTPSAMPLE_TAG_NAME = "httpSample";
private static final String SAMPLE_TAG_NAME = "sample";
private static final Logger LOG = LoggerFactory
.getLogger(JtlAbstractSampleReader.class);
private static final boolean LOG_DEBUG = LOG.isDebugEnabled();
private static final boolean LOG_TRACE = LOG.isTraceEnabled();
private XMLEventReader reader;
private XMLEventWriter writer;
private XmlOptions options;
private XmlOptions validationOptions;
private List<XmlValidationError> validationErrors = new ArrayList<XmlValidationError>();
private ResettableStringWriter swriter;
private int readCount = 0;
private int lastAvailableBytes = 0;
/**
* Ability for reading JTL result files when jmeter
* test is running running is only available for java 1.6 and later.
* See methods getCanReadRunningTestJtl and takeCareWhenNearToJTLEnd.
*/
private int growingJtlWaitTime = 0;
private synchronized boolean getCanReadRunningTestJtl() {
boolean canReadRunning = growingJtlWaitTime > 0;
if (canReadRunning) {
canReadRunning =
JavaUtil.getJREMajorVersion()==1 && JavaUtil.getJREMinorVersion()>=6 ||
JavaUtil.getJREMajorVersion()>1;
if (!canReadRunning) {
LOG.warn("Ability for reading JTL results when jmeter test is running "
+ "is NOT AVAILABLE. You will need at least java 1.6 for this.");
}
}
return canReadRunning;
}
public JtlAbstractSampleReader(InputStream is) {
this(is, 0);
}
public JtlAbstractSampleReader(InputStream is, int growingJtlWaitTime) {
super(is);
if (LOG_DEBUG) {
LOG.debug("Creating JtlAbstractSampleReader ...");
}
try {
this.growingJtlWaitTime = growingJtlWaitTime;
// Create the input reader to read the file. We will
// use the defaults here, but it could be configured
// using the properties.
XMLInputFactory inFactory = XMLInputFactory.newInstance();
reader = inFactory.createXMLEventReader(is);
// Create an output writer. The writer uses the resetable
// buffer so we can use the same writer continuously.
XMLOutputFactory outFactory = XMLOutputFactory.newInstance();
outFactory.setProperty("javax.xml.stream.isRepairingNamespaces", true);
swriter = new ResettableStringWriter(FIRST_LEVEL_SAMPLE_MAX_SIZE);
writer = outFactory.createXMLEventWriter(swriter);
options = new XmlOptions();
Map<String, String> ns = new HashMap<String, String>();
ns.put("", NAMESPACE);
options.setLoadSubstituteNamespaces(ns);
options.setDocumentType(TestResultsDocument.TestResults.type);
validationOptions = new XmlOptions();
validationOptions.setErrorListener(validationErrors);
} catch (XMLStreamException e) {
if (LOG_DEBUG) {
LOG.debug("Cannot create JtlReader", e);
}
throw new JtlReaderException("Cannot create JtlReader", e);
}
}
/**
* From version 0.3, JMeterReportServer can read jtl files of tests that
* are still running. So, when reading, we have to take care if EOF is near
* because maybe we are reading a growing file and then we have to wait if
* there are not enough available information.
*
* Feature available only for java 1.6 or later. See SONARPLUGINS-2355.
*
* @throws IOException
*/
private void takeCareWhenNearToJTLEnd() throws IOException {
int availableBytes = is.available();
while (availableBytes < FIRST_LEVEL_SAMPLE_MAX_SIZE && availableBytes>lastAvailableBytes) {
LOG.debug(
"JTL EOF is near ("+availableBytes+" bytes). Maybe there is " +
"a jmeter test in process. We'll wait "+growingJtlWaitTime+" secs."
);
try {
Thread.sleep(growingJtlWaitTime * 1000);
} catch (InterruptedException e) {
// Check remaining data size again ...
}
lastAvailableBytes = availableBytes;
availableBytes = is.available();
}
if (availableBytes >= FIRST_LEVEL_SAMPLE_MAX_SIZE) {
lastAvailableBytes = 0;
}
// else
// Available JTL bytes not growing after growingJtlWaitTime
// JMeter tests must be finished
}
/**
* Returns one first level Sample or one HttpSample for each call,
* that contains child info inside.
*/
@Override
public AbstractSample read() {
try {
if (getCanReadRunningTestJtl()) {
takeCareWhenNearToJTLEnd();
}
// Begin reading events
int sampleDepth = 0;
while (reader.hasNext()) {
XMLEvent evt = reader.nextEvent();
if (evt.isStartElement()) {
StartElement elem = (StartElement) evt;
String tagName = elem.getName().getLocalPart();
if (HTTPSAMPLE_TAG_NAME.equals(tagName)
|| SAMPLE_TAG_NAME.equals(tagName)) {
// Empieza un HttpSample ...
sampleDepth++;
}
}
if (sampleDepth > 0) {
// Sample o HttpSample. Take xml from sample and all its descendants
writer.add(evt);
} else {
// Root node. Discard info but include text into xml
// so it will be valid
writer.add(evt);
writer.flush();
swriter.reset();
}
if (evt.isEndElement()) {
EndElement elem = (EndElement) evt;
String tagName = elem.getName().getLocalPart();
if (HTTPSAMPLE_TAG_NAME.equals(tagName)
|| SAMPLE_TAG_NAME.equals(tagName)) {
// HttpSample or Sample ends ...
readCount++; // Update read elements counter
if (sampleDepth == 1) {
// End of parent Sample/HttpSample
// Save xml and return object
// Get xml from buffer and reset
writer.flush();
String sampleXml = swriter.reset();
if (LOG_TRACE) {
LOG.trace(sampleXml);
}
// Convert xml into XmlBeans object
// We have to use container node TestResults to save inner html
TestResultsDocument.TestResults testResults = (TestResultsDocument.TestResults) XmlObject.Factory
.parse(sampleXml, options);
if (!testResults.validate(validationOptions)) {
throw new JtlReaderException(validationErrors.get(0).toString());
}
// Return read sample
if (HTTPSAMPLE_TAG_NAME.equals(tagName)) {
return testResults.getHttpSampleArray(0);
} else {
return testResults.getSampleArray(0);
}
}
sampleDepth--;
}
}
}
if (LOG_DEBUG) {
LOG.debug(String.format("JtlAbstractSampleReader read %d nodes",
readCount));
}
return null;
} catch (XMLStreamException e) {
throw new JtlReaderException(e);
} catch (XmlException e) {
throw new JtlReaderException(e);
} catch (IOException e) {
throw new JtlReaderException(e);
}
}
}