Skip to content

Commit da93e55

Browse files
committed
In benchmarks, handle language repos that contain multiple grammars
Refs tree-sitter/tree-sitter-typescript#68
1 parent 659f282 commit da93e55

File tree

1 file changed

+42
-34
lines changed

1 file changed

+42
-34
lines changed

cli/benches/benchmark.rs

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,46 @@ lazy_static! {
1414
static ref EXAMPLE_FILTER: Option<String> =
1515
env::var("TREE_SITTER_BENCHMARK_EXAMPLE_FILTER").ok();
1616
static ref TEST_LOADER: Loader = Loader::new(SCRATCH_DIR.clone());
17-
static ref EXAMPLE_PATHS_BY_LANGUAGE_NAME: BTreeMap<String, Vec<PathBuf>> = {
18-
let mut result = BTreeMap::new();
19-
let grammar_dirs = fs::read_dir(&(*GRAMMARS_DIR)).unwrap();
20-
for grammar_dir in grammar_dirs {
21-
let grammar_dir = grammar_dir.unwrap();
22-
if !grammar_dir.path().is_dir() {
23-
continue;
24-
}
25-
26-
let language_name = grammar_dir.file_name();
27-
let language_name = language_name.to_str().unwrap();
28-
if let Ok(example_files) = fs::read_dir(&grammar_dir.path().join("examples")) {
29-
result.insert(
30-
language_name.to_string(),
31-
example_files
32-
.filter_map(|p| {
33-
let p = p.unwrap().path();
34-
if p.is_file() {
35-
Some(p)
36-
} else {
37-
None
38-
}
39-
})
40-
.collect(),
41-
);
17+
static ref EXAMPLE_PATHS_BY_LANGUAGE_DIR: BTreeMap<PathBuf, Vec<PathBuf>> = {
18+
fn process_dir(result: &mut BTreeMap<PathBuf, Vec<PathBuf>>, dir: &Path) {
19+
if dir.join("grammar.js").exists() {
20+
let relative_path = dir.strip_prefix(GRAMMARS_DIR.as_path()).unwrap();
21+
if let Ok(example_files) = fs::read_dir(&dir.join("examples")) {
22+
result.insert(
23+
relative_path.to_owned(),
24+
example_files
25+
.filter_map(|p| {
26+
let p = p.unwrap().path();
27+
if p.is_file() {
28+
Some(p)
29+
} else {
30+
None
31+
}
32+
})
33+
.collect(),
34+
);
35+
} else {
36+
result.insert(relative_path.to_owned(), Vec::new());
37+
}
4238
} else {
43-
result.insert(language_name.to_string(), Vec::new());
39+
for entry in fs::read_dir(&dir).unwrap() {
40+
let entry = entry.unwrap().path();
41+
if entry.is_dir() {
42+
process_dir(result, &entry);
43+
}
44+
}
4445
}
4546
}
4647

48+
let mut result = BTreeMap::new();
49+
process_dir(&mut result, &GRAMMARS_DIR);
4750
result
4851
};
4952
}
5053

5154
fn main() {
5255
let mut parser = Parser::new();
53-
let max_path_length = EXAMPLE_PATHS_BY_LANGUAGE_NAME
56+
let max_path_length = EXAMPLE_PATHS_BY_LANGUAGE_DIR
5457
.iter()
5558
.flat_map(|(_, paths)| paths.iter())
5659
.map(|p| p.file_name().unwrap().to_str().unwrap().chars().count())
@@ -60,15 +63,17 @@ fn main() {
6063
let mut all_normal_speeds = Vec::new();
6164
let mut all_error_speeds = Vec::new();
6265

63-
for (language_name, example_paths) in EXAMPLE_PATHS_BY_LANGUAGE_NAME.iter() {
66+
for (language_path, example_paths) in EXAMPLE_PATHS_BY_LANGUAGE_DIR.iter() {
67+
let language_name = language_path.file_name().unwrap().to_str().unwrap();
68+
6469
if let Some(filter) = LANGUAGE_FILTER.as_ref() {
6570
if language_name != filter.as_str() {
6671
continue;
6772
}
6873
}
6974

7075
eprintln!("\nLanguage: {}", language_name);
71-
parser.set_language(get_language(language_name)).unwrap();
76+
parser.set_language(get_language(language_path)).unwrap();
7277

7378
eprintln!(" Normal examples:");
7479
let mut normal_speeds = Vec::new();
@@ -84,8 +89,8 @@ fn main() {
8489

8590
eprintln!(" Error examples (mismatched languages):");
8691
let mut error_speeds = Vec::new();
87-
for (other_language_name, example_paths) in EXAMPLE_PATHS_BY_LANGUAGE_NAME.iter() {
88-
if other_language_name != language_name {
92+
for (other_language_path, example_paths) in EXAMPLE_PATHS_BY_LANGUAGE_DIR.iter() {
93+
if other_language_path != language_path {
8994
for example_path in example_paths {
9095
if let Some(filter) = EXAMPLE_FILTER.as_ref() {
9196
if !example_path.to_str().unwrap().contains(filter.as_str()) {
@@ -147,7 +152,9 @@ fn parse(parser: &mut Parser, example_path: &Path, max_path_length: usize) -> us
147152
width = max_path_length
148153
);
149154

150-
let source_code = fs::read(example_path).unwrap();
155+
let source_code = fs::read(example_path)
156+
.map_err(|e| format!("Failed to read {:?} - {}", example_path, e))
157+
.unwrap();
151158
let time = Instant::now();
152159
let _tree = parser
153160
.parse(&source_code, None)
@@ -160,9 +167,10 @@ fn parse(parser: &mut Parser, example_path: &Path, max_path_length: usize) -> us
160167
speed
161168
}
162169

163-
fn get_language(name: &str) -> Language {
164-
let src_dir = GRAMMARS_DIR.join(name).join("src");
170+
fn get_language(path: &Path) -> Language {
171+
let src_dir = GRAMMARS_DIR.join(path).join("src");
165172
TEST_LOADER
166173
.load_language_at_path(&src_dir, &src_dir)
174+
.map_err(|e| format!("Failed to load language at path {:?} - {:?}", src_dir, e))
167175
.unwrap()
168176
}

0 commit comments

Comments
 (0)