View Javadoc
1   package org.apache.maven.shared.artifact.filter;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Arrays;
23  import java.util.concurrent.TimeUnit;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.artifact.DefaultArtifact;
27  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
28  import org.openjdk.jmh.annotations.Benchmark;
29  import org.openjdk.jmh.annotations.BenchmarkMode;
30  import org.openjdk.jmh.annotations.Level;
31  import org.openjdk.jmh.annotations.Mode;
32  import org.openjdk.jmh.annotations.OutputTimeUnit;
33  import org.openjdk.jmh.annotations.Param;
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.Warmup;
38  import org.openjdk.jmh.runner.Runner;
39  import org.openjdk.jmh.runner.RunnerException;
40  import org.openjdk.jmh.runner.options.Options;
41  import org.openjdk.jmh.runner.options.OptionsBuilder;
42  import org.openjdk.jmh.runner.options.TimeValue;
43  
44  @BenchmarkMode(Mode.Throughput)
45  @OutputTimeUnit(TimeUnit.MILLISECONDS)
46  @Warmup(iterations = 3, time = 3 )
47  public class PatternFilterPerfTest {
48  
49      @State(Scope.Benchmark)
50      static public class OldPatternState {
51  
52          @Param({
53                  "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",
54                  "groupId:artifact-99",
55                  "groupId:artifact-*",
56                  "*:artifact-99",
57                  "*:artifact-*",
58                  "*:artifact-*:*",
59                  "*:artifact-99:*",
60          })
61          public String patterns;
62  
63          ArtifactFilter filter;
64          Artifact artifact;
65  
66          @Setup(Level.Invocation)
67          public void setup()
68          {
69              filter = new OldPatternIncludesArtifactFilter( Arrays.asList( patterns.split( "," ) ) );
70              artifact = new DefaultArtifact(
71                      "groupId", "artifact-99", "1.0", "runtime",
72                      "jar", "", null
73              );
74          }
75  
76      }
77  
78      @State(Scope.Benchmark)
79      static public class GNPatternState {
80  
81          @Param({
82                  "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",
83                  "groupId:artifact-99",
84                  "groupId:artifact-*",
85                  "*:artifact-99",
86                  "*:artifact-*",
87                  "*:artifact-*:*",
88                  "*:artifact-99:*",
89          })
90          public String patterns;
91  
92          ArtifactFilter filter;
93          Artifact artifact;
94  
95          @Setup(Level.Invocation)
96          public void setup()
97          {
98              filter = new GNPatternIncludesArtifactFilter( Arrays.asList( patterns.split( "," ) ) );
99              artifact = new DefaultArtifact(
100                     "groupId", "artifact-99", "1.0", "runtime",
101                     "jar", "", null
102             );
103         }
104 
105     }
106 
107     @State(Scope.Benchmark)
108     static public class NewPatternState {
109 
110         @Param({
111                 "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",
112                 "groupId:artifact-99",
113                 "groupId:artifact-*",
114                 "*:artifact-99",
115                 "*:artifact-*",
116                 "*:artifact-*:*",
117                 "*:artifact-99:*",
118         })
119         public String patterns;
120 
121         ArtifactFilter filter;
122         Artifact artifact;
123 
124         @Setup(Level.Invocation)
125         public void setup()
126         {
127             filter = new PatternIncludesArtifactFilter( Arrays.asList( patterns.split( "," ) ) );
128             artifact = new DefaultArtifact(
129                     "groupId", "artifact-99", "1.0", "runtime",
130                     "jar", "", null
131             );
132         }
133 
134     }
135 
136 
137     @Benchmark
138     public boolean newPatternTest(NewPatternState state )
139     {
140         return state.filter.include( state.artifact );
141     }
142 
143     @Benchmark
144     public boolean gnPatternTest(GNPatternState state )
145     {
146         return state.filter.include( state.artifact );
147     }
148 
149     @Benchmark
150     public boolean oldPatternTest(OldPatternState state )
151     {
152         return state.filter.include( state.artifact );
153     }
154 
155     public static void main( String... args )
156             throws RunnerException
157     {
158         Options opts = new OptionsBuilder()
159                 .measurementIterations( 3 )
160                 .measurementTime( TimeValue.milliseconds( 3000 ) )
161                 .forks( 1 )
162                 .include( "org.apache.maven.shared.artifact.filter.PatternFilterPerfTest" )
163                 .build();
164         new Runner( opts ).run();
165     }
166 }