|
| 1 | +// Copyright (c) 2022 VMware, Inc. or its affiliates. All rights reserved. |
| 2 | +// |
| 3 | +// This software, the RabbitMQ Stream Java client library, is dual-licensed under the |
| 4 | +// Mozilla Public License 2.0 ("MPL"), and the Apache License version 2 ("ASL"). |
| 5 | +// For the MPL, please see LICENSE-MPL-RabbitMQ. For the ASL, |
| 6 | +// please see LICENSE-APACHE2. |
| 7 | +// |
| 8 | +// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, |
| 9 | +// either express or implied. See the LICENSE file for specific language governing |
| 10 | +// rights and limitations of this software. |
| 11 | +// |
| 12 | +// If you have any questions regarding licensing, please contact us at |
| 13 | + |
| 14 | +package com.rabbitmq.stream.benchmark; |
| 15 | + |
| 16 | +import java.nio.charset.StandardCharsets; |
| 17 | +import java.util.concurrent.ConcurrentHashMap; |
| 18 | +import java.util.concurrent.TimeUnit; |
| 19 | +import java.util.stream.IntStream; |
| 20 | +import org.apache.commons.codec.digest.MurmurHash3; |
| 21 | +import org.openjdk.jmh.annotations.Benchmark; |
| 22 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 23 | +import org.openjdk.jmh.annotations.Fork; |
| 24 | +import org.openjdk.jmh.annotations.Measurement; |
| 25 | +import org.openjdk.jmh.annotations.Mode; |
| 26 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 27 | +import org.openjdk.jmh.annotations.Param; |
| 28 | +import org.openjdk.jmh.annotations.Scope; |
| 29 | +import org.openjdk.jmh.annotations.Setup; |
| 30 | +import org.openjdk.jmh.annotations.State; |
| 31 | +import org.openjdk.jmh.annotations.Threads; |
| 32 | +import org.openjdk.jmh.annotations.Warmup; |
| 33 | +import org.openjdk.jmh.runner.Runner; |
| 34 | +import org.openjdk.jmh.runner.RunnerException; |
| 35 | +import org.openjdk.jmh.runner.options.Options; |
| 36 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 37 | + |
| 38 | +@BenchmarkMode(Mode.Throughput) |
| 39 | +@OutputTimeUnit(TimeUnit.SECONDS) |
| 40 | +@State(Scope.Benchmark) |
| 41 | +@Warmup(iterations = 5, time = 5) |
| 42 | +@Measurement(iterations = 3, time = 5) |
| 43 | +@Fork(1) |
| 44 | +@Threads(1) |
| 45 | +public class ModuloBenchmark { |
| 46 | + |
| 47 | + static final int partitionSize = 3; |
| 48 | + |
| 49 | + static ConcurrentHashMap<String, IntToIntFunction> MODULOS = |
| 50 | + new ConcurrentHashMap<String, IntToIntFunction>() { |
| 51 | + { |
| 52 | + put("mod", i -> i % partitionSize); |
| 53 | + put("remainderUnsigned", i -> Integer.remainderUnsigned(i, partitionSize)); |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + @Param({"mod", "remainderUnsigned"}) |
| 58 | + String algorithm; |
| 59 | + |
| 60 | + IntToIntFunction modulo; |
| 61 | + |
| 62 | + int[] content; |
| 63 | + |
| 64 | + public static void main(String[] args) throws RunnerException { |
| 65 | + Options opt = |
| 66 | + new OptionsBuilder() |
| 67 | + .include(ModuloBenchmark.class.getSimpleName()) |
| 68 | + .warmupIterations(3) |
| 69 | + .measurementIterations(2) |
| 70 | + .forks(1) |
| 71 | + .build(); |
| 72 | + |
| 73 | + new Runner(opt).run(); |
| 74 | + } |
| 75 | + |
| 76 | + @Setup |
| 77 | + public void setUp() { |
| 78 | + modulo = MODULOS.get(algorithm); |
| 79 | + content = |
| 80 | + IntStream.range(0, 10) |
| 81 | + .mapToObj(i -> "hello-" + i) |
| 82 | + .mapToInt( |
| 83 | + s -> |
| 84 | + MurmurHash3.hash32x86( |
| 85 | + s.getBytes(StandardCharsets.UTF_8), 0, s.length(), 104729)) |
| 86 | + .toArray(); |
| 87 | + } |
| 88 | + |
| 89 | + @Benchmark |
| 90 | + public void modulo() { |
| 91 | + for (int i : content) { |
| 92 | + modulo.applyAsLong(i); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @FunctionalInterface |
| 97 | + public interface IntToIntFunction { |
| 98 | + |
| 99 | + int applyAsLong(int value); |
| 100 | + } |
| 101 | +} |
0 commit comments