1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
43
44
45
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)
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
69
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
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
89 bh.consume(e);
90 }
91 }
92
93
94
95
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
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
116
117 @Benchmark
118 public void concurrentConstructionOld(Blackhole bh) {
119 try {
120 PlexusConfiguration config = new XmlPlexusConfigurationOld(testNode);
121
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
132
133 @Benchmark
134 public void concurrentConstructionNew(Blackhole bh) {
135 PlexusConfiguration config = new XmlPlexusConfiguration(testNode);
136
137 for (int i = 0; i < config.getChildCount(); i++) {
138 bh.consume(config.getChild(i).getName());
139 }
140 }
141
142
143
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 }