|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.common; |
| 10 | + |
| 11 | +import org.openjdk.jmh.annotations.Benchmark; |
| 12 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 13 | +import org.openjdk.jmh.annotations.Fork; |
| 14 | +import org.openjdk.jmh.annotations.Measurement; |
| 15 | +import org.openjdk.jmh.annotations.Mode; |
| 16 | +import org.openjdk.jmh.annotations.Param; |
| 17 | +import org.openjdk.jmh.annotations.Scope; |
| 18 | +import org.openjdk.jmh.annotations.Setup; |
| 19 | +import org.openjdk.jmh.annotations.State; |
| 20 | +import org.openjdk.jmh.annotations.Warmup; |
| 21 | +import org.openjdk.jmh.infra.Blackhole; |
| 22 | + |
| 23 | +import java.util.Random; |
| 24 | +import java.util.function.Supplier; |
| 25 | + |
| 26 | +@Fork(value = 3) |
| 27 | +@Warmup(iterations = 3, time = 1) |
| 28 | +@Measurement(iterations = 1, time = 1) |
| 29 | +@BenchmarkMode(Mode.Throughput) |
| 30 | +public class ArrayRoundingBenchmark { |
| 31 | + |
| 32 | + @Benchmark |
| 33 | + public void round(Blackhole bh, Options opts) { |
| 34 | + Rounding.Prepared rounding = opts.supplier.get(); |
| 35 | + for (long key : opts.queries) { |
| 36 | + bh.consume(rounding.round(key)); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + @State(Scope.Benchmark) |
| 41 | + public static class Options { |
| 42 | + @Param({ |
| 43 | + "1", |
| 44 | + "2", |
| 45 | + "3", |
| 46 | + "4", |
| 47 | + "5", |
| 48 | + "6", |
| 49 | + "7", |
| 50 | + "8", |
| 51 | + "9", |
| 52 | + "10", |
| 53 | + "12", |
| 54 | + "14", |
| 55 | + "16", |
| 56 | + "18", |
| 57 | + "20", |
| 58 | + "22", |
| 59 | + "24", |
| 60 | + "26", |
| 61 | + "29", |
| 62 | + "32", |
| 63 | + "37", |
| 64 | + "41", |
| 65 | + "45", |
| 66 | + "49", |
| 67 | + "54", |
| 68 | + "60", |
| 69 | + "64", |
| 70 | + "74", |
| 71 | + "83", |
| 72 | + "90", |
| 73 | + "98", |
| 74 | + "108", |
| 75 | + "118", |
| 76 | + "128", |
| 77 | + "144", |
| 78 | + "159", |
| 79 | + "171", |
| 80 | + "187", |
| 81 | + "204", |
| 82 | + "229", |
| 83 | + "256" }) |
| 84 | + public Integer size; |
| 85 | + |
| 86 | + @Param({ "binary", "linear" }) |
| 87 | + public String type; |
| 88 | + |
| 89 | + @Param({ "uniform", "skewed_edge", "skewed_center" }) |
| 90 | + public String distribution; |
| 91 | + |
| 92 | + public long[] queries; |
| 93 | + public Supplier<Rounding.Prepared> supplier; |
| 94 | + |
| 95 | + @Setup |
| 96 | + public void setup() { |
| 97 | + Random random = new Random(size); |
| 98 | + long[] values = new long[size]; |
| 99 | + for (int i = 1; i < values.length; i++) { |
| 100 | + values[i] = values[i - 1] + 100; |
| 101 | + } |
| 102 | + |
| 103 | + long range = values[values.length - 1] - values[0] + 100; |
| 104 | + long mean, stddev; |
| 105 | + queries = new long[1000000]; |
| 106 | + |
| 107 | + switch (distribution) { |
| 108 | + case "uniform": // all values equally likely. |
| 109 | + for (int i = 0; i < queries.length; i++) { |
| 110 | + queries[i] = values[0] + (nextPositiveLong(random) % range); |
| 111 | + } |
| 112 | + break; |
| 113 | + case "skewed_edge": // distribution centered at p90 with ± 5% stddev. |
| 114 | + mean = values[0] + (long) (range * 0.9); |
| 115 | + stddev = (long) (range * 0.05); |
| 116 | + for (int i = 0; i < queries.length; i++) { |
| 117 | + queries[i] = Math.max(values[0], mean + (long) (random.nextGaussian() * stddev)); |
| 118 | + } |
| 119 | + break; |
| 120 | + case "skewed_center": // distribution centered at p50 with ± 5% stddev. |
| 121 | + mean = values[0] + (long) (range * 0.5); |
| 122 | + stddev = (long) (range * 0.05); |
| 123 | + for (int i = 0; i < queries.length; i++) { |
| 124 | + queries[i] = Math.max(values[0], mean + (long) (random.nextGaussian() * stddev)); |
| 125 | + } |
| 126 | + break; |
| 127 | + default: |
| 128 | + throw new IllegalArgumentException("invalid distribution: " + distribution); |
| 129 | + } |
| 130 | + |
| 131 | + switch (type) { |
| 132 | + case "binary": |
| 133 | + supplier = () -> new Rounding.BinarySearchArrayRounding(values, size, null); |
| 134 | + break; |
| 135 | + case "linear": |
| 136 | + supplier = () -> new Rounding.BidirectionalLinearSearchArrayRounding(values, size, null); |
| 137 | + break; |
| 138 | + default: |
| 139 | + throw new IllegalArgumentException("invalid type: " + type); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private static long nextPositiveLong(Random random) { |
| 144 | + return random.nextLong() & Long.MAX_VALUE; |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments