001package io.prometheus.benchmark;
002
003import com.codahale.metrics.MetricRegistry;
004
005import java.util.concurrent.TimeUnit;
006import org.openjdk.jmh.annotations.BenchmarkMode;
007import org.openjdk.jmh.annotations.Mode;
008import org.openjdk.jmh.annotations.Benchmark;
009import org.openjdk.jmh.annotations.OutputTimeUnit;
010import org.openjdk.jmh.annotations.Scope;
011import org.openjdk.jmh.annotations.Setup;
012import org.openjdk.jmh.annotations.State;
013import org.openjdk.jmh.runner.Runner;
014import org.openjdk.jmh.runner.RunnerException;
015import org.openjdk.jmh.runner.options.Options;
016import org.openjdk.jmh.runner.options.OptionsBuilder;
017
018@State(Scope.Benchmark)
019public class SummaryBenchmark {
020
021  MetricRegistry registry;
022  com.codahale.metrics.Histogram codahaleHistogram;
023
024  io.prometheus.client.metrics.Summary prometheusSummary;
025  io.prometheus.client.metrics.Summary.Child prometheusSummaryChild;
026  io.prometheus.client.Summary prometheusSimpleSummary;
027  io.prometheus.client.Summary.Child prometheusSimpleSummaryChild;
028  io.prometheus.client.Summary prometheusSimpleSummaryNoLabels;
029  io.prometheus.client.Histogram prometheusSimpleHistogram;
030  io.prometheus.client.Histogram.Child prometheusSimpleHistogramChild;
031  io.prometheus.client.Histogram prometheusSimpleHistogramNoLabels;
032
033  @Setup
034  public void setup() {
035    prometheusSummary = io.prometheus.client.metrics.Summary.newBuilder()
036      .name("name")
037      .documentation("some description..")
038      .build();
039    prometheusSummaryChild = prometheusSummary.newPartial().apply();
040
041    prometheusSimpleSummary = io.prometheus.client.Summary.build()
042      .name("name")
043      .help("some description..")
044      .labelNames("some", "group").create();
045    prometheusSimpleSummaryChild = prometheusSimpleSummary.labels("test", "group");
046
047    prometheusSimpleSummaryNoLabels = io.prometheus.client.Summary.build()
048      .name("name")
049      .help("some description..")
050      .create();
051
052    prometheusSimpleHistogram = io.prometheus.client.Histogram.build()
053      .name("name")
054      .help("some description..")
055      .labelNames("some", "group").create();
056    prometheusSimpleHistogramChild = prometheusSimpleHistogram.labels("test", "group");
057
058    prometheusSimpleHistogramNoLabels = io.prometheus.client.Histogram.build()
059      .name("name")
060      .help("some description..")
061      .create();
062
063    registry = new MetricRegistry();
064    codahaleHistogram = registry.histogram("name");
065  }
066
067  @Benchmark
068  @BenchmarkMode({Mode.AverageTime})
069  @OutputTimeUnit(TimeUnit.NANOSECONDS)
070  public void prometheusSummaryBenchmark() {
071    prometheusSummary.newPartial().apply().observe(1.0);
072  }
073
074  @Benchmark
075  @BenchmarkMode({Mode.AverageTime})
076  @OutputTimeUnit(TimeUnit.NANOSECONDS)
077  public void prometheusSummaryChildBenchmark() {
078    prometheusSummaryChild.observe(1.0);
079  }
080
081  @Benchmark
082  @BenchmarkMode({Mode.AverageTime})
083  @OutputTimeUnit(TimeUnit.NANOSECONDS)
084  public void prometheusSimpleSummaryBenchmark() {
085    prometheusSimpleSummary.labels("test", "group").observe(1) ;
086  }
087
088  @Benchmark
089  @BenchmarkMode({Mode.AverageTime})
090  @OutputTimeUnit(TimeUnit.NANOSECONDS)
091  public void prometheusSimpleSummaryChildBenchmark() {
092    prometheusSimpleSummaryChild.observe(1); 
093  }
094
095  @Benchmark
096  @BenchmarkMode({Mode.AverageTime})
097  @OutputTimeUnit(TimeUnit.NANOSECONDS)
098  public void prometheusSimpleSummaryNoLabelsBenchmark() {
099    prometheusSimpleSummaryNoLabels.observe(1); 
100  }
101
102  @Benchmark
103  @BenchmarkMode({Mode.AverageTime})
104  @OutputTimeUnit(TimeUnit.NANOSECONDS)
105  public void prometheusSimpleHistogramBenchmark() {
106    prometheusSimpleHistogram.labels("test", "group").observe(1) ;
107  }
108
109  @Benchmark
110  @BenchmarkMode({Mode.AverageTime})
111  @OutputTimeUnit(TimeUnit.NANOSECONDS)
112  public void prometheusSimpleHistogramChildBenchmark() {
113    prometheusSimpleHistogramChild.observe(1);
114  }
115
116  @Benchmark
117  @BenchmarkMode({Mode.AverageTime})
118  @OutputTimeUnit(TimeUnit.NANOSECONDS)
119  public void prometheusSimpleHistogramNoLabelsBenchmark() {
120    prometheusSimpleHistogramNoLabels.observe(1);
121  }
122
123  @Benchmark
124  @BenchmarkMode({Mode.AverageTime})
125  @OutputTimeUnit(TimeUnit.NANOSECONDS)
126  public void codahaleHistogramBenchmark() {
127    codahaleHistogram.update(1);
128  }
129
130  public static void main(String[] args) throws RunnerException {
131
132    Options opt = new OptionsBuilder()
133      .include(SummaryBenchmark.class.getSimpleName())
134      .warmupIterations(5)
135      .measurementIterations(4)
136      .threads(4)
137      .forks(1)
138      .build();
139
140    new Runner(opt).run();
141  }
142}