|
1 | | -use std::{path::Path, sync::Arc}; |
| 1 | +use std::{ |
| 2 | + path::{Path, PathBuf}, |
| 3 | + sync::Arc, |
| 4 | +}; |
2 | 5 |
|
| 6 | +use oxc_span::SourceType; |
3 | 7 | use rustc_hash::FxHashMap; |
4 | 8 |
|
5 | 9 | use oxc_allocator::Allocator; |
@@ -61,5 +65,107 @@ fn bench_linter(criterion: &mut Criterion) { |
61 | 65 | group.finish(); |
62 | 66 | } |
63 | 67 |
|
64 | | -criterion_group!(linter, bench_linter); |
| 68 | +struct RepoTestFile { |
| 69 | + path: PathBuf, |
| 70 | + source_type: SourceType, |
| 71 | + source_text: String, |
| 72 | +} |
| 73 | + |
| 74 | +impl RepoTestFile { |
| 75 | + fn new(path: PathBuf) -> Self { |
| 76 | + let source_type = SourceType::from_path(&path).expect("invalid source type"); |
| 77 | + let source_text = std::fs::read_to_string(&path).expect("Failed to read source file"); |
| 78 | + Self { path, source_type, source_text } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +fn bench_linter_real_world(criterion: &mut Criterion) { |
| 83 | + let mut group = criterion.benchmark_group("linter_real_world"); |
| 84 | + |
| 85 | + let calcom_path = std::env::var("CALCOM_PATH").expect("CALCOM_PATH env var not set"); |
| 86 | + |
| 87 | + // TODO: Enable walker to lint whole repository |
| 88 | + // let walker = ignore::WalkBuilder::new(&calcom_path) |
| 89 | + // .ignore(false) |
| 90 | + // .git_global(false) |
| 91 | + // .git_ignore(true) |
| 92 | + // .follow_links(true) |
| 93 | + // .hidden(false) |
| 94 | + // .require_git(false) |
| 95 | + // .build_parallel(); |
| 96 | + |
| 97 | + // Read source files once, outside of the benchmark |
| 98 | + let mut test_files = Vec::new(); |
| 99 | + // TODO: Add files from walker here. |
| 100 | + test_files.push(RepoTestFile::new( |
| 101 | + format!("{}{}", calcom_path, "/apps/web/components/PageWrapper.tsx").into(), |
| 102 | + )); |
| 103 | + test_files.push(RepoTestFile::new( |
| 104 | + format!("{}{}", calcom_path, "/apps/web/components/PageWrapperAppDir.tsx").into(), |
| 105 | + )); |
| 106 | + test_files.push(RepoTestFile::new( |
| 107 | + format!("{}{}", calcom_path, "/apps/web/components/EnterprisePage.tsx").into(), |
| 108 | + )); |
| 109 | + |
| 110 | + // Build the linter config once, outside of the benchmark |
| 111 | + let mut external_plugin_store = ExternalPluginStore::default(); |
| 112 | + let lint_config = ConfigStoreBuilder::all().build(&mut external_plugin_store).unwrap(); |
| 113 | + let linter = Linter::new( |
| 114 | + LintOptions::default(), |
| 115 | + ConfigStore::new(lint_config, FxHashMap::default(), external_plugin_store), |
| 116 | + None, |
| 117 | + ) |
| 118 | + .with_fix(FixKind::None); |
| 119 | + |
| 120 | + // Create allocator outside of bench_function so same allocator is used for |
| 121 | + // both the warmup and measurement phases |
| 122 | + let mut allocator = Allocator::default(); |
| 123 | + |
| 124 | + // Group all of the files together as a single benchmark for cal.com |
| 125 | + group.bench_function("calcom_repo", |b| { |
| 126 | + b.iter_with_setup_wrapper(|runner| { |
| 127 | + // Reset allocator at start of each iteration to reclaim memory |
| 128 | + allocator.reset(); |
| 129 | + |
| 130 | + // Step 1: Parse all files first |
| 131 | + let parser_results: Vec<_> = test_files |
| 132 | + .iter() |
| 133 | + .map(|test_file| { |
| 134 | + Parser::new(&allocator, &test_file.source_text, test_file.source_type).parse() |
| 135 | + }) |
| 136 | + .collect(); |
| 137 | + |
| 138 | + // Step 2: Build semantic analysis for each file |
| 139 | + let lint_inputs: Vec<_> = test_files |
| 140 | + .iter() |
| 141 | + .zip(parser_results.iter()) |
| 142 | + .map(|(test_file, parser_ret)| { |
| 143 | + let semantic_ret = SemanticBuilder::new() |
| 144 | + .with_scope_tree_child_ids(true) |
| 145 | + .with_cfg(true) |
| 146 | + .build(&parser_ret.program); |
| 147 | + let semantic = semantic_ret.semantic; |
| 148 | + let module_record = Arc::new(ModuleRecord::new( |
| 149 | + &test_file.path, |
| 150 | + &parser_ret.module_record, |
| 151 | + &semantic, |
| 152 | + )); |
| 153 | + (&test_file.path, semantic, module_record) |
| 154 | + }) |
| 155 | + .collect(); |
| 156 | + |
| 157 | + runner.run(|| { |
| 158 | + for (path, semantic, module_record) in lint_inputs { |
| 159 | + linter.run( |
| 160 | + path, |
| 161 | + vec![ContextSubHost::new(semantic, Arc::clone(&module_record), 0)], |
| 162 | + &allocator, |
| 163 | + ); |
| 164 | + } |
| 165 | + }); |
| 166 | + }); |
| 167 | + }); |
| 168 | +} |
| 169 | + |
| 170 | +criterion_group!(linter, bench_linter, bench_linter_real_world); |
65 | 171 | criterion_main!(linter); |
0 commit comments