1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
107
108
109
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 }