Commit 7016bf7
Use bulk string writes for text formats (#1273)
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 <[email protected]>
Co-authored-by: Doug Hoard <[email protected]>1 parent 39a83b9 commit 7016bf7
File tree
4 files changed
+189
-18
lines changed- benchmarks
- src/main/java/io/prometheus/metrics/benchmarks
- prometheus-metrics-exposition-textformats/src
- main/java/io/prometheus/metrics/expositionformats
- test/java/io/prometheus/metrics/expositionformats
4 files changed
+189
-18
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
54 | 48 | | |
55 | | - | |
56 | 49 | | |
57 | 50 | | |
58 | | - | |
| 51 | + | |
59 | 52 | | |
60 | 53 | | |
61 | 54 | | |
| |||
Lines changed: 122 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
Lines changed: 39 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
38 | | - | |
39 | | - | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
40 | 64 | | |
41 | 65 | | |
42 | | - | |
| 66 | + | |
| 67 | + | |
43 | 68 | | |
44 | 69 | | |
45 | | - | |
| 70 | + | |
| 71 | + | |
46 | 72 | | |
47 | 73 | | |
48 | | - | |
| 74 | + | |
| 75 | + | |
49 | 76 | | |
50 | | - | |
51 | | - | |
52 | 77 | | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
53 | 85 | | |
54 | 86 | | |
55 | 87 | | |
| |||
Lines changed: 24 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
0 commit comments