|
| 1 | +package io.prometheus.client; |
| 2 | + |
| 3 | +import io.prometheus.client.CKMSQuantiles.Quantile; |
| 4 | +import org.openjdk.jmh.annotations.*; |
| 5 | +import org.openjdk.jmh.infra.Blackhole; |
| 6 | +import org.openjdk.jmh.runner.Runner; |
| 7 | +import org.openjdk.jmh.runner.RunnerException; |
| 8 | +import org.openjdk.jmh.runner.options.Options; |
| 9 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 10 | + |
| 11 | +import java.util.*; |
| 12 | +import java.util.concurrent.TimeUnit; |
| 13 | + |
| 14 | +public class CKMSQuantileBenchmark { |
| 15 | + |
| 16 | + @State(Scope.Benchmark) |
| 17 | + public static class EmptyBenchmarkState { |
| 18 | + @Param({"10000", "100000", "1000000"}) |
| 19 | + public int value; |
| 20 | + |
| 21 | + CKMSQuantiles ckmsQuantiles; |
| 22 | + |
| 23 | + List<Quantile> quantiles; |
| 24 | + Random rand = new Random(0); |
| 25 | + |
| 26 | + long[] shuffle; |
| 27 | + |
| 28 | + @Setup(Level.Trial) |
| 29 | + public void setup() { |
| 30 | + quantiles = new ArrayList<Quantile>(); |
| 31 | + quantiles.add(new Quantile(0.50, 0.050)); |
| 32 | + quantiles.add(new Quantile(0.90, 0.010)); |
| 33 | + quantiles.add(new Quantile(0.95, 0.005)); |
| 34 | + quantiles.add(new Quantile(0.99, 0.001)); |
| 35 | + |
| 36 | + |
| 37 | + shuffle = new long[value]; |
| 38 | + for (int i = 0; i < shuffle.length; i++) { |
| 39 | + shuffle[i] = i; |
| 40 | + } |
| 41 | + Collections.shuffle(Arrays.asList(shuffle), rand); |
| 42 | + |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Benchmark |
| 47 | + @BenchmarkMode({Mode.AverageTime}) |
| 48 | + @OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 49 | + public void ckmsQuantileInsertBenchmark(Blackhole blackhole, EmptyBenchmarkState state) { |
| 50 | + CKMSQuantiles q = new CKMSQuantiles(state.quantiles.toArray(new Quantile[]{})); |
| 51 | + for (long l : state.shuffle) { |
| 52 | + q.insert(l); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @State(Scope.Benchmark) |
| 57 | + public static class PrefilledBenchmarkState { |
| 58 | + public int value = 1000000; |
| 59 | + |
| 60 | + CKMSQuantiles ckmsQuantiles; |
| 61 | + |
| 62 | + List<Quantile> quantiles; |
| 63 | + Random rand = new Random(0); |
| 64 | + |
| 65 | + long[] shuffle; |
| 66 | + |
| 67 | + @Setup(Level.Trial) |
| 68 | + public void setup() { |
| 69 | + quantiles = new ArrayList<Quantile>(); |
| 70 | + quantiles.add(new Quantile(0.50, 0.050)); |
| 71 | + quantiles.add(new Quantile(0.90, 0.010)); |
| 72 | + quantiles.add(new Quantile(0.95, 0.005)); |
| 73 | + quantiles.add(new Quantile(0.99, 0.001)); |
| 74 | + |
| 75 | + |
| 76 | + shuffle = new long[value]; |
| 77 | + for (int i = 0; i < shuffle.length; i++) { |
| 78 | + shuffle[i] = i; |
| 79 | + } |
| 80 | + Collections.shuffle(Arrays.asList(shuffle), rand); |
| 81 | + ckmsQuantiles = new CKMSQuantiles(quantiles.toArray(new Quantile[]{})); |
| 82 | + for (long l : shuffle) { |
| 83 | + ckmsQuantiles.insert(l); |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Benchmark |
| 90 | + @BenchmarkMode({Mode.AverageTime}) |
| 91 | + @OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 92 | + public void ckmsQuantileGetBenchmark(Blackhole blackhole, PrefilledBenchmarkState state) { |
| 93 | + blackhole.consume(state.ckmsQuantiles.get(0.95)); |
| 94 | + } |
| 95 | + |
| 96 | + public static void main(String[] args) throws RunnerException { |
| 97 | + |
| 98 | + Options opt = new OptionsBuilder() |
| 99 | + .include(CKMSQuantileBenchmark.class.getSimpleName()) |
| 100 | + .warmupIterations(5) |
| 101 | + .measurementIterations(4) |
| 102 | + .threads(1) |
| 103 | + .forks(1) |
| 104 | + .build(); |
| 105 | + |
| 106 | + new Runner(opt).run(); |
| 107 | + } |
| 108 | +} |
0 commit comments