-
Notifications
You must be signed in to change notification settings - Fork 2k
/
HtmlTemplateExporter.java
496 lines (428 loc) · 21.3 KB
/
HtmlTemplateExporter.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jmeter.report.dashboard;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.apache.commons.text.StringEscapeUtils;
import org.apache.jmeter.JMeter;
import org.apache.jmeter.report.config.ExporterConfiguration;
import org.apache.jmeter.report.config.GraphConfiguration;
import org.apache.jmeter.report.config.ReportGeneratorConfiguration;
import org.apache.jmeter.report.config.SubConfiguration;
import org.apache.jmeter.report.core.DataContext;
import org.apache.jmeter.report.core.TimeHelper;
import org.apache.jmeter.report.processor.ListResultData;
import org.apache.jmeter.report.processor.MapResultData;
import org.apache.jmeter.report.processor.ResultData;
import org.apache.jmeter.report.processor.ResultDataVisitor;
import org.apache.jmeter.report.processor.SampleContext;
import org.apache.jmeter.report.processor.ValueResultData;
import org.apache.jmeter.report.processor.graph.AbstractGraphConsumer;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.util.JOrphanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import freemarker.template.Configuration;
import freemarker.template.TemplateExceptionHandler;
/**
* The class HtmlTemplateExporter provides a data exporter that generates and
* processes template files using freemarker.
*
* @since 3.0
*/
public class HtmlTemplateExporter extends AbstractDataExporter {
private static final String CUSTOM_GRAPH_PREFIX = "custom_";
/** Format used for non null check of parameters. */
private static final String MUST_NOT_BE_NULL = "%s must not be null";
private static final Logger log = LoggerFactory.getLogger(HtmlTemplateExporter.class);
public static final String DATA_CTX_REPORT_TITLE = "reportTitle";
public static final String DATA_CTX_TESTFILE = "testFile";
public static final String DATA_CTX_BEGINDATE = "beginDate";
public static final String DATA_CTX_ENDDATE = "endDate";
public static final String DATA_CTX_TIMEZONE = "timeZone";
public static final String DATA_CTX_TIMEZONE_OFFSET = "timeZoneOffset";
public static final String DATA_CTX_OVERALL_FILTER = "overallFilter";
public static final String DATA_CTX_SHOW_CONTROLLERS_ONLY = "showControllersOnly";
public static final String DATA_CTX_RESULT = "result";
public static final String DATA_CTX_EXTRA_OPTIONS = "extraOptions";
public static final String DATA_CTX_SERIES_FILTER = "seriesFilter";
public static final String DATA_CTX_FILTERS_ONLY_SAMPLE_SERIES = "filtersOnlySampleSeries";
public static final String TIMESTAMP_FORMAT_MS = "ms";
// Template directory
private static final String TEMPLATE_DIR = "template_dir";
private static final String TEMPLATE_DIR_NAME_DEFAULT = "report-template";
// Output directory
static final String OUTPUT_DIR = "output_dir";
// Default output folder name
static final String OUTPUT_DIR_NAME_DEFAULT = "report-output";
/**
* Adds to context the value surrounding it with quotes
* @param key Key
* @param value Value
* @param context {@link DataContext}
*/
private static void addToContext(String key, Object value, DataContext context) {
if (value instanceof String) {
value = '"' + (String) value + '"';
}
context.put(key, value);
}
/**
* This class allows to customize data before exporting them
*/
private interface ResultCustomizer {
ResultData customizeResult(ResultData result);
}
/**
* This class allows to inject graph_options properties to the exported data
*/
private static class ExtraOptionsResultCustomizer implements ResultCustomizer {
private SubConfiguration extraOptions;
/**
* Sets the extra options to inject in the result data
*
* @param extraOptions to inject
*/
public final void setExtraOptions(SubConfiguration extraOptions) {
this.extraOptions = extraOptions;
}
/*
* (non-Javadoc)
*
* @see org.apache.jmeter.report.dashboard.HtmlTemplateExporter.
* ResultCustomizer#customizeResult(org.apache.jmeter.report.processor.
* ResultData)
*/
@Override
public ResultData customizeResult(ResultData result) {
MapResultData customizedResult = new MapResultData();
customizedResult.setResult(DATA_CTX_RESULT, result);
if (extraOptions == null) {
return customizedResult;
}
MapResultData extraResult = new MapResultData();
extraOptions.getProperties().forEach((key, value) ->
extraResult.setResult(key, new ValueResultData(value)));
customizedResult.setResult(DATA_CTX_EXTRA_OPTIONS, extraResult);
return customizedResult;
}
}
/** This class allows to check exported data */
private interface ResultChecker {
boolean checkResult(DataContext dataContext, ResultData result);
}
/** This class allows to detect empty graphs */
private static class EmptyGraphChecker implements ResultChecker {
private final boolean filtersOnlySampleSeries;
private final boolean showControllerSeriesOnly;
private final Pattern filterPattern;
private boolean excludesControllers;
private String graphId;
public final void setExcludesControllers(boolean excludesControllers) {
this.excludesControllers = excludesControllers;
}
public final void setGraphId(String graphId) {
this.graphId = graphId;
}
/**
* Instantiates a new EmptyGraphChecker.
*
* @param filtersOnlySampleSeries flag to control filter for samples
* @param showControllerSeriesOnly flag to control visibility of controller
* @param filterPattern to use
*/
public EmptyGraphChecker(boolean filtersOnlySampleSeries,
boolean showControllerSeriesOnly, Pattern filterPattern) {
this.filtersOnlySampleSeries = filtersOnlySampleSeries;
this.showControllerSeriesOnly = showControllerSeriesOnly;
this.filterPattern = filterPattern;
}
/** @see ResultChecker#checkResult(DataContext, ResultData) */
@Override
public boolean checkResult(DataContext dataContext, ResultData result) {
boolean supportsControllerDiscrimination = findValue(Boolean.class,
AbstractGraphConsumer.RESULT_SUPPORTS_CONTROLLERS_DISCRIMINATION,
result);
if (supportsControllerDiscrimination
&& showControllerSeriesOnly
&& excludesControllers) {
// Exporter shows controller series only
// whereas the current graph support controller
// discrimination and excludes controllers
log.warn("{} is set while the graph {} excludes controllers.",
ReportGeneratorConfiguration.EXPORTER_KEY_SHOW_CONTROLLERS_ONLY, graphId);
return false;
}
if (filterPattern == null) {
return true;
}
// Detect whether none series matches the series filter.
ResultData seriesResult = findData(AbstractGraphConsumer.RESULT_SERIES, result);
if (!(seriesResult instanceof ListResultData)) {
return true;
}
// Try to find at least one pattern matching
ListResultData seriesList = (ListResultData) seriesResult;
int count = seriesList.getSize();
int index = 0;
boolean matches = false;
while (index < count && !matches) {
ResultData currentResult = seriesList.get(index);
if (currentResult instanceof MapResultData) {
MapResultData seriesData = (MapResultData) currentResult;
String name = findValue(String.class,
AbstractGraphConsumer.RESULT_SERIES_NAME,
seriesData);
// Is the current series a controller series ?
boolean isController = findValue(Boolean.class,
AbstractGraphConsumer.RESULT_SERIES_IS_CONTROLLER,
seriesData);
matches = filterPattern.matcher(name).matches();
if (matches) {
// If the name matches pattern, other
// properties can discard the series
matches = !filtersOnlySampleSeries
|| !supportsControllerDiscrimination
|| isController
|| !showControllerSeriesOnly;
if(log.isDebugEnabled()) {
log.debug(
"name:{} matches pattern:{}, supportsControllerDiscrimination:{}, "
+ "isController:{}, showControllerSeriesOnly:{}",
name, filterPattern.pattern(),
supportsControllerDiscrimination,
isController,
showControllerSeriesOnly);
}
} else {
// If the name does not match the pattern,
// other properties can hold the series
matches = filtersOnlySampleSeries
&& !supportsControllerDiscrimination;
if (log.isDebugEnabled()) {
log.debug("name:{} does not match pattern:{}, filtersOnlySampleSeries:{},"
+ " supportsControllerDiscrimination:{}",
name, filterPattern.pattern(),
filtersOnlySampleSeries,
supportsControllerDiscrimination);
}
}
}
index++;
}
if (!matches) {
log.warn("No series matches the series_filter: {} in graph: {}",
ReportGeneratorConfiguration.EXPORTER_KEY_SERIES_FILTER, graphId);
return false;
}
return true;
}
}
private static boolean htmlReportFileFilter(File file) {
String fileName = file.getName();
boolean isIndexHtmlFile = file.isFile() && fileName.equals("index.html");
boolean isContentOrAdmin = fileName.equals("content") || fileName.startsWith("sbadmin2-");
return isIndexHtmlFile || (file.isDirectory() && isContentOrAdmin);
}
/**
* @see DataExporter#export(SampleContext, File, ReportGeneratorConfiguration)
*/
@Override
public void export(SampleContext context, File file,
ReportGeneratorConfiguration configuration) throws ExportException {
Validate.notNull(context, MUST_NOT_BE_NULL, "context");
Validate.notNull(file, MUST_NOT_BE_NULL, "file");
Validate.notNull(configuration, MUST_NOT_BE_NULL, "configuration");
log.debug("Start template processing");
// Create data context and populate it
DataContext dataContext = new DataContext();
// Get the configuration of the current exporter
final ExporterConfiguration exportCfg =
configuration.getExportConfigurations().get(getName());
// Get template directory property value
File templateDirectory = getPropertyFromConfig(
exportCfg,
TEMPLATE_DIR,
new File(JMeterUtils.getJMeterBinDir(), TEMPLATE_DIR_NAME_DEFAULT),
File.class);
if (!templateDirectory.isDirectory()) {
String message = String.format(
"\"%s\" is not a valid template directory",
templateDirectory.getAbsolutePath());
log.error(message);
throw new ExportException(message);
}
// Get output directory property value
File outputDir = getPropertyFromConfig(exportCfg, OUTPUT_DIR,
new File(JMeterUtils.getJMeterBinDir(), OUTPUT_DIR_NAME_DEFAULT), File.class);
String globallyDefinedOutputDir = JMeterUtils.getProperty(JMeter.JMETER_REPORT_OUTPUT_DIR_PROPERTY);
if(!StringUtils.isEmpty(globallyDefinedOutputDir)) {
outputDir = new File(globallyDefinedOutputDir);
}
JOrphanUtils.canSafelyWriteToFolder(outputDir, HtmlTemplateExporter::htmlReportFileFilter);
if (log.isInfoEnabled()) {
log.info("Will generate dashboard in folder: {}", outputDir.getAbsolutePath());
}
// Add a flag defining if only sample series are filtered to the context
final boolean filtersOnlySampleSeries = exportCfg.filtersOnlySampleSeries();
addToContext(
DATA_CTX_FILTERS_ONLY_SAMPLE_SERIES,
filtersOnlySampleSeries,
dataContext);
// Add the series filter to the context
final String seriesFilter = exportCfg.getSeriesFilter();
Pattern filterPattern = null;
if (StringUtils.isNotBlank(seriesFilter)) {
try {
filterPattern = Pattern.compile(seriesFilter);
} catch (PatternSyntaxException ex) {
log.error("Invalid series filter: '{}', {}", seriesFilter, ex.getDescription());
}
}
addToContext(DATA_CTX_SERIES_FILTER, seriesFilter, dataContext);
// Add the flag defining whether only controller series are displayed
final boolean showControllerSeriesOnly = exportCfg.showControllerSeriesOnly();
addToContext(
DATA_CTX_SHOW_CONTROLLERS_ONLY,
showControllerSeriesOnly,
dataContext);
JsonizerVisitor jsonizer = new JsonizerVisitor();
Map<String, Object> storedData = context.getData();
// Add begin date consumer result to the data context
addResultToContext(
ReportGenerator.BEGIN_DATE_CONSUMER_NAME, storedData, dataContext, jsonizer);
// Add end date summary consumer result to the data context
addResultToContext(
ReportGenerator.END_DATE_CONSUMER_NAME, storedData, dataContext, jsonizer);
// Add Apdex summary consumer result to the data context
addResultToContext(
ReportGenerator.APDEX_SUMMARY_CONSUMER_NAME, storedData, dataContext, jsonizer);
// Add errors summary consumer result to the data context
addResultToContext(
ReportGenerator.ERRORS_SUMMARY_CONSUMER_NAME, storedData, dataContext, jsonizer);
// Add requests summary consumer result to the data context
addResultToContext(
ReportGenerator.REQUESTS_SUMMARY_CONSUMER_NAME, storedData, dataContext, jsonizer);
// Add statistics summary consumer result to the data context
addResultToContext(
ReportGenerator.STATISTICS_SUMMARY_CONSUMER_NAME, storedData, dataContext, jsonizer);
// Add Top 5 errors by sampler consumer result to the data context
addResultToContext(
ReportGenerator.TOP5_ERRORS_BY_SAMPLER_CONSUMER_NAME, storedData, dataContext, jsonizer);
// Collect graph results from sample context and transform them into
// Json strings to inject in the data context
ExtraOptionsResultCustomizer customizer = new ExtraOptionsResultCustomizer();
EmptyGraphChecker checker =
new EmptyGraphChecker(filtersOnlySampleSeries, showControllerSeriesOnly, filterPattern);
Map<String, GraphConfiguration> mapConfiguration = new HashMap<>();
DataContext customGraphs = new DataContext();
for (Map.Entry<String, GraphConfiguration> graphEntry : configuration.getGraphConfigurations().entrySet()) {
final String graphId = graphEntry.getKey();
final GraphConfiguration graphConfiguration = graphEntry.getValue();
// Initialize customizer and checker
customizer.setExtraOptions(exportCfg.getGraphExtraConfigurations().get(graphId));
checker.setExcludesControllers(graphConfiguration.excludesControllers());
checker.setGraphId(graphId);
mapConfiguration.put(graphId, graphConfiguration);
if (graphId.startsWith(CUSTOM_GRAPH_PREFIX)) {
addResultToContext(
graphId, storedData, customGraphs, jsonizer, customizer, checker);
} else {
// Export graph data
addResultToContext(
graphId, storedData, dataContext, jsonizer, customizer, checker);
}
}
dataContext.put("graphConfigurations", mapConfiguration);
dataContext.put("customsGraphsData", customGraphs);
// Replace the begin date with its formatted string and store the old timestamp
long oldTimestamp = formatTimestamp(
ReportGenerator.BEGIN_DATE_CONSUMER_NAME, dataContext);
// Replace the end date with its formatted string
formatTimestamp(ReportGenerator.END_DATE_CONSUMER_NAME, dataContext);
// Add time zone offset (that matches the begin date) to the context
TimeZone timezone = TimeZone.getDefault();
addToContext(
DATA_CTX_TIMEZONE_OFFSET,
timezone.getOffset(oldTimestamp),
dataContext);
// Add report title to the context
if (StringUtils.isNotEmpty(configuration.getReportTitle())) {
dataContext.put(DATA_CTX_REPORT_TITLE, StringEscapeUtils.escapeHtml4(configuration.getReportTitle()));
}
// Add the test file name to the context
addToContext(DATA_CTX_TESTFILE, file.getName(), dataContext);
// Add the overall filter property to the context
addToContext(DATA_CTX_OVERALL_FILTER, configuration.getSampleFilter(), dataContext);
// Walk template directory to copy files and process templated ones
Configuration templateCfg = new Configuration(Configuration.VERSION_2_3_30);
try {
templateCfg.setDirectoryForTemplateLoading(templateDirectory);
templateCfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
if (log.isInfoEnabled()) {
log.info("Report will be generated in: {}, creating folder structure", outputDir.getAbsolutePath());
}
FileUtils.forceMkdir(outputDir);
TemplateVisitor visitor = new TemplateVisitor(
templateDirectory.toPath(),
outputDir.toPath(),
templateCfg,
dataContext);
Files.walkFileTree(templateDirectory.toPath(), visitor);
} catch (IOException ex) {
throw new ExportException("Unable to process template files.", ex);
}
log.debug("End of template processing");
}
private static <T> void addResultToContext(
String resultKey, Map<String, Object> storage,
DataContext dataContext, ResultDataVisitor<T> visitor) {
addResultToContext(resultKey, storage, dataContext, visitor, null, null);
}
private static <T> void addResultToContext(
String resultKey, Map<String, Object> storage, DataContext dataContext,
ResultDataVisitor<T> visitor, ResultCustomizer customizer, ResultChecker checker) {
Object data = storage.get(resultKey);
if (data instanceof ResultData) {
ResultData result = (ResultData) data;
if (checker != null) {
checker.checkResult(dataContext, result);
}
if (customizer != null) {
result = customizer.customizeResult(result);
}
dataContext.put(resultKey, result.accept(visitor));
}
}
private static long formatTimestamp(String key, DataContext context) {
// FIXME Why convert to double then long (rounding ?)
double result = Double.parseDouble((String) context.get(key));
long timestamp = (long) result;
// Quote the string to respect Json spec.
context.put(key, '"' + TimeHelper.formatTimeStamp(timestamp) + '"');
return timestamp;
}
}