View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2012-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
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   * JMH benchmark for {@link XSLDocument#transform(XML)}.
20   *
21   * <p>Three scenarios:
22   * <ul>
23   *   <li>{@link #reuseInstance} — same {@link XSLDocument} reused every call</li>
24   *   <li>{@link #withParamEachCall} — new instance via {@code .with()} each call</li>
25   *   <li>{@link #freshInstanceEachCall} — brand-new {@link XSLDocument} every call</li>
26   * </ul>
27   *
28   * @since 0.35.0
29   * @checkstyle AbbreviationAsWordInNameCheck (15 lines)
30   * @checkstyle NonStaticMethodCheck (100 lines)
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       * XSL stylesheet with a {@code step} parameter.
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       * Input XML document.
58       */
59      private static final XML INPUT = new XMLDocument(
60          "<root><item>hello</item></root>"
61      );
62  
63      /**
64       * Reused XSL instance.
65       */
66      private static final XSL XSL = new XSLDocument(
67          XSLDocumentBenchmark.STYLESHEET
68      );
69  
70      /**
71       * Same {@link XSLDocument} instance reused on every call.
72       * @return Transformed XML
73       */
74      @Benchmark
75      public final XML reuseInstance() {
76          return XSLDocumentBenchmark.XSL.transform(XSLDocumentBenchmark.INPUT);
77      }
78  
79      /**
80       * New {@link XSLDocument} via {@code .with("step", n)} on every call.
81       * @return Transformed XML
82       */
83      @Benchmark
84      public final XML withParamEachCall() {
85          return XSLDocumentBenchmark.XSL
86              .with("step", 1)
87              .transform(XSLDocumentBenchmark.INPUT);
88      }
89  
90      /**
91       * Brand-new {@link XSLDocument} constructed on every call.
92       * @return Transformed XML
93       */
94      @Benchmark
95      public final XML freshInstanceEachCall() {
96          return new XSLDocument(XSLDocumentBenchmark.STYLESHEET)
97              .transform(XSLDocumentBenchmark.INPUT);
98      }
99  
100     /**
101      * {@link XSLDocument#toString()} on a reused instance.
102      * The result is computed once and returned from a
103      * {@link org.cactoos.scalar.Sticky} cache on every subsequent call.
104      * @return Formatted XSL string
105      */
106     @Benchmark
107     public final String toStringCached() {
108         return XSLDocumentBenchmark.XSL.toString();
109     }
110 
111 }