Debian Codegraph @

src/test/java/io/airlift/slice/BenchmarkMurmur3Hash32.java

raw
1 /*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14 package io.airlift.slice;
15
16 import com.google.common.hash.Hashing;
17 import org.openjdk.jmh.annotations.Fork;
18 import org.openjdk.jmh.annotations.Benchmark;
19 import org.openjdk.jmh.annotations.Measurement;
20 import org.openjdk.jmh.annotations.OutputTimeUnit;
21 import org.openjdk.jmh.annotations.Scope;
22 import org.openjdk.jmh.annotations.State;
23 import org.openjdk.jmh.annotations.Warmup;
24 import org.openjdk.jmh.runner.Runner;
25 import org.openjdk.jmh.runner.RunnerException;
26 import org.openjdk.jmh.runner.options.Options;
27 import org.openjdk.jmh.runner.options.OptionsBuilder;
28 import org.openjdk.jmh.runner.options.VerboseMode;
29
30 import java.util.concurrent.TimeUnit;
31
32 @State(Scope.Thread)
33 @OutputTimeUnit(TimeUnit.SECONDS)
34 @Fork(5)
35 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
36 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
37 public class BenchmarkMurmur3Hash32
38 {
39 @Benchmark
40 public int hash(BenchmarkData data, ByteCounter counter)
41 {
42 counter.add(data.getSlice().length());
43 return Murmur3Hash32.hash(data.getSlice());
44 }
45
46 @Benchmark
47 public int guava(BenchmarkData data, ByteCounter counter)
48 {
49 counter.add(data.getSlice().length());
50 return Hashing.murmur3_32().hashBytes(data.getBytes()).asInt();
51 }
52
53 @Benchmark
54 public int specializedHashInt(SingleLong data, ByteCounter counter)
55 {
56 counter.add(SizeOf.SIZE_OF_INT);
57 return Murmur3Hash32.hash((int) data.getValue());
58 }
59
60 @Benchmark
61 public int hashInt(BenchmarkData data, ByteCounter counter)
62 {
63 counter.add(SizeOf.SIZE_OF_INT);
64 return Murmur3Hash32.hash(data.getSlice(), 0, 4);
65 }
66
67 @Benchmark
68 public int specializedHashLong(SingleLong data, ByteCounter counter)
69 {
70 counter.add(SizeOf.SIZE_OF_LONG);
71 return Murmur3Hash32.hash(data.getValue());
72 }
73
74 @Benchmark
75 public int hashLong(BenchmarkData data, ByteCounter counter)
76 {
77 counter.add(SizeOf.SIZE_OF_LONG);
78 return Murmur3Hash32.hash(data.getSlice(), 0, 8);
79 }
80
81 public static void main(String[] args)
82 throws RunnerException
83 {
84 Options options = new OptionsBuilder()
85 .verbosity(VerboseMode.NORMAL)
86 .include(".*" + BenchmarkMurmur3Hash32.class.getSimpleName() + ".*")
87 .build();
88
89 new Runner(options).run();
90 }
91 }
92