-
Notifications
You must be signed in to change notification settings - Fork 830
fix: Reduce allocations for classic histogram buckets #2081
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
96a0061
Create name just one
jaydeluca 29f7c73
add benchmark
jaydeluca 9444848
more allocations avoided
jaydeluca 5fe88b1
fix lint
jaydeluca b29c0f1
update benchmarks
jaydeluca b1f89ba
format
jaydeluca 64bc726
remove shared writer for now
jaydeluca 9168d96
simplify
jaydeluca 314fb28
add command to nightly benchmark job too
jaydeluca File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
benchmarks/src/main/java/io/prometheus/metrics/benchmarks/HistogramTextFormatBenchmark.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package io.prometheus.metrics.benchmarks; | ||
|
|
||
| import io.prometheus.metrics.config.EscapingScheme; | ||
| import io.prometheus.metrics.expositionformats.OpenMetricsTextFormatWriter; | ||
| import io.prometheus.metrics.expositionformats.PrometheusTextFormatWriter; | ||
| import io.prometheus.metrics.model.snapshots.ClassicHistogramBuckets; | ||
| import io.prometheus.metrics.model.snapshots.HistogramSnapshot; | ||
| import io.prometheus.metrics.model.snapshots.HistogramSnapshot.HistogramDataPointSnapshot; | ||
| import io.prometheus.metrics.model.snapshots.Labels; | ||
| import io.prometheus.metrics.model.snapshots.MetricSnapshots; | ||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
| import java.util.concurrent.TimeUnit; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Measurement; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
|
|
||
| /** | ||
| * Benchmarks for writing a classic histogram (10 label combinations × 12 buckets) to text formats. | ||
| * Output goes to /dev/null to isolate pure formatting CPU cost with zero IO overhead. | ||
| */ | ||
| @Fork(3) | ||
| @Warmup(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS) | ||
| @Measurement(iterations = 10, time = 2, timeUnit = TimeUnit.SECONDS) | ||
| public class HistogramTextFormatBenchmark { | ||
|
|
||
| private static final MetricSnapshots SNAPSHOTS; | ||
|
|
||
| static { | ||
| double[] upperBounds = { | ||
| .005, .01, .025, .05, .1, .25, .5, 1.0, 2.5, 5.0, 10.0, Double.POSITIVE_INFINITY | ||
| }; | ||
| Number[] counts = {1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L}; | ||
| ClassicHistogramBuckets buckets = ClassicHistogramBuckets.of(upperBounds, counts); | ||
|
|
||
| HistogramSnapshot.Builder builder = | ||
| HistogramSnapshot.builder().name("http_request_duration_seconds"); | ||
|
|
||
| for (int i = 0; i < 10; i++) { | ||
| builder.dataPoint( | ||
| HistogramDataPointSnapshot.builder() | ||
| .classicHistogramBuckets(buckets) | ||
| .labels(Labels.of("status", "value_" + i)) | ||
| .sum(123.456) | ||
| .createdTimestampMillis(1000L) | ||
| .build()); | ||
| } | ||
|
|
||
| SNAPSHOTS = MetricSnapshots.of(builder.build()); | ||
| } | ||
|
|
||
| private static final OpenMetricsTextFormatWriter OPEN_METRICS_TEXT_FORMAT_WRITER = | ||
| OpenMetricsTextFormatWriter.create(); | ||
| private static final PrometheusTextFormatWriter PROMETHEUS_TEXT_FORMAT_WRITER = | ||
| PrometheusTextFormatWriter.create(); | ||
|
|
||
| @Benchmark | ||
| public OutputStream openMetricsWriteToNull() throws IOException { | ||
| OutputStream nullOutputStream = TextFormatUtilBenchmark.NullOutputStream.INSTANCE; | ||
| OPEN_METRICS_TEXT_FORMAT_WRITER.write(nullOutputStream, SNAPSHOTS, EscapingScheme.ALLOW_UTF8); | ||
| return nullOutputStream; | ||
| } | ||
|
|
||
| @Benchmark | ||
| public OutputStream prometheusWriteToNull() throws IOException { | ||
| OutputStream nullOutputStream = TextFormatUtilBenchmark.NullOutputStream.INSTANCE; | ||
| PROMETHEUS_TEXT_FORMAT_WRITER.write(nullOutputStream, SNAPSHOTS, EscapingScheme.ALLOW_UTF8); | ||
| return nullOutputStream; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.