Skip to content

Commit 64bc726

Browse files
committed
remove shared writer for now
Signed-off-by: Jay DeLuca <jaydeluca4@gmail.com>
1 parent b1f89ba commit 64bc726

4 files changed

Lines changed: 2 additions & 139 deletions

File tree

benchmarks/src/main/java/io/prometheus/metrics/benchmarks/HistogramTextFormatBenchmark.java

Lines changed: 1 addition & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,17 @@
88
import io.prometheus.metrics.model.snapshots.HistogramSnapshot.HistogramDataPointSnapshot;
99
import io.prometheus.metrics.model.snapshots.Labels;
1010
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
11-
import java.io.BufferedWriter;
12-
import java.io.ByteArrayOutputStream;
1311
import java.io.IOException;
1412
import java.io.OutputStream;
15-
import java.io.OutputStreamWriter;
16-
import java.io.Writer;
17-
import java.nio.charset.StandardCharsets;
1813
import java.util.concurrent.TimeUnit;
1914
import org.openjdk.jmh.annotations.Benchmark;
2015
import org.openjdk.jmh.annotations.Fork;
2116
import org.openjdk.jmh.annotations.Measurement;
22-
import org.openjdk.jmh.annotations.Scope;
23-
import org.openjdk.jmh.annotations.State;
2417
import org.openjdk.jmh.annotations.Warmup;
2518

2619
/**
2720
* Benchmarks for writing a classic histogram (10 label combinations × 12 buckets) to text formats.
28-
*
29-
* <p>Three variants per format:
30-
*
31-
* <ul>
32-
* <li>{@code writeToByteArray} — OutputStream path, new BufferedWriter created per call (~41
33-
* KB/op; IO stack allocation dominates).
34-
* <li>{@code writeToNull} — OutputStream path to /dev/null, new BufferedWriter still created per
35-
* call (same allocation as writeToByteArray; shows ByteArrayOutputStream write cost is
36-
* negligible).
37-
* <li>{@code reusingWriter} — Writer path, BufferedWriter reused across calls (~18 KB/op; saves
38-
* the ~25 KB BufferedWriter char[] buffer and OutputStreamWriter per call).
39-
* <li>{@code reusingWriterToNull} — Writer path, BufferedWriter reused, output to /dev/null.
40-
* Lowest-allocation floor; isolates pure formatting CPU cost with zero IO stack overhead.
41-
* </ul>
42-
*
43-
* <p>Key allocation sources:
44-
*
45-
* <ul>
46-
* <li>BufferedWriter internal char[8192] buffer: 16,384 bytes per call (eliminated by reuse).
47-
* <li>OutputStreamWriter + StreamEncoder state: ~1–2 KB per call (eliminated by reuse).
48-
* <li>Remaining ~18 KB/op in reuse variants: ByteArrayOutputStream byte[] growth + any per-call
49-
* String allocations remaining in the format path.
50-
* </ul>
21+
* Output goes to /dev/null to isolate pure formatting CPU cost with zero IO overhead.
5122
*/
5223
@Fork(3)
5324
@Warmup(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS)
@@ -84,110 +55,17 @@ public class HistogramTextFormatBenchmark {
8455
private static final PrometheusTextFormatWriter PROMETHEUS_TEXT_FORMAT_WRITER =
8556
PrometheusTextFormatWriter.create();
8657

87-
@State(Scope.Benchmark)
88-
public static class WriterState {
89-
90-
final ByteArrayOutputStream byteArrayOutputStream;
91-
92-
public WriterState() {
93-
this.byteArrayOutputStream = new ByteArrayOutputStream();
94-
}
95-
}
96-
97-
@State(Scope.Benchmark)
98-
public static class ReusableWriterState {
99-
100-
final ByteArrayOutputStream openMetricsByteArrayOutputStream;
101-
final ByteArrayOutputStream prometheusByteArrayOutputStream;
102-
final BufferedWriter openMetricsWriter;
103-
final BufferedWriter prometheusWriter;
104-
105-
public ReusableWriterState() {
106-
this.openMetricsByteArrayOutputStream = new ByteArrayOutputStream();
107-
this.prometheusByteArrayOutputStream = new ByteArrayOutputStream();
108-
this.openMetricsWriter =
109-
new BufferedWriter(
110-
new OutputStreamWriter(openMetricsByteArrayOutputStream, StandardCharsets.UTF_8));
111-
this.prometheusWriter =
112-
new BufferedWriter(
113-
new OutputStreamWriter(prometheusByteArrayOutputStream, StandardCharsets.UTF_8));
114-
}
115-
}
116-
117-
@State(Scope.Benchmark)
118-
public static class ReusableWriterToNullState {
119-
120-
final BufferedWriter openMetricsWriter;
121-
final BufferedWriter prometheusWriter;
122-
123-
public ReusableWriterToNullState() {
124-
OutputStream nullOutputStream = TextFormatUtilBenchmark.NullOutputStream.INSTANCE;
125-
this.openMetricsWriter =
126-
new BufferedWriter(new OutputStreamWriter(nullOutputStream, StandardCharsets.UTF_8));
127-
this.prometheusWriter =
128-
new BufferedWriter(new OutputStreamWriter(nullOutputStream, StandardCharsets.UTF_8));
129-
}
130-
}
131-
132-
@Benchmark
133-
public OutputStream openMetricsWriteToByteArray(WriterState writerState) throws IOException {
134-
ByteArrayOutputStream byteArrayOutputStream = writerState.byteArrayOutputStream;
135-
byteArrayOutputStream.reset();
136-
OPEN_METRICS_TEXT_FORMAT_WRITER.write(
137-
byteArrayOutputStream, SNAPSHOTS, EscapingScheme.ALLOW_UTF8);
138-
return byteArrayOutputStream;
139-
}
140-
14158
@Benchmark
14259
public OutputStream openMetricsWriteToNull() throws IOException {
14360
OutputStream nullOutputStream = TextFormatUtilBenchmark.NullOutputStream.INSTANCE;
14461
OPEN_METRICS_TEXT_FORMAT_WRITER.write(nullOutputStream, SNAPSHOTS, EscapingScheme.ALLOW_UTF8);
14562
return nullOutputStream;
14663
}
14764

148-
@Benchmark
149-
public Writer openMetricsReusingWriter(ReusableWriterState state) throws IOException {
150-
state.openMetricsByteArrayOutputStream.reset();
151-
OPEN_METRICS_TEXT_FORMAT_WRITER.write(
152-
state.openMetricsWriter, SNAPSHOTS, EscapingScheme.ALLOW_UTF8);
153-
return state.openMetricsWriter;
154-
}
155-
156-
@Benchmark
157-
public Writer openMetricsReusingWriterToNull(ReusableWriterToNullState state) throws IOException {
158-
OPEN_METRICS_TEXT_FORMAT_WRITER.write(
159-
state.openMetricsWriter, SNAPSHOTS, EscapingScheme.ALLOW_UTF8);
160-
return state.openMetricsWriter;
161-
}
162-
163-
@Benchmark
164-
public OutputStream prometheusWriteToByteArray(WriterState writerState) throws IOException {
165-
ByteArrayOutputStream byteArrayOutputStream = writerState.byteArrayOutputStream;
166-
byteArrayOutputStream.reset();
167-
PROMETHEUS_TEXT_FORMAT_WRITER.write(
168-
byteArrayOutputStream, SNAPSHOTS, EscapingScheme.ALLOW_UTF8);
169-
return byteArrayOutputStream;
170-
}
171-
17265
@Benchmark
17366
public OutputStream prometheusWriteToNull() throws IOException {
17467
OutputStream nullOutputStream = TextFormatUtilBenchmark.NullOutputStream.INSTANCE;
17568
PROMETHEUS_TEXT_FORMAT_WRITER.write(nullOutputStream, SNAPSHOTS, EscapingScheme.ALLOW_UTF8);
17669
return nullOutputStream;
17770
}
178-
179-
@Benchmark
180-
public Writer prometheusReusingWriter(ReusableWriterState state) throws IOException {
181-
state.prometheusByteArrayOutputStream.reset();
182-
PROMETHEUS_TEXT_FORMAT_WRITER.write(
183-
state.prometheusWriter, SNAPSHOTS, EscapingScheme.ALLOW_UTF8);
184-
return state.prometheusWriter;
185-
}
186-
187-
@Benchmark
188-
public Writer prometheusReusingWriterToNull(ReusableWriterToNullState state) throws IOException {
189-
PROMETHEUS_TEXT_FORMAT_WRITER.write(
190-
state.prometheusWriter, SNAPSHOTS, EscapingScheme.ALLOW_UTF8);
191-
return state.prometheusWriter;
192-
}
19371
}

