-
Notifications
You must be signed in to change notification settings - Fork 205
src/encoder/text: Optimize metric formatting #327
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 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
78e7810
src/benches&src/bin: Add basic text encoder benchmark
mxinden ad0b96a
src/encoder/text: Use `write_all` instead of `write_fmt`
mxinden fba40e7
src/encoder/text: Use regex crate to determine if escaping is needed
mxinden 93b35e2
src/encoder/text: Shrink escaped string before returning
mxinden 86c054b
Merge branch 'tikv/master' into text-encoder
mxinden 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Copyright 2020 PingCAP, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #![feature(test)] | ||
|
|
||
| extern crate test; | ||
|
|
||
| use prometheus::{CounterVec, Encoder, HistogramOpts, HistogramVec, Opts, Registry, TextEncoder}; | ||
| use test::Bencher; | ||
|
|
||
| #[bench] | ||
| fn bench_text_encoder_without_escaping(b: &mut Bencher) { | ||
| let registry = registry_with_test_metrics(false); | ||
| run_text_encoder(registry, b) | ||
| } | ||
|
|
||
| #[bench] | ||
| fn bench_text_encoder_with_escaping(b: &mut Bencher) { | ||
| let registry = registry_with_test_metrics(true); | ||
| run_text_encoder(registry, b) | ||
| } | ||
|
|
||
| fn registry_with_test_metrics(with_escaping: bool) -> Registry { | ||
| let registry = Registry::new(); | ||
|
|
||
| for i in 0..100 { | ||
| let counter = CounterVec::new( | ||
| Opts::new( | ||
| format!("benchmark_counter_{}", i), | ||
| "A counter to benchmark it.", | ||
| ), | ||
| &["one", "two", "three"], | ||
| ) | ||
| .unwrap(); | ||
| registry.register(Box::new(counter.clone())).unwrap(); | ||
|
|
||
| let histogram = HistogramVec::new( | ||
| HistogramOpts::new( | ||
| format!("benchmark_histogram_{}", i), | ||
| "A histogram to benchmark it.", | ||
| ), | ||
| &["one", "two", "three"], | ||
| ) | ||
| .unwrap(); | ||
| registry.register(Box::new(histogram.clone())).unwrap(); | ||
|
|
||
| for j in 0..100 { | ||
| let j_string = j.to_string(); | ||
| let label_values = if with_escaping { | ||
| ["ei\\ns\n", "zw\"e\"i", &j_string] | ||
| } else { | ||
| ["eins", "zwei", &j_string] | ||
| }; | ||
|
|
||
| counter.with_label_values(&label_values).inc(); | ||
| histogram.with_label_values(&label_values).observe(j.into()); | ||
| } | ||
| } | ||
|
|
||
| registry | ||
| } | ||
|
|
||
| fn run_text_encoder(registry: Registry, b: &mut Bencher) { | ||
| let mut buffer = vec![]; | ||
| let encoder = TextEncoder::new(); | ||
| let metric_families = registry.gather(); | ||
|
|
||
| b.iter(|| encoder.encode(&metric_families, &mut buffer).unwrap()); | ||
| } |
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
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.