View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.internal.xml;
20  
21  import java.util.List;
22  import java.util.Map;
23  import java.util.concurrent.TimeUnit;
24  
25  import org.apache.maven.api.xml.XmlNode;
26  import org.codehaus.plexus.configuration.PlexusConfiguration;
27  import org.openjdk.jmh.annotations.Benchmark;
28  import org.openjdk.jmh.annotations.BenchmarkMode;
29  import org.openjdk.jmh.annotations.Fork;
30  import org.openjdk.jmh.annotations.Group;
31  import org.openjdk.jmh.annotations.Measurement;
32  import org.openjdk.jmh.annotations.Mode;
33  import org.openjdk.jmh.annotations.OutputTimeUnit;
34  import org.openjdk.jmh.annotations.Scope;
35  import org.openjdk.jmh.annotations.Setup;
36  import org.openjdk.jmh.annotations.State;
37  import org.openjdk.jmh.annotations.Threads;
38  import org.openjdk.jmh.annotations.Warmup;
39  import org.openjdk.jmh.infra.Blackhole;
40  
41  /**
42   * JMH benchmarks for testing thread safety and concurrent performance.
43   *
44   * This benchmark specifically tests the thread safety improvements in the new implementation
45   * by running concurrent operations that would cause race conditions in the old version.
46   */
47  @BenchmarkMode(Mode.Throughput)
48  @OutputTimeUnit(TimeUnit.SECONDS)
49  @State(Scope.Benchmark)
50  @Fork(1)
51  @Warmup(iterations = 3, time = 2, timeUnit = TimeUnit.SECONDS)
52  @Measurement(iterations = 5, time = 3, timeUnit = TimeUnit.SECONDS)
53  @Threads(4) // Test with multiple threads to expose race conditions
54  public class XmlPlexusConfigurationConcurrencyBenchmark {
55  
56      private XmlNode testNode;
57      private PlexusConfiguration configOld;
58      private PlexusConfiguration configNew;
59  
60      @Setup
61      public void setup() {
62          testNode = createTestNode();
63          configOld = new XmlPlexusConfigurationOld(testNode);
64          configNew = new XmlPlexusConfiguration(testNode);
65      }
66  
67      /**
68       * Test concurrent child access with old implementation
69       * This may expose race conditions and inconsistent behavior
70       */
71      @Benchmark
72      @Group("concurrentAccessOld")
73      public void concurrentChildAccessOld(Blackhole bh) {
74          try {
75              for (int i = 0; i < configOld.getChildCount(); i++) {
76                  PlexusConfiguration child = configOld.getChild(i);
77                  bh.consume(child.getName());
78                  bh.consume(child.getValue());
79  
80                  // Access nested children to stress the implementation
81                  for (int j = 0; j < child.getChildCount(); j++) {
82                      PlexusConfiguration nested = child.getChild(j);
83                      bh.consume(nested.getName());
84                      bh.consume(nested.getValue());
85                  }
86              }
87          } catch (Exception e) {
88              // Old implementation may throw exceptions under concurrent access
89              bh.consume(e);
90          }
91      }
92  
93      /**
94       * Test concurrent child access with new implementation
95       * This should be thread-safe and perform consistently
96       */
97      @Benchmark
98      @Group("concurrentAccessNew")
99      public void concurrentChildAccessNew(Blackhole bh) {
100         for (int i = 0; i < configNew.getChildCount(); i++) {
101             PlexusConfiguration child = configNew.getChild(i);
102             bh.consume(child.getName());
103             bh.consume(child.getValue());
104 
105             // Access nested children to stress the implementation
106             for (int j = 0; j < child.getChildCount(); j++) {
107                 PlexusConfiguration nested = child.getChild(j);
108                 bh.consume(nested.getName());
109                 bh.consume(nested.getValue());
110             }
111         }
112     }
113 
114     /**
115      * Test concurrent construction and access with old implementation
116      */
117     @Benchmark
118     public void concurrentConstructionOld(Blackhole bh) {
119         try {
120             PlexusConfiguration config = new XmlPlexusConfigurationOld(testNode);
121             // Immediately access children to trigger potential race conditions
122             for (int i = 0; i < config.getChildCount(); i++) {
123                 bh.consume(config.getChild(i).getName());
124             }
125         } catch (Exception e) {
126             bh.consume(e);
127         }
128     }
129 
130     /**
131      * Test concurrent construction and access with new implementation
132      */
133     @Benchmark
134     public void concurrentConstructionNew(Blackhole bh) {
135         PlexusConfiguration config = new XmlPlexusConfiguration(testNode);
136         // Immediately access children to test thread safety
137         for (int i = 0; i < config.getChildCount(); i++) {
138             bh.consume(config.getChild(i).getName());
139         }
140     }
141 
142     /**
143      * Test concurrent attribute access
144      */
145     @Benchmark
146     public void concurrentAttributeAccessOld(Blackhole bh) {
147         try {
148             String[] attrNames = configOld.getAttributeNames();
149             for (String attrName : attrNames) {
150                 bh.consume(configOld.getAttribute(attrName));
151             }
152         } catch (Exception e) {
153             bh.consume(e);
154         }
155     }
156 
157     @Benchmark
158     public void concurrentAttributeAccessNew(Blackhole bh) {
159         String[] attrNames = configNew.getAttributeNames();
160         for (String attrName : attrNames) {
161             bh.consume(configNew.getAttribute(attrName));
162         }
163     }
164 
165     private XmlNode createTestNode() {
166         Map<String, String> rootAttrs = Map.of("id", "test-root", "version", "1.0", "type", "benchmark");
167 
168         List<XmlNode> children = List.of(
169                 XmlNode.newBuilder()
170                         .name("section1")
171                         .attributes(Map.of("name", "section1"))
172                         .children(List.of(
173                                 XmlNode.newInstance("item1", "value1"),
174                                 XmlNode.newInstance("item2", "value2"),
175                                 XmlNode.newInstance("item3", "value3")))
176                         .build(),
177                 XmlNode.newBuilder()
178                         .name("section2")
179                         .attributes(Map.of("name", "section2"))
180                         .children(
181                                 List.of(XmlNode.newInstance("item4", "value4"), XmlNode.newInstance("item5", "value5")))
182                         .build(),
183                 XmlNode.newBuilder()
184                         .name("section3")
185                         .attributes(Map.of("name", "section3"))
186                         .children(List.of(XmlNode.newBuilder()
187                                 .name("nested")
188                                 .children(List.of(
189                                         XmlNode.newInstance("deep1", "deep-value1"),
190                                         XmlNode.newInstance("deep2", "deep-value2")))
191                                 .build()))
192                         .build());
193 
194         return XmlNode.newBuilder()
195                 .name("root")
196                 .attributes(rootAttrs)
197                 .children(children)
198                 .build();
199     }
200 }