prometheus-metrics-exposition-textformats/src/main/java/io/prometheus/metrics/expositionformats/OpenMetrics2TextFormatWriter.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,6 @@ public OpenMetrics2Properties getOpenMetrics2Properties() {
144144
public void write(OutputStream out, MetricSnapshots metricSnapshots, EscapingScheme scheme)
145145
throws IOException {
146146
Writer writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
147-
write(writer, metricSnapshots, scheme);
148-
}
149-
150-
public void write(Writer writer, MetricSnapshots metricSnapshots, EscapingScheme scheme)
151-
throws IOException {
152147
MetricSnapshots merged = TextFormatUtil.mergeDuplicates(metricSnapshots);
153148
for (MetricSnapshot s : merged) {
154149
MetricSnapshot snapshot = SnapshotEscaper.escapeMetricSnapshot(s, scheme);

prometheus-metrics-exposition-textformats/src/main/java/io/prometheus/metrics/expositionformats/OpenMetricsTextFormatWriter.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,6 @@ public String getContentType() {
114114
public void write(OutputStream out, MetricSnapshots metricSnapshots, EscapingScheme scheme)
115115
throws IOException {
116116
Writer writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
117-
write(writer, metricSnapshots, scheme);
118-
}
119-
120-
public void write(Writer writer, MetricSnapshots metricSnapshots, EscapingScheme scheme)
121-
throws IOException {
122117
MetricSnapshots merged = TextFormatUtil.mergeDuplicates(metricSnapshots);
123118
for (MetricSnapshot s : merged) {
124119
MetricSnapshot snapshot = SnapshotEscaper.escapeMetricSnapshot(s, scheme);

prometheus-metrics-exposition-textformats/src/main/java/io/prometheus/metrics/expositionformats/PrometheusTextFormatWriter.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,10 @@ public String getContentType() {
112112
@Override
113113
public void write(OutputStream out, MetricSnapshots metricSnapshots, EscapingScheme scheme)
114114
throws IOException {
115-
Writer writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
116-
write(writer, metricSnapshots, scheme);
117-
}
118-
119-
public void write(Writer writer, MetricSnapshots metricSnapshots, EscapingScheme scheme)
120-
throws IOException {
121115
// See https://prometheus.io/docs/instrumenting/exposition_formats/
122116
// "unknown", "gauge", "counter", "stateset", "info", "histogram", "gaugehistogram", and
123117
// "summary".
118+
Writer writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
124119
MetricSnapshots merged = TextFormatUtil.mergeDuplicates(metricSnapshots);
125120
for (MetricSnapshot s : merged) {
126121
MetricSnapshot snapshot = escapeMetricSnapshot(s, scheme);

0 commit comments

Comments
 (0)