From 8083ae660244856d162b6db455aa5250afa02d8e Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 2 Aug 2019 12:01:35 -0700 Subject: [PATCH] benchmark: Parse each sample a configurable number of times Refs #343 --- cli/benches/benchmark.rs | 16 ++++++++++++---- script/benchmark | 9 +++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/cli/benches/benchmark.rs b/cli/benches/benchmark.rs index ef67bd21..667b566b 100644 --- a/cli/benches/benchmark.rs +++ b/cli/benches/benchmark.rs @@ -14,6 +14,10 @@ lazy_static! { env::var("TREE_SITTER_BENCHMARK_LANGUAGE_FILTER").ok(); static ref EXAMPLE_FILTER: Option = env::var("TREE_SITTER_BENCHMARK_EXAMPLE_FILTER").ok(); + static ref REPETITION_COUNT: usize = env::var("TREE_SITTER_BENCHMARK_REPETITION_COUNT") + .map(|s| usize::from_str_radix(&s, 10).unwrap()) + .unwrap_or(5); + static ref TEST_LOADER: Loader = Loader::new(SCRATCH_DIR.clone()); static ref EXAMPLE_PATHS_BY_LANGUAGE_DIR: BTreeMap> = { fn process_dir(result: &mut BTreeMap>, dir: &Path) { @@ -73,6 +77,8 @@ fn main() { } } + eprintln!("Benchmarking with {} repetitions", *REPETITION_COUNT); + eprintln!("\nLanguage: {}", language_name); parser.set_language(get_language(language_path)).unwrap(); @@ -157,10 +163,12 @@ fn parse(parser: &mut Parser, example_path: &Path, max_path_length: usize) -> us .map_err(Error::wrap(|| format!("Failed to read {:?}", example_path))) .unwrap(); let time = Instant::now(); - let _tree = parser - .parse(&source_code, None) - .expect("Incompatible language version"); - let duration = time.elapsed(); + for _ in 0..*REPETITION_COUNT { + parser + .parse(&source_code, None) + .expect("Incompatible language version"); + } + let duration = time.elapsed() / (*REPETITION_COUNT as u32); let duration_ms = duration.as_secs() as f64 * 1000.0 + duration.subsec_nanos() as f64 / 1000000.0; let speed = (source_code.len() as f64 / duration_ms) as usize; diff --git a/script/benchmark b/script/benchmark index 9b4ec3f0..5be3465b 100755 --- a/script/benchmark +++ b/script/benchmark @@ -6,7 +6,7 @@ function usage { cat <<-EOF USAGE - $0 [-h] [-l language-name] [-e example-file-name] + $0 [-h] [-l language-name] [-e example-file-name] [-r repetition-count] OPTIONS @@ -16,10 +16,12 @@ OPTIONS -e run only the benchmarks that parse the example file with the given name + -r parse each sample the given number of times (default 5) + EOF } -while getopts "hl:e:" option; do +while getopts "hl:e:r:" option; do case ${option} in h) usage @@ -31,6 +33,9 @@ while getopts "hl:e:" option; do l) export TREE_SITTER_BENCHMARK_LANGUAGE_FILTER=${OPTARG} ;; + r) + export TREE_SITTER_BENCHMARK_REPETITION_COUNT=${OPTARG} + ;; esac done