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 #1248

Merged
merged 1 commit into from
Jan 6, 2025
Merged

Use buffered writer #1248

merged 1 commit into from
Jan 6, 2025

Conversation

dhoard
Copy link
Collaborator

@dhoard dhoard commented Jan 6, 2025

Code to use buffered writer when writing OpenMetrics, similar to the changes made in PrometheusTextWriter (PR #1241)

Signed-off-by: dhoard <doug.hoard@gmail.com>
@dhoard dhoard requested a review from zeitlinger January 6, 2025 14:14
@dhoard dhoard merged commit 64f02ea into prometheus:main Jan 6, 2025
1 check passed
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.

1 participant