-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reapply "perf(interpreter): improve ATC interprater's cache efficiency (
- Loading branch information
Showing
12 changed files
with
598 additions
and
75 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use atc_router::ast::{Type, Value}; | ||
use atc_router::context::Context; | ||
use atc_router::router::Router; | ||
use atc_router::schema::Schema; | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use uuid::Uuid; | ||
|
||
// To run this benchmark, execute the following command: | ||
// ```shell | ||
// cargo bench --bench build | ||
// ``` | ||
|
||
const N: usize = 1000; | ||
|
||
fn make_uuid(a: usize) -> String { | ||
format!("8cb2a7d0-c775-4ed9-989f-{:012}", a) | ||
} | ||
|
||
fn criterion_benchmark(c: &mut Criterion) { | ||
let mut schema = Schema::default(); | ||
schema.add_field("a", Type::Int); | ||
|
||
let mut context = Context::new(&schema); | ||
context.add_value("a", Value::Int(N as i64)); | ||
|
||
c.bench_function("Build Router", |b| { | ||
b.iter_with_large_drop(|| { | ||
let mut router = Router::new(&schema); | ||
for i in 0..N { | ||
let expr = format!("((a > 0 || a < {}) && a != 0) && a == 1", N + 1); | ||
let variant = make_uuid(i); | ||
let uuid = Uuid::try_from(variant.as_str()).unwrap(); | ||
router.add_matcher(N - i, uuid, &expr).unwrap(); | ||
} | ||
router | ||
}); | ||
}); | ||
} | ||
|
||
criterion_group!(benches, criterion_benchmark); | ||
criterion_main!(benches); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use atc_router::ast::Value; | ||
use atc_router::context::Context; | ||
use atc_router::router::Router; | ||
use atc_router::schema::Schema; | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use uuid::Uuid; | ||
|
||
// To run this benchmark, execute the following command: | ||
// ```shell | ||
// cargo bench --bench match_mix | ||
// ``` | ||
|
||
const N: usize = 100000; | ||
|
||
fn make_uuid(a: usize) -> String { | ||
format!("8cb2a7d0-c775-4ed9-989f-{:012}", a) | ||
} | ||
|
||
fn criterion_benchmark(c: &mut Criterion) { | ||
let mut schema = Schema::default(); | ||
schema.add_field("http.path", atc_router::ast::Type::String); | ||
schema.add_field("http.version", atc_router::ast::Type::String); | ||
schema.add_field("a", atc_router::ast::Type::Int); | ||
|
||
let mut router = Router::new(&schema); | ||
|
||
for i in 0..N { | ||
let expr = format!( | ||
r#"(http.path == "hello{}" && http.version == "1.1") || !(( a == 2) && ( a == 9 )) || !(a == 1) || ( a == 3 && a == 4) && !(a == 5)"#, | ||
i | ||
); | ||
let variant = make_uuid(i); | ||
let uuid = Uuid::try_from(variant.as_str()).unwrap(); | ||
router.add_matcher(N - i, uuid, &expr).unwrap(); | ||
} | ||
|
||
let mut ctx_match = Context::new(&schema); | ||
ctx_match.add_value( | ||
"http.path", | ||
atc_router::ast::Value::String("hello49999".to_string()), | ||
); | ||
ctx_match.add_value( | ||
"http.version", | ||
atc_router::ast::Value::String("1.1".to_string()), | ||
); | ||
ctx_match.add_value("a", Value::Int(3 as i64)); | ||
|
||
c.bench_function("Match", |b| { | ||
b.iter(|| { | ||
let is_match = router.execute(&mut ctx_match); | ||
assert!(is_match); | ||
}); | ||
}); | ||
} | ||
|
||
criterion_group!(benches, criterion_benchmark); | ||
criterion_main!(benches); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use atc_router::ast::{Type, Value}; | ||
use atc_router::context::Context; | ||
use atc_router::router::Router; | ||
use atc_router::schema::Schema; | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use uuid::Uuid; | ||
|
||
// To run this benchmark, execute the following command: | ||
// ```shell | ||
// cargo bench --bench not_match_mix | ||
// ``` | ||
|
||
const N: usize = 100000; | ||
|
||
fn make_uuid(a: usize) -> String { | ||
format!("8cb2a7d0-c775-4ed9-989f-{:012}", a) | ||
} | ||
|
||
fn criterion_benchmark(c: &mut Criterion) { | ||
let mut schema = Schema::default(); | ||
schema.add_field("http.path", atc_router::ast::Type::String); | ||
schema.add_field("http.version", atc_router::ast::Type::String); | ||
schema.add_field("a", atc_router::ast::Type::Int); | ||
|
||
let mut router = Router::new(&schema); | ||
|
||
for i in 0..N { | ||
let expr = format!( | ||
r#"(http.path == "hello{}" && http.version == "1.1") || !(( a == 2) && ( a == 9 )) || !(a == 1) || ( a == 3 && a == 4) && !(a == 5)"#, | ||
i | ||
); | ||
let variant = make_uuid(i); | ||
let uuid = Uuid::try_from(variant.as_str()).unwrap(); | ||
router.add_matcher(N - i, uuid, &expr).unwrap(); | ||
} | ||
|
||
let mut ctx_match = Context::new(&schema); | ||
ctx_match.add_value( | ||
"http.path", | ||
atc_router::ast::Value::String("hello49999".to_string()), | ||
); | ||
ctx_match.add_value( | ||
"http.version", | ||
atc_router::ast::Value::String("1.1".to_string()), | ||
); | ||
ctx_match.add_value("a", Value::Int(5 as i64)); | ||
|
||
c.bench_function("Doesn't Match", |b| { | ||
b.iter(|| { | ||
let is_match = router.execute(&mut ctx_match); | ||
assert!(!is_match); | ||
}); | ||
}); | ||
} | ||
|
||
criterion_group!(benches, criterion_benchmark); | ||
criterion_main!(benches); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use atc_router::ast::{Type, Value}; | ||
use atc_router::context::Context; | ||
use atc_router::router::Router; | ||
use atc_router::schema::Schema; | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use uuid::Uuid; | ||
|
||
// To run this benchmark, execute the following command: | ||
// ```shell | ||
// cargo bench --bench string | ||
// ``` | ||
|
||
const N_MATCHER: usize = 1; | ||
const N_EXECUTE: usize = 100000; | ||
|
||
fn make_uuid(a: usize) -> String { | ||
format!("8cb2a7d0-c775-4ed9-989f-{:012}", a) | ||
} | ||
|
||
fn criterion_benchmark(c: &mut Criterion) { | ||
let mut schema = Schema::default(); | ||
schema.add_field("http.path.segments.*", Type::String); | ||
schema.add_field("http.path.segments.len", Type::Int); | ||
|
||
let mut router = Router::new(&schema); | ||
|
||
for i in 0..N_MATCHER { | ||
let expr = format!( | ||
r#"http.path.segments.0_1 == "test/run" && http.path.segments.3 == "address{}" && http.path.segments.len == 3"#, | ||
i | ||
); | ||
let variant = make_uuid(i); | ||
let uuid = Uuid::try_from(variant.as_str()).unwrap(); | ||
router.add_matcher(N_MATCHER - i, uuid, &expr).unwrap(); | ||
} | ||
|
||
let mut context = Context::new(&schema); | ||
context.add_value("http.path.segments.0_1", "test/run".to_string().into()); | ||
context.add_value("http.path.segments.3", "bar".to_string().into()); | ||
context.add_value("http.path.segments.len", Value::Int(3 as i64)); | ||
|
||
c.bench_function("Doesn't Match", |b| { | ||
b.iter(|| { | ||
for _i in 0..N_EXECUTE { | ||
let is_match = router.execute(&mut context); | ||
assert!(!is_match); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
criterion_group!(benches, criterion_benchmark); | ||
criterion_main!(benches); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use atc_router::ast::{Type, Value}; | ||
use atc_router::context::Context; | ||
use atc_router::router::Router; | ||
use atc_router::schema::Schema; | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use uuid::Uuid; | ||
|
||
// To run this benchmark, execute the following command: | ||
// ```shell | ||
// cargo bench --bench test | ||
// ``` | ||
|
||
const N: usize = 100000; | ||
|
||
fn make_uuid(a: usize) -> String { | ||
format!("8cb2a7d0-c775-4ed9-989f-{:012}", a) | ||
} | ||
|
||
fn criterion_benchmark(c: &mut Criterion) { | ||
let mut schema = Schema::default(); | ||
schema.add_field("a", Type::Int); | ||
|
||
let mut router = Router::new(&schema); | ||
|
||
for i in 0..N { | ||
let expr = format!("((a > 0 || a < {}) && a != 0) && a == 1", N + 1); | ||
let variant = make_uuid(i); | ||
let uuid = Uuid::try_from(variant.as_str()).unwrap(); | ||
router.add_matcher(N - i, uuid, &expr).unwrap(); | ||
} | ||
|
||
let mut context = Context::new(&schema); | ||
context.add_value("a", Value::Int(N as i64)); | ||
|
||
c.bench_function("Doesn't Match", |b| { | ||
b.iter(|| { | ||
for _ in 0..10 { | ||
let is_match = router.execute(&mut context); | ||
assert!(!is_match); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
criterion_group!(benches, criterion_benchmark); | ||
criterion_main!(benches); |
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
Oops, something went wrong.