-
Notifications
You must be signed in to change notification settings - Fork 316
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
fix: fix line protocol parser #1833
Merged
Merged
Conversation
This file contains 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
Kree0
force-pushed
the
fix/lp
branch
2 times, most recently
from
December 20, 2023 03:30
01b7c1e
to
6182b94
Compare
Performance
Old
Now
Bench codeRustuse std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
use std::process::exit;
use std::time::Duration;
use protocol_parser::line_protocol::parser::Parser;
fn print_usage() {
eprintln!(
r#"Benchmark for line protocol parser.
Usage: benchmark single_line <file path>
or benchmark multi_line <file path> <line num>
"#
);
}
fn main() {
let args = std::env::args().skip(1).collect::<Vec<String>>();
if args.len() < 2 || args.len() > 3 {
print_usage();
exit(1);
}
let mode = args[0].as_str();
eprintln!("- mode: {mode}");
let path = PathBuf::from(&args[1]);
eprintln!("- path: {}", path.display());
let file = File::open(path).unwrap();
let parser = Parser::new(1);
let start_time = std::time::Instant::now();
if mode == "single_line" {
let avg = single_line_mode(file, &parser);
println!("avg: {}ns", avg.as_nanos());
} else if mode == "multi_line" {
if args.len() != 3 {
print_usage();
exit(1);
}
let line_num: u32 = args[2].parse().unwrap();
eprintln!("- line: {line_num}");
let avg = multi_line_mode(file, &parser, line_num as usize);
println!("avg: {}ns", avg.as_nanos());
} else {
print_usage();
exit(1);
}
println!("total: {}s", start_time.elapsed().as_secs());
}
fn single_line_mode(file: File, parser: &Parser) -> Duration {
let mut reader = BufReader::with_capacity(1024 * 1024 * 128, file);
let mut line = String::with_capacity(1024);
let mut avg = 0_u128;
let mut avg_n = 0_usize;
loop {
match reader.read_line(&mut line) {
Ok(n) => {
if n == 0 {
break;
}
let start_time = std::time::Instant::now();
match parser.parse(&line) {
Ok(_l) => {
avg = avg + start_time.elapsed().as_nanos();
avg_n += 1;
if avg_n == 10000 {
avg = avg / 10000;
avg_n = 0;
}
}
Err(e) => panic!("Parse error: {e}"),
}
line.clear();
}
Err(e) => panic!("Read file error: {e}"),
}
}
Duration::from_nanos((avg / avg_n as u128) as u64)
}
fn multi_line_mode(file: File, parser: &Parser, line_num: usize) -> Duration {
let mut reader = BufReader::with_capacity(1024 * 1024 * 16, file);
let mut buf = String::with_capacity(1024 * 1024);
let mut ln = 0_usize;
let mut avg = 0_u128;
let mut avg_n = 0_usize;
loop {
match reader.read_line(&mut buf) {
Ok(n) => {
if n == 0 {
let start_time = std::time::Instant::now();
match parser.parse(&buf) {
Ok(_l) => {
avg = avg + start_time.elapsed().as_nanos();
avg_n += 1;
avg = avg / avg_n as u128;
}
Err(e) => panic!("Parse error: {e}"),
}
break;
}
if ln < line_num {
ln += 1;
continue;
}
ln = 0;
let start_time = std::time::Instant::now();
match parser.parse(&buf) {
Ok(_l) => {
avg = avg + start_time.elapsed().as_nanos();
avg_n += 1;
if avg_n == 10000 {
avg = avg / 10000;
avg_n = 0;
}
}
Err(e) => panic!("Parse error: {e}"),
}
buf.clear();
}
Err(e) => panic!("Read file error: {e}"),
}
}
Duration::from_nanos(avg as u64)
} ScriptSingle line modecargo run --release -- single_line seed-123_scale-100_20240101-20240104_interval-1s Multi line modecargo run --release -- multi_line seed-123_scale-100_20240101-20240104_interval-1s 1000 |
Kree0
force-pushed
the
fix/lp
branch
4 times, most recently
from
December 29, 2023 03:04
9977578
to
b47c85a
Compare
roseboy-liu
reviewed
Dec 29, 2023
roseboy-liu
previously approved these changes
Dec 29, 2023
roseboy-liu
approved these changes
Jan 2, 2024
This was referenced Jan 2, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Required checklist
config
,meta/config
anddefault config
)api change
label.Which issue does this PR close?
#1830
#1817
Rationale for this change
Are there any user-facing changes?