| 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 BenchmarkMurmur3Hash128 |
| 38 |
{ |
| 39 |
@Benchmark |
| 40 |
public long hash64(BenchmarkData data, ByteCounter counter) |
| 41 |
{ |
| 42 |
counter.add(data.getSlice().length()); |
| 43 |
return Murmur3Hash128.hash64(data.getSlice()); |
| 44 |
} |
| 45 |
|
| 46 |
@Benchmark |
| 47 |
public Slice hash(BenchmarkData data, ByteCounter counter) |
| 48 |
{ |
| 49 |
counter.add(data.getSlice().length()); |
| 50 |
return Murmur3Hash128.hash(data.getSlice()); |
| 51 |
} |
| 52 |
|
| 53 |
@Benchmark |
| 54 |
public long guava(BenchmarkData data, ByteCounter counter) |
| 55 |
{ |
| 56 |
counter.add(data.getSlice().length()); |
| 57 |
return Hashing.murmur3_128().hashBytes(data.getBytes()).asLong(); |
| 58 |
} |
| 59 |
|
| 60 |
@Benchmark |
| 61 |
public long specializedHashLong(SingleLong data, ByteCounter counter) |
| 62 |
{ |
| 63 |
counter.add(SizeOf.SIZE_OF_LONG); |
| 64 |
return Murmur3Hash128.hash64(data.getValue()); |
| 65 |
} |
| 66 |
|
| 67 |
@Benchmark |
| 68 |
public long hashLong(BenchmarkData data, ByteCounter counter) |
| 69 |
{ |
| 70 |
counter.add(SizeOf.SIZE_OF_LONG); |
| 71 |
return Murmur3Hash128.hash64(data.getSlice(), 0, 8); |
| 72 |
} |
| 73 |
|
| 74 |
public static void main(String[] args) |
| 75 |
throws RunnerException |
| 76 |
{ |
| 77 |
Options options = new OptionsBuilder() |
| 78 |
.verbosity(VerboseMode.NORMAL) |
| 79 |
.include(".*" + BenchmarkMurmur3Hash128.class.getSimpleName() + ".*") |
| 80 |
.build(); |
| 81 |
|
| 82 |
new Runner(options).run(); |
| 83 |
} |
| 84 |
} |
| 85 |
|