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.buildcache.hash;
20  
21  import java.io.IOException;
22  import java.nio.file.Files;
23  import java.nio.file.Path;
24  import java.nio.file.Paths;
25  import java.util.List;
26  import java.util.concurrent.TimeUnit;
27  import java.util.stream.Collectors;
28  import java.util.stream.Stream;
29  
30  import org.openjdk.jmh.annotations.Benchmark;
31  import org.openjdk.jmh.annotations.BenchmarkMode;
32  import org.openjdk.jmh.annotations.Level;
33  import org.openjdk.jmh.annotations.Mode;
34  import org.openjdk.jmh.annotations.OutputTimeUnit;
35  import org.openjdk.jmh.annotations.Scope;
36  import org.openjdk.jmh.annotations.Setup;
37  import org.openjdk.jmh.annotations.State;
38  import org.openjdk.jmh.annotations.Warmup;
39  import org.openjdk.jmh.runner.Runner;
40  import org.openjdk.jmh.runner.RunnerException;
41  import org.openjdk.jmh.runner.options.Options;
42  import org.openjdk.jmh.runner.options.OptionsBuilder;
43  import org.openjdk.jmh.runner.options.TimeValue;
44  
45  @BenchmarkMode(Mode.Throughput)
46  @OutputTimeUnit(TimeUnit.MILLISECONDS)
47  @Warmup(iterations = 3, time = 3, timeUnit = TimeUnit.SECONDS)
48  public class PerfTest {
49  
50      @State(Scope.Benchmark)
51      public static class HashState {
52          List<Path> paths;
53  
54          @Setup(Level.Iteration)
55          public void setUp() throws IOException {
56              try (Stream<Path> stream = Files.walk(Paths.get(System.getProperty("user.dir")))) {
57                  paths = stream.filter(p -> p.getFileName().toString().endsWith(".java"))
58                          .collect(Collectors.toList());
59              }
60          }
61      }
62  
63      String doTest(HashFactory hashFactory, HashState state) throws IOException {
64          HashAlgorithm hash = hashFactory.createAlgorithm();
65          StringBuilder sb = new StringBuilder();
66          for (Path path : state.paths) {
67              if (sb.length() > 0) {
68                  sb.append("\n");
69              }
70              sb.append(hash.hash(path));
71          }
72          return sb.toString();
73      }
74  
75      @Benchmark
76      public String SHA1(HashState state) throws IOException {
77          return doTest(HashFactory.SHA1, state);
78      }
79  
80      @Benchmark
81      public String SHA256(HashState state) throws IOException {
82          return doTest(HashFactory.SHA256, state);
83      }
84  
85      @Benchmark
86      public String XX(HashState state) throws IOException {
87          return doTest(HashFactory.XX, state);
88      }
89  
90      @Benchmark
91      public String XXMM(HashState state) throws IOException {
92          return doTest(HashFactory.XXMM, state);
93      }
94  
95      @Benchmark
96      public String METRO(HashState state) throws IOException {
97          return doTest(HashFactory.METRO, state);
98      }
99  
100     @Benchmark
101     public String METRO_MM(HashState state) throws IOException {
102         return doTest(HashFactory.METRO_MM, state);
103     }
104 
105     /*
106      * <p>main.</p>
107      *
108      * @param args a {@link java.lang.String} object.
109      * @throws org.openjdk.jmh.runner.RunnerException if any.
110      */
111     public static void main(String... args) throws RunnerException {
112         Options opts = new OptionsBuilder()
113                 .measurementIterations(3)
114                 .measurementTime(TimeValue.milliseconds(3000))
115                 .forks(1)
116                 .build();
117         new Runner(opts).run();
118     }
119 }