1
2
3
4
5 package com.jcabi.xml;
6
7 import java.util.concurrent.TimeUnit;
8 import org.openjdk.jmh.annotations.Benchmark;
9 import org.openjdk.jmh.annotations.BenchmarkMode;
10 import org.openjdk.jmh.annotations.Fork;
11 import org.openjdk.jmh.annotations.Measurement;
12 import org.openjdk.jmh.annotations.Mode;
13 import org.openjdk.jmh.annotations.OutputTimeUnit;
14 import org.openjdk.jmh.annotations.Scope;
15 import org.openjdk.jmh.annotations.State;
16 import org.openjdk.jmh.annotations.Warmup;
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 @Fork(1)
33 @BenchmarkMode(Mode.AverageTime)
34 @OutputTimeUnit(TimeUnit.MILLISECONDS)
35 @Warmup(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS)
36 @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
37 @State(Scope.Benchmark)
38 public class XSLDocumentBenchmark {
39
40
41
42
43 private static final String STYLESHEET = String.join(
44 "",
45 "<xsl:stylesheet version='2.0'",
46 " xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>",
47 "<xsl:param name='step' select='0'/>",
48 "<xsl:template match='/'>",
49 "<result step='{$step}'>",
50 "<xsl:copy-of select='node()'/>",
51 "</result>",
52 "</xsl:template>",
53 "</xsl:stylesheet>"
54 );
55
56
57
58
59 private static final XML INPUT = new XMLDocument(
60 "<root><item>hello</item></root>"
61 );
62
63
64
65
66 private static final XSL XSL = new XSLDocument(
67 XSLDocumentBenchmark.STYLESHEET
68 );
69
70
71
72
73
74 @Benchmark
75 public final XML reuseInstance() {
76 return XSLDocumentBenchmark.XSL.transform(XSLDocumentBenchmark.INPUT);
77 }
78
79
80
81
82
83 @Benchmark
84 public final XML withParamEachCall() {
85 return XSLDocumentBenchmark.XSL
86 .with("step", 1)
87 .transform(XSLDocumentBenchmark.INPUT);
88 }
89
90
91
92
93
94 @Benchmark
95 public final XML freshInstanceEachCall() {
96 return new XSLDocument(XSLDocumentBenchmark.STYLESHEET)
97 .transform(XSLDocumentBenchmark.INPUT);
98 }
99
100
101
102
103
104
105
106 @Benchmark
107 public final String toStringCached() {
108 return XSLDocumentBenchmark.XSL.toString();
109 }
110
111 }