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

Divan benchmarking PoC #140

Merged
merged 9 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ features = ["derive"]
version = "1.0.61"

[dev-dependencies]
divan = "0.1.4"
insta = { version = "1.26.0", default-features = false }

[dev-dependencies.quickcheck]
Expand All @@ -52,5 +53,9 @@ tracing = ["dep:tracing", "dep:tracing-subscriber", "dep:tracing-flame"]
name = "bnf"
harness = false

[[bench]]
name = "divan"
harness = false

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2.5", features = ["js"] } # needed for rand
107 changes: 107 additions & 0 deletions benches/divan.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
fn main() {
init_tracing();

#[cfg(feature = "tracing")]
let _span = tracing::span!(tracing::Level::DEBUG, "BENCH EXAMPLES").entered();

// Run registered benchmarks.
divan::main();
}

#[cfg(feature = "tracing")]
fn init_tracing() -> impl Drop {
use tracing_flame::FlameLayer;
use tracing_subscriber::{fmt, prelude::*};
let filter_layer = tracing_subscriber::EnvFilter::from_default_env();
let fmt_layer = fmt::Layer::default();
let (flame_layer, _guard) = FlameLayer::with_file("./tracing.folded").unwrap();

tracing_subscriber::registry()
.with(filter_layer)
.with(fmt_layer)
.with(flame_layer)
.init();

_guard
}

#[cfg(not(feature = "tracing"))]
fn init_tracing() {}

mod examples {
#[divan::bench]
fn parse_postal(bencher: divan::Bencher) {
let input = divan::black_box(include_str!(
"../tests/fixtures/postal_address.terminated.input.bnf"
));

bencher.bench(|| {
input.parse::<bnf::Grammar>().unwrap();
});
}

#[divan::bench]
fn generate_dna(bencher: divan::Bencher) {
bencher
.with_inputs(|| {
let input = "<dna> ::= <base> | <base> <dna>
<base> ::= 'A' | 'C' | 'G' | 'T'";
let grammar: bnf::Grammar = input.parse().unwrap();
grammar
})
.bench_refs(|grammar| {
grammar.generate().unwrap();
});
}
CrockAgile marked this conversation as resolved.
Show resolved Hide resolved

#[divan::bench]
fn parse_polish_calculator(bencher: divan::Bencher) {
let polish_calc_grammar: bnf::Grammar = "<product> ::= <number> | <op> <product> <product>
<op> ::= '+' | '-' | '*' | '/'
<number> ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
"
.parse()
.unwrap();

// use pseudo random for consistent metrics
use rand::seq::SliceRandom;
let mut rng: rand::rngs::StdRng = rand::SeedableRng::seed_from_u64(0);
let random_walk_count = 100usize;
let mut random_walks: Vec<_> = (0..random_walk_count)
.map(|_| polish_calc_grammar.generate_seeded(&mut rng).unwrap())
.collect();

random_walks.shuffle(&mut rng);
let mut random_walks = divan::black_box(random_walks.into_iter());

bencher.bench_local(|| {
let input = random_walks.next().unwrap();
polish_calc_grammar
.parse_input(&input)
.for_each(|v| _ = divan::black_box(v));
CrockAgile marked this conversation as resolved.
Show resolved Hide resolved
});
}

#[divan::bench]
fn parse_infinite_nullable_grammar(bencher: divan::Bencher) {
use rand::Rng;

let infinite_grammar: bnf::Grammar = "
<a> ::= '' | <b>
<b> ::= <a>"
.parse()
.unwrap();

let mut rng: rand::rngs::StdRng = rand::SeedableRng::seed_from_u64(0);

bencher
.with_inputs(|| rng.gen_range(1..100))
.count_inputs_as::<divan::counter::ItemsCount>()
.bench_local_values(|parse_count| {
infinite_grammar
.parse_input("")
.take(parse_count)
.for_each(|v| _ = divan::black_box(v));
CrockAgile marked this conversation as resolved.
Show resolved Hide resolved
});
}
}
Loading