Skip to content
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

Use buffered writer #1241

Merged
merged 1 commit into from
Jan 6, 2025
Merged

Conversation

michaldo
Copy link
Contributor

Without buffered reader, every single output character through given stack goes to "StreamEncoder public void write(int c)" and single element char array is created. It makes pressure to GC and CPU

In my case 800K char[1] arrays are created and garbage collected

BufferedReader reduce the pressure

write:130, StreamEncoder (sun.nio.cs)
write:201, OutputStreamWriter (java.io)
writeMetadata:338, PrometheusTextFormatWriter (io.prometheus.metrics.expositionformats) 
writeGauge:129, PrometheusTextFormatWriter (io.prometheus.metrics.expositionformats) 
write:68, PrometheusTextFormatWriter (io.prometheus.metrics.expositionformats) 
write:48, PrometheusOutputFormat$1 (org.springframework.boot.actuate.metrics.export.prometheus) 
scrape:87, PrometheusScrapeEndpoint (org.springframework.boot.actuate.metrics.export.prometheus)
StreamEncoder public void write(int c) throws IOException {
  char[] cbuf = new char[1];
  cbuf[0] = (char) c;

Without buffered reader, every single output character through given stack goes to "StreamEncoder public void write(int c)" and single element char array is created.
It makes pressure to GC and CPU
BufferedReader reduce the pressure

write:130, StreamEncoder (sun.nio.cs)
write:201, OutputStreamWriter (java.io)
writeMetadata:338, PrometheusTextFormatWriter (io.prometheus.metrics.expositionformats)
writeGauge:129, PrometheusTextFormatWriter (io.prometheus.metrics.expositionformats)
write:68, PrometheusTextFormatWriter (io.prometheus.metrics.expositionformats)
write:48, PrometheusOutputFormat$1 (org.springframework.boot.actuate.metrics.export.prometheus)
scrape:87, PrometheusScrapeEndpoint (org.springframework.boot.actuate.metrics.export.prometheus)

StreamEncoder public void write(int c) throws IOException {
  char[] cbuf = new char[1];
  cbuf[0] = (char) c;

Signed-off-by: Michal Domagala <michaldo@github.io>
@michaldo michaldo force-pushed the use-buffered-writer branch from b26dd34 to b9421b5 Compare December 22, 2024 18:41
@zeitlinger
Copy link
Member

Thanks for the contribution!

@zeitlinger zeitlinger merged commit 9043b47 into prometheus:main Jan 6, 2025
4 checks passed
@dhoard dhoard mentioned this pull request Jan 6, 2025
dhoard added a commit that referenced this pull request Feb 18, 2025
Further optimize `TextFormatUtil#writeEscapedLabelValue`. We're seeing
`TextFormatUtil#writeEscapedLabelValue` show up in our production traces
due to the single `char` writes to `OutputStreamWriter`. We're using
`OpenMetricsTextFormatWriter`. #1241 and #1248 should take care of most
of these issues but there still remains some optimization potential
left. `BufferedWriter#write(int)` has some minimal overhead in terms of
locking. If we assume that rarely any characters need to be escaped and
instead optimize to write as large of a part of the String as possible
in one method call.

Before
---------
```
Benchmark                                             Mode  Cnt       Score      Error  Units
TextFormatUtilBenchmark.openMetricsWriteToByteArray  thrpt   25  438485.372 ± 4270.355  ops/s
TextFormatUtilBenchmark.openMetricsWriteToNull       thrpt   25  440105.281 ± 2891.572  ops/s
TextFormatUtilBenchmark.prometheusWriteToByteArray   thrpt   25  467213.001 ±  878.780  ops/s
TextFormatUtilBenchmark.prometheusWriteToNull        thrpt   25  472931.759 ±  976.028  ops/s
```

After
-------

```
Benchmark                                             Mode  Cnt       Score      Error  Units
TextFormatUtilBenchmark.openMetricsWriteToByteArray  thrpt   25  462852.243 ± 5071.696  ops/s
TextFormatUtilBenchmark.openMetricsWriteToNull       thrpt   25  469910.681 ± 1670.430  ops/s
TextFormatUtilBenchmark.prometheusWriteToByteArray   thrpt   25  482362.506 ± 2051.684  ops/s
TextFormatUtilBenchmark.prometheusWriteToNull        thrpt   25  487707.557 ± 3344.881  ops/s
```

About a 5% to 6% gain for `OpenMetricsTextFormatWriter` and around 3%
for `PrometheusTextFormatWriter`.

Note that this benchmark is actually for `OpenMetricsTextFormatWriter`
and `PrometheusTextFormatWriter` since `TextFormatUtil` is not public
and can therefore not be benchmarked directly. The relative gains in
`#writeEscapedLabelValue` are higher because the benchmark runs the full
text format writers.

I also added a test for `TextFormatUtil#writeEscapedLabelValue` since
the code is now more complicated.

---------

Signed-off-by: Philippe Marschall <philippe.marschall@gmail.com>
Co-authored-by: Doug Hoard <dhoard@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants