diff options
Diffstat (limited to 'crates/ra_cli/src/analysis_bench.rs')
-rw-r--r-- | crates/ra_cli/src/analysis_bench.rs | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/crates/ra_cli/src/analysis_bench.rs b/crates/ra_cli/src/analysis_bench.rs new file mode 100644 index 000000000..33d472838 --- /dev/null +++ b/crates/ra_cli/src/analysis_bench.rs | |||
@@ -0,0 +1,92 @@ | |||
1 | use std::{ | ||
2 | path::{PathBuf, Path}, | ||
3 | time::Instant, | ||
4 | }; | ||
5 | |||
6 | use ra_db::{SourceDatabase, salsa::Database}; | ||
7 | use ra_ide_api::{AnalysisHost, Analysis, LineCol, FilePosition}; | ||
8 | |||
9 | use crate::Result; | ||
10 | |||
11 | pub(crate) enum Op { | ||
12 | Highlight { path: PathBuf }, | ||
13 | Complete { path: PathBuf, line: u32, column: u32 }, | ||
14 | } | ||
15 | |||
16 | pub(crate) fn run(verbose: bool, path: &Path, op: Op) -> Result<()> { | ||
17 | let start = Instant::now(); | ||
18 | eprint!("loading: "); | ||
19 | let (host, roots) = ra_batch::load_cargo(path)?; | ||
20 | let db = host.raw_database(); | ||
21 | eprintln!("{:?}\n", start.elapsed()); | ||
22 | |||
23 | let file_id = { | ||
24 | let path = match &op { | ||
25 | Op::Highlight { path } => path, | ||
26 | Op::Complete { path, .. } => path, | ||
27 | }; | ||
28 | let path = std::env::current_dir()?.join(path).canonicalize()?; | ||
29 | roots | ||
30 | .iter() | ||
31 | .find_map(|(source_root_id, project_root)| { | ||
32 | if project_root.is_member() { | ||
33 | for (rel_path, file_id) in &db.source_root(*source_root_id).files { | ||
34 | let abs_path = rel_path.to_path(project_root.path()); | ||
35 | if abs_path == path { | ||
36 | return Some(*file_id); | ||
37 | } | ||
38 | } | ||
39 | } | ||
40 | None | ||
41 | }) | ||
42 | .ok_or_else(|| format!("Can't find {:?}", path))? | ||
43 | }; | ||
44 | |||
45 | match op { | ||
46 | Op::Highlight { .. } => { | ||
47 | let res = do_work(&host, |analysis| { | ||
48 | analysis.diagnostics(file_id).unwrap(); | ||
49 | analysis.highlight_as_html(file_id, false).unwrap() | ||
50 | }); | ||
51 | if verbose { | ||
52 | println!("\n{}", res); | ||
53 | } | ||
54 | } | ||
55 | Op::Complete { line, column, .. } => { | ||
56 | let offset = host | ||
57 | .analysis() | ||
58 | .file_line_index(file_id) | ||
59 | .offset(LineCol { line, col_utf16: column }); | ||
60 | let file_postion = FilePosition { file_id, offset }; | ||
61 | |||
62 | let res = do_work(&host, |analysis| analysis.completions(file_postion)); | ||
63 | if verbose { | ||
64 | println!("\n{:#?}", res); | ||
65 | } | ||
66 | } | ||
67 | } | ||
68 | Ok(()) | ||
69 | } | ||
70 | |||
71 | fn do_work<F: Fn(&Analysis) -> T, T>(host: &AnalysisHost, work: F) -> T { | ||
72 | { | ||
73 | let start = Instant::now(); | ||
74 | eprint!("from scratch: "); | ||
75 | work(&host.analysis()); | ||
76 | eprintln!("{:?}", start.elapsed()); | ||
77 | } | ||
78 | { | ||
79 | let start = Instant::now(); | ||
80 | eprint!("no change: "); | ||
81 | work(&host.analysis()); | ||
82 | eprintln!("{:?}", start.elapsed()); | ||
83 | } | ||
84 | { | ||
85 | let start = Instant::now(); | ||
86 | eprint!("trivial change: "); | ||
87 | host.raw_database().salsa_runtime().next_revision(); | ||
88 | let res = work(&host.analysis()); | ||
89 | eprintln!("{:?}", start.elapsed()); | ||
90 | res | ||
91 | } | ||
92 | } | ||