1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.impl.model;
20
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.concurrent.TimeUnit;
24
25 import org.apache.maven.api.Session;
26 import org.apache.maven.api.model.Dependency;
27 import org.apache.maven.api.model.DependencyManagement;
28 import org.apache.maven.api.model.Model;
29 import org.apache.maven.api.model.Plugin;
30 import org.apache.maven.api.services.model.ModelValidator;
31 import org.apache.maven.impl.model.profile.SimpleProblemCollector;
32 import org.apache.maven.impl.standalone.ApiRunner;
33 import org.openjdk.jmh.annotations.Benchmark;
34 import org.openjdk.jmh.annotations.BenchmarkMode;
35 import org.openjdk.jmh.annotations.Level;
36 import org.openjdk.jmh.annotations.Measurement;
37 import org.openjdk.jmh.annotations.Mode;
38 import org.openjdk.jmh.annotations.OutputTimeUnit;
39 import org.openjdk.jmh.annotations.Param;
40 import org.openjdk.jmh.annotations.Scope;
41 import org.openjdk.jmh.annotations.Setup;
42 import org.openjdk.jmh.annotations.State;
43 import org.openjdk.jmh.annotations.Warmup;
44 import org.openjdk.jmh.runner.Runner;
45 import org.openjdk.jmh.runner.RunnerException;
46 import org.openjdk.jmh.runner.options.Options;
47 import org.openjdk.jmh.runner.options.OptionsBuilder;
48
49
50
51
52
53
54
55
56
57 @BenchmarkMode(Mode.AverageTime)
58 @OutputTimeUnit(TimeUnit.MICROSECONDS)
59 @Warmup(iterations = 3, time = 2)
60 @Measurement(iterations = 5, time = 3)
61 @State(Scope.Benchmark)
62 public class ModelValidationBenchmark {
63
64 @Param({"1", "10", "100"})
65 private int dependencyCount;
66
67 private Session session;
68 private ModelValidator validator;
69 private Model validModel;
70 private Model invalidModel;
71 private SimpleProblemCollector problemCollector;
72
73 @Setup(Level.Trial)
74 public void setup() {
75 session = ApiRunner.createSession();
76 validator = new DefaultModelValidator();
77
78
79 validModel = createValidModel(dependencyCount);
80 invalidModel = createInvalidModel(dependencyCount);
81 }
82
83 @Setup(Level.Invocation)
84 public void setupInvocation() {
85 problemCollector = new SimpleProblemCollector();
86 }
87
88
89
90
91
92
93 @Benchmark
94 public void validateValidModel() {
95 validator.validateEffectiveModel(session, validModel, ModelValidator.VALIDATION_LEVEL_STRICT, problemCollector);
96 }
97
98
99
100
101
102
103 @Benchmark
104 public void validateInvalidModel() {
105 validator.validateEffectiveModel(
106 session, invalidModel, ModelValidator.VALIDATION_LEVEL_STRICT, problemCollector);
107 }
108
109
110
111
112
113 @Benchmark
114 public void validateRawModel() {
115 validator.validateRawModel(session, validModel, ModelValidator.VALIDATION_LEVEL_STRICT, problemCollector);
116 }
117
118
119
120
121
122 @Benchmark
123 public void validateMinimalLevel() {
124 validator.validateEffectiveModel(
125 session, validModel, ModelValidator.VALIDATION_LEVEL_MINIMAL, problemCollector);
126 }
127
128
129
130
131
132
133 @Benchmark
134 public void validateDependencyManagement() {
135 Model modelWithManyManagedDeps = createModelWithManyManagedDependencies(dependencyCount);
136 validator.validateEffectiveModel(
137 session, modelWithManyManagedDeps, ModelValidator.VALIDATION_LEVEL_STRICT, problemCollector);
138 }
139
140
141
142
143
144 private Model createValidModel(int dependencyCount) {
145 List<Dependency> dependencies = new ArrayList<>();
146 List<Dependency> managedDependencies = new ArrayList<>();
147 List<Plugin> plugins = new ArrayList<>();
148
149
150 for (int i = 0; i < dependencyCount; i++) {
151 dependencies.add(Dependency.newBuilder()
152 .groupId("org.example.group" + i)
153 .artifactId("artifact" + i)
154 .version("1.0.0")
155 .type("jar")
156 .scope("compile")
157 .build());
158 }
159
160
161 int managedCount = Math.max(1, dependencyCount / 3);
162 for (int i = 0; i < managedCount; i++) {
163 managedDependencies.add(Dependency.newBuilder()
164 .groupId("org.managed.group" + i)
165 .artifactId("managed-artifact" + i)
166 .version("2.0.0")
167 .type("jar")
168 .scope("compile")
169 .build());
170 }
171
172
173 int pluginCount = Math.max(1, dependencyCount / 5);
174 for (int i = 0; i < pluginCount; i++) {
175 plugins.add(Plugin.newBuilder()
176 .groupId("org.apache.maven.plugins")
177 .artifactId("maven-plugin-" + i)
178 .version("3.0.0")
179 .build());
180 }
181
182 return Model.newBuilder()
183 .modelVersion("4.0.0")
184 .groupId("org.apache.maven.benchmark")
185 .artifactId("validation-benchmark")
186 .version("1.0.0")
187 .packaging("jar")
188 .dependencies(dependencies)
189 .dependencyManagement(DependencyManagement.newBuilder()
190 .dependencies(managedDependencies)
191 .build())
192 .build();
193 }
194
195
196
197
198
199
200 private Model createInvalidModel(int dependencyCount) {
201 List<Dependency> dependencies = new ArrayList<>();
202 List<Dependency> managedDependencies = new ArrayList<>();
203
204
205 for (int i = 0; i < dependencyCount; i++) {
206 if (i % 4 == 0) {
207
208 dependencies.add(Dependency.newBuilder()
209 .groupId("org.example.group" + i)
210 .artifactId("artifact" + i)
211 .type("jar")
212 .scope("compile")
213 .build());
214 } else if (i % 4 == 1) {
215
216 dependencies.add(Dependency.newBuilder()
217 .artifactId("artifact" + i)
218 .version("1.0.0")
219 .type("jar")
220 .scope("compile")
221 .build());
222 } else if (i % 4 == 2) {
223
224 dependencies.add(Dependency.newBuilder()
225 .groupId("org.example.group" + i)
226 .version("1.0.0")
227 .type("jar")
228 .scope("compile")
229 .build());
230 } else {
231
232 dependencies.add(Dependency.newBuilder()
233 .groupId("org.example.group" + i)
234 .artifactId("artifact" + i)
235 .version("1.0.0")
236 .type("jar")
237 .scope("compile")
238 .build());
239 }
240 }
241
242
243 int managedCount = Math.max(1, dependencyCount / 3);
244 for (int i = 0; i < managedCount; i++) {
245 if (i % 2 == 0) {
246
247 managedDependencies.add(Dependency.newBuilder()
248 .groupId("org.managed.group" + i)
249 .artifactId("managed-artifact" + i)
250 .type("jar")
251 .build());
252 } else {
253
254 managedDependencies.add(Dependency.newBuilder()
255 .groupId("org.managed.group" + i)
256 .artifactId("managed-artifact" + i)
257 .version("2.0.0")
258 .type("jar")
259 .build());
260 }
261 }
262
263 return Model.newBuilder()
264 .modelVersion("4.0.0")
265 .groupId("org.apache.maven.benchmark")
266 .artifactId("validation-benchmark")
267 .version("1.0.0")
268 .packaging("jar")
269 .dependencies(dependencies)
270 .dependencyManagement(DependencyManagement.newBuilder()
271 .dependencies(managedDependencies)
272 .build())
273 .build();
274 }
275
276
277
278
279
280 private Model createModelWithManyManagedDependencies(int dependencyCount) {
281 List<Dependency> managedDependencies = new ArrayList<>();
282
283
284 for (int i = 0; i < dependencyCount; i++) {
285 String classifier = (i % 3 == 0) ? "sources" : (i % 3 == 1) ? "javadoc" : null;
286 String type = (i % 4 == 0) ? "jar" : (i % 4 == 1) ? "war" : (i % 4 == 2) ? "pom" : "ejb";
287
288 managedDependencies.add(Dependency.newBuilder()
289 .groupId("org.managed.group" + i)
290 .artifactId("managed-artifact" + i)
291 .version("2.0.0")
292 .type(type)
293 .classifier(classifier)
294 .scope("compile")
295 .build());
296 }
297
298 return Model.newBuilder()
299 .modelVersion("4.0.0")
300 .groupId("org.apache.maven.benchmark")
301 .artifactId("dependency-management-benchmark")
302 .version("1.0.0")
303 .packaging("pom")
304 .dependencyManagement(DependencyManagement.newBuilder()
305 .dependencies(managedDependencies)
306 .build())
307 .build();
308 }
309
310
311
312
313 public int getDependencyCount() {
314 return dependencyCount;
315 }
316
317
318
319
320 public static void main(String[] args) throws RunnerException {
321 Options opts = new OptionsBuilder()
322 .include(ModelValidationBenchmark.class.getSimpleName())
323 .forks(1)
324 .build();
325 new Runner(opts).run();
326 }
327 }