Skip to content

Commit

Permalink
Cargo bench
Browse files Browse the repository at this point in the history
  • Loading branch information
kazcw committed Jul 24, 2024
1 parent 50149bd commit 57c76d5
Showing 1 changed file with 64 additions and 9 deletions.
73 changes: 64 additions & 9 deletions lib/rust/parser/debug/src/bin/bench_parse.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Parses Enso sources, measuring time spent in the parser.
#![feature(test)]
// === Non-Standard Linter Configuration ===
#![allow(clippy::option_map_unit_fn)]
#![allow(clippy::precedence)]
Expand All @@ -11,24 +12,78 @@



// =============
// === Tests ===
// =============
// ===========
// === CLI ===
// ===========

fn main() {
let args = std::env::args().skip(1);
let parser = enso_parser::Parser::new();
let parse_time: std::time::Duration = args
.map(|path| {
let code = std::fs::read_to_string(path).unwrap();
let mut code = code.as_str();
if let Some((_meta, code_)) = enso_parser::metadata::parse(code) {
code = code_;
}
let code = read_source(path).unwrap();
let start = std::time::Instant::now();
std::hint::black_box(parser.run(code));
std::hint::black_box(parser.run(&code));
start.elapsed()
})
.sum();
println!("Parse time: {} ms", parse_time.as_millis());
}

fn read_source(path: impl AsRef<Path>) -> io::Result<String> {
let code = fs::read_to_string(path)?;
Ok(if let Some((_meta, code)) = enso_parser::metadata::parse(&code) {
code.to_owned()
} else {
code
})
}



// ===============================
// === `cargo bench` interface ===
// ===============================

extern crate test;

use std::fs::DirEntry;
use std::fs::{self};
use std::io;
use std::path::Path;

fn visit_files<F: FnMut(&DirEntry)>(dir: &Path, f: &mut F) -> io::Result<()> {
if dir.is_dir() {
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
visit_files(&path, f)?;
} else {
f(&entry);
}
}
}
Ok(())
}

#[bench]
fn bench_std_lib(b: &mut Bencher) {
let mut sources = vec![];
visit_files(Path::new("../../../../distribution/lib"), &mut |dir_ent| {
let path = dir_ent.path();
if let Some(ext) = path.extension() {
if ext == "enso" {
sources.push(read_source(path).unwrap())
}
}
})
.unwrap();
let parser = enso_parser::Parser::new();
b.bytes = sources.iter().map(|s| s.len() as u64).sum();
b.iter(|| {
for source in &sources {
test::black_box(parser.run(source));
}
});
}

0 comments on commit 57c76d5

Please sign in to comment.