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.shared.artifact.filter;
20  
21  import java.util.Arrays;
22  import java.util.concurrent.TimeUnit;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.artifact.DefaultArtifact;
26  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
27  import org.openjdk.jmh.annotations.Benchmark;
28  import org.openjdk.jmh.annotations.BenchmarkMode;
29  import org.openjdk.jmh.annotations.Level;
30  import org.openjdk.jmh.annotations.Mode;
31  import org.openjdk.jmh.annotations.OutputTimeUnit;
32  import org.openjdk.jmh.annotations.Param;
33  import org.openjdk.jmh.annotations.Scope;
34  import org.openjdk.jmh.annotations.Setup;
35  import org.openjdk.jmh.annotations.State;
36  import org.openjdk.jmh.annotations.Warmup;
37  import org.openjdk.jmh.runner.Runner;
38  import org.openjdk.jmh.runner.RunnerException;
39  import org.openjdk.jmh.runner.options.Options;
40  import org.openjdk.jmh.runner.options.OptionsBuilder;
41  import org.openjdk.jmh.runner.options.TimeValue;
42  
43  @BenchmarkMode(Mode.Throughput)
44  @OutputTimeUnit(TimeUnit.MILLISECONDS)
45  @Warmup(iterations = 3, time = 3)
46  public class PatternFilterPerfTest {
47  
48      @State(Scope.Benchmark)
49      public static class OldPatternState {
50  
51          @Param({
52              "groupId:artifact-00,groupId:artifact-01,groupId:artifact-02,groupId:artifact-03,groupId:artifact-04,groupId:artifact-05,groupId:artifact-06,groupId:artifact-07,groupId:artifact-08,groupId:artifact-09",
53              "groupId:artifact-99",
54              "groupId:artifact-*",
55              "*:artifact-99",
56              "*:artifact-*",
57              "*:artifact-*:*",
58              "*:artifact-99:*",
59          })
60          public String patterns;
61  
62          ArtifactFilter filter;
63          Artifact artifact;
64  
65          @Setup(Level.Invocation)
66          public void setup() {
67              filter = new OldPatternIncludesArtifactFilter(Arrays.asList(patterns.split(",")));
68              artifact = new DefaultArtifact("groupId", "artifact-99", "1.0", "runtime", "jar", "", null);
69          }
70      }
71  
72      @State(Scope.Benchmark)
73      public static class GNPatternState {
74  
75          @Param({
76              "groupId:artifact-00,groupId:artifact-01,groupId:artifact-02,groupId:artifact-03,groupId:artifact-04,groupId:artifact-05,groupId:artifact-06,groupId:artifact-07,groupId:artifact-08,groupId:artifact-09",
77              "groupId:artifact-99",
78              "groupId:artifact-*",
79              "*:artifact-99",
80              "*:artifact-*",
81              "*:artifact-*:*",
82              "*:artifact-99:*",
83          })
84          public String patterns;
85  
86          ArtifactFilter filter;
87          Artifact artifact;
88  
89          @Setup(Level.Invocation)
90          public void setup() {
91              filter = new GNPatternIncludesArtifactFilter(Arrays.asList(patterns.split(",")));
92              artifact = new DefaultArtifact("groupId", "artifact-99", "1.0", "runtime", "jar", "", null);
93          }
94      }
95  
96      @State(Scope.Benchmark)
97      public static class NewPatternState {
98  
99          @Param({
100             "groupId:artifact-00,groupId:artifact-01,groupId:artifact-02,groupId:artifact-03,groupId:artifact-04,groupId:artifact-05,groupId:artifact-06,groupId:artifact-07,groupId:artifact-08,groupId:artifact-09",
101             "groupId:artifact-99",
102             "groupId:artifact-*",
103             "*:artifact-99",
104             "*:artifact-*",
105             "*:artifact-*:*",
106             "*:artifact-99:*",
107         })
108         public String patterns;
109 
110         ArtifactFilter filter;
111         Artifact artifact;
112 
113         @Setup(Level.Invocation)
114         public void setup() {
115             filter = new PatternIncludesArtifactFilter(Arrays.asList(patterns.split(",")));
116             artifact = new DefaultArtifact("groupId", "artifact-99", "1.0", "runtime", "jar", "", null);
117         }
118     }
119 
120     @Benchmark
121     public boolean newPatternTest(NewPatternState state) {
122         return state.filter.include(state.artifact);
123     }
124 
125     @Benchmark
126     public boolean gnPatternTest(GNPatternState state) {
127         return state.filter.include(state.artifact);
128     }
129 
130     @Benchmark
131     public boolean oldPatternTest(OldPatternState state) {
132         return state.filter.include(state.artifact);
133     }
134 
135     public static void main(String... args) throws RunnerException {
136         Options opts = new OptionsBuilder()
137                 .measurementIterations(3)
138                 .measurementTime(TimeValue.milliseconds(3000))
139                 .forks(1)
140                 .include("org.apache.maven.shared.artifact.filter.PatternFilterPerfTest")
141                 .build();
142         new Runner(opts).run();
143     }
144 }