|
| 1 | +package io.prometheus.metrics.benchmarks; |
| 2 | + |
| 3 | +import io.prometheus.metrics.expositionformats.ExpositionFormatWriter; |
| 4 | +import io.prometheus.metrics.expositionformats.OpenMetricsTextFormatWriter; |
| 5 | +import io.prometheus.metrics.expositionformats.PrometheusTextFormatWriter; |
| 6 | +import io.prometheus.metrics.model.snapshots.GaugeSnapshot; |
| 7 | +import io.prometheus.metrics.model.snapshots.GaugeSnapshot.GaugeDataPointSnapshot; |
| 8 | +import io.prometheus.metrics.model.snapshots.Labels; |
| 9 | +import io.prometheus.metrics.model.snapshots.MetricSnapshot; |
| 10 | +import io.prometheus.metrics.model.snapshots.MetricSnapshots; |
| 11 | +import io.prometheus.metrics.model.snapshots.SummarySnapshot; |
| 12 | +import io.prometheus.metrics.model.snapshots.SummarySnapshot.SummaryDataPointSnapshot; |
| 13 | +import io.prometheus.metrics.model.snapshots.Unit; |
| 14 | +import java.io.ByteArrayOutputStream; |
| 15 | +import java.io.IOException; |
| 16 | +import java.io.OutputStream; |
| 17 | +import org.openjdk.jmh.annotations.Benchmark; |
| 18 | +import org.openjdk.jmh.annotations.Scope; |
| 19 | +import org.openjdk.jmh.annotations.State; |
| 20 | + |
| 21 | +public class TextFormatUtilBenchmark { |
| 22 | + |
| 23 | + private static final MetricSnapshots SNAPSHOTS; |
| 24 | + |
| 25 | + static { |
| 26 | + MetricSnapshot gaugeSnapshot = |
| 27 | + GaugeSnapshot.builder() |
| 28 | + .name("gauge_snapshot_name") |
| 29 | + .dataPoint( |
| 30 | + GaugeDataPointSnapshot.builder() |
| 31 | + .labels(Labels.of("name", "value")) |
| 32 | + .scrapeTimestampMillis(1000L) |
| 33 | + .value(123.45d) |
| 34 | + .build()) |
| 35 | + .build(); |
| 36 | + |
| 37 | + MetricSnapshot summaryDataPointSnapshot = |
| 38 | + SummarySnapshot.builder() |
| 39 | + .name("summary_snapshot_name_bytes") |
| 40 | + .dataPoint( |
| 41 | + SummaryDataPointSnapshot.builder() |
| 42 | + .count(5) |
| 43 | + .labels(Labels.of("name", "value")) |
| 44 | + .sum(123456d) |
| 45 | + .build()) |
| 46 | + .unit(Unit.BYTES) |
| 47 | + .build(); |
| 48 | + |
| 49 | + SNAPSHOTS = MetricSnapshots.of(gaugeSnapshot, summaryDataPointSnapshot); |
| 50 | + } |
| 51 | + |
| 52 | + private static final ExpositionFormatWriter OPEN_METRICS_TEXT_FORMAT_WRITER = |
| 53 | + new OpenMetricsTextFormatWriter(false, false); |
| 54 | + private static final ExpositionFormatWriter PROMETHEUS_TEXT_FORMAT_WRITER = |
| 55 | + new PrometheusTextFormatWriter(false); |
| 56 | + |
| 57 | + @State(Scope.Benchmark) |
| 58 | + public static class WriterState { |
| 59 | + |
| 60 | + final ByteArrayOutputStream byteArrayOutputStream; |
| 61 | + |
| 62 | + public WriterState() { |
| 63 | + this.byteArrayOutputStream = new ByteArrayOutputStream(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Benchmark |
| 68 | + public OutputStream openMetricsWriteToByteArray(WriterState writerState) throws IOException { |
| 69 | + // avoid growing the array |
| 70 | + ByteArrayOutputStream byteArrayOutputStream = writerState.byteArrayOutputStream; |
| 71 | + byteArrayOutputStream.reset(); |
| 72 | + OPEN_METRICS_TEXT_FORMAT_WRITER.write(byteArrayOutputStream, SNAPSHOTS); |
| 73 | + return byteArrayOutputStream; |
| 74 | + } |
| 75 | + |
| 76 | + @Benchmark |
| 77 | + public OutputStream openMetricsWriteToNull() throws IOException { |
| 78 | + OutputStream nullOutputStream = NullOutputStream.INSTANCE; |
| 79 | + OPEN_METRICS_TEXT_FORMAT_WRITER.write(nullOutputStream, SNAPSHOTS); |
| 80 | + return nullOutputStream; |
| 81 | + } |
| 82 | + |
| 83 | + @Benchmark |
| 84 | + public OutputStream prometheusWriteToByteArray(WriterState writerState) throws IOException { |
| 85 | + // avoid growing the array |
| 86 | + ByteArrayOutputStream byteArrayOutputStream = writerState.byteArrayOutputStream; |
| 87 | + byteArrayOutputStream.reset(); |
| 88 | + PROMETHEUS_TEXT_FORMAT_WRITER.write(byteArrayOutputStream, SNAPSHOTS); |
| 89 | + return byteArrayOutputStream; |
| 90 | + } |
| 91 | + |
| 92 | + @Benchmark |
| 93 | + public OutputStream prometheusWriteToNull() throws IOException { |
| 94 | + OutputStream nullOutputStream = NullOutputStream.INSTANCE; |
| 95 | + PROMETHEUS_TEXT_FORMAT_WRITER.write(nullOutputStream, SNAPSHOTS); |
| 96 | + return nullOutputStream; |
| 97 | + } |
| 98 | + |
| 99 | + static final class NullOutputStream extends OutputStream { |
| 100 | + |
| 101 | + static final OutputStream INSTANCE = new NullOutputStream(); |
| 102 | + |
| 103 | + private NullOutputStream() { |
| 104 | + super(); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void write(int b) {} |
| 109 | + |
| 110 | + @Override |
| 111 | + public void write(byte[] b) {} |
| 112 | + |
| 113 | + @Override |
| 114 | + public void write(byte[] b, int off, int len) {} |
| 115 | + |
| 116 | + @Override |
| 117 | + public void flush() {} |
| 118 | + |
| 119 | + @Override |
| 120 | + public void close() {} |
| 121 | + } |
| 122 | +} |
0 commit comments