benchmark: Parse each sample a configurable number of times

Refs #343
This commit is contained in:
Max Brunsfeld 2019-08-02 12:01:35 -07:00
parent 0afbc31789
commit 8083ae6602
2 changed files with 19 additions and 6 deletions

View file

@ -14,6 +14,10 @@ lazy_static! {
env::var("TREE_SITTER_BENCHMARK_LANGUAGE_FILTER").ok();
static ref EXAMPLE_FILTER: Option<String> =
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<PathBuf, Vec<PathBuf>> = {
fn process_dir(result: &mut BTreeMap<PathBuf, Vec<PathBuf>>, 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;

View file

@ -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