1 //! Fully integrated benchmarks for rust-analyzer, which load real cargo
4 //! The benchmark here is used to debug specific performance regressions. If you
5 //! notice that, eg, completion is slow in some specific case, you can modify
6 //! code here exercise this specific completion, and thus have a fast
7 //! edit/compile/test cycle.
9 //! Note that "Rust Analyzer: Run" action does not allow running a single test
10 //! in release mode in VS Code. There's however "Rust Analyzer: Copy Run Command Line"
11 //! which you can use to paste the command in terminal and add `--release` manually.
13 use std::{convert::TryFrom, sync::Arc};
15 use ide::{Change, CompletionConfig, FilePosition, TextSize};
16 use ide_db::helpers::{
17 insert_use::{ImportGranularity, InsertUseConfig},
20 use project_model::CargoConfig;
21 use test_utils::project_root;
22 use vfs::{AbsPathBuf, VfsPath};
24 use crate::cli::load_cargo::{load_workspace_at, LoadCargoConfig};
27 fn integrated_highlighting_benchmark() {
28 if std::env::var("RUN_SLOW_BENCHES").is_err() {
32 // Load rust-analyzer itself.
33 let workspace_to_load = project_root();
34 let file = "./crates/ide_db/src/apply_change.rs";
36 let cargo_config = CargoConfig::default();
37 let load_cargo_config = LoadCargoConfig {
38 load_out_dirs_from_check: true,
39 with_proc_macro: false,
40 prefill_caches: false,
43 let (mut host, vfs, _proc_macro) = {
44 let _it = stdx::timeit("workspace loading");
45 load_workspace_at(&workspace_to_load, &cargo_config, &load_cargo_config, &|_| {}).unwrap()
49 let file = workspace_to_load.join(file);
50 let path = VfsPath::from(AbsPathBuf::assert(file));
51 vfs.file_id(&path).unwrap_or_else(|| panic!("can't find virtual file for {}", path))
55 let _it = stdx::timeit("initial");
56 let analysis = host.analysis();
57 analysis.highlight_as_html(file_id, false).unwrap();
60 profile::init_from("*>100");
61 // let _s = profile::heartbeat_span();
64 let _it = stdx::timeit("change");
65 let mut text = host.analysis().file_text(file_id).unwrap().to_string();
66 text.push_str("\npub fn _dummy() {}\n");
67 let mut change = Change::new();
68 change.change_file(file_id, Some(Arc::new(text)));
69 host.apply_change(change);
73 let _it = stdx::timeit("after change");
74 let _span = profile::cpu_span();
75 let analysis = host.analysis();
76 analysis.highlight_as_html(file_id, false).unwrap();
81 fn integrated_completion_benchmark() {
82 if std::env::var("RUN_SLOW_BENCHES").is_err() {
86 // Load rust-analyzer itself.
87 let workspace_to_load = project_root();
88 let file = "./crates/hir/src/lib.rs";
90 let cargo_config = CargoConfig::default();
91 let load_cargo_config = LoadCargoConfig {
92 load_out_dirs_from_check: true,
93 with_proc_macro: false,
97 let (mut host, vfs, _proc_macro) = {
98 let _it = stdx::timeit("workspace loading");
99 load_workspace_at(&workspace_to_load, &cargo_config, &load_cargo_config, &|_| {}).unwrap()
103 let file = workspace_to_load.join(file);
104 let path = VfsPath::from(AbsPathBuf::assert(file));
105 vfs.file_id(&path).unwrap_or_else(|| panic!("can't find virtual file for {}", path))
109 let _it = stdx::timeit("initial");
110 let analysis = host.analysis();
111 analysis.highlight_as_html(file_id, false).unwrap();
114 profile::init_from("*>5");
115 // let _s = profile::heartbeat_span();
117 let completion_offset = {
118 let _it = stdx::timeit("change");
119 let mut text = host.analysis().file_text(file_id).unwrap().to_string();
120 let completion_offset =
121 patch(&mut text, "db.struct_data(self.id)", "sel;\ndb.struct_data(self.id)")
123 let mut change = Change::new();
124 change.change_file(file_id, Some(Arc::new(text)));
125 host.apply_change(change);
130 let _p = profile::span("unqualified path completion");
131 let _span = profile::cpu_span();
132 let analysis = host.analysis();
133 let config = CompletionConfig {
134 enable_postfix_completions: true,
135 enable_imports_on_the_fly: true,
136 enable_self_on_the_fly: true,
137 add_call_parenthesis: true,
138 add_call_argument_snippets: true,
139 snippet_cap: SnippetCap::new(true),
140 insert_use: InsertUseConfig {
141 granularity: ImportGranularity::Crate,
142 prefix_kind: hir::PrefixKind::ByCrate,
143 enforce_granularity: true,
145 skip_glob_imports: true,
149 FilePosition { file_id, offset: TextSize::try_from(completion_offset).unwrap() };
150 analysis.completions(&config, position).unwrap();
153 let completion_offset = {
154 let _it = stdx::timeit("change");
155 let mut text = host.analysis().file_text(file_id).unwrap().to_string();
156 let completion_offset =
157 patch(&mut text, "sel;\ndb.struct_data(self.id)", "self.;\ndb.struct_data(self.id)")
159 let mut change = Change::new();
160 change.change_file(file_id, Some(Arc::new(text)));
161 host.apply_change(change);
166 let _p = profile::span("dot completion");
167 let _span = profile::cpu_span();
168 let analysis = host.analysis();
169 let config = CompletionConfig {
170 enable_postfix_completions: true,
171 enable_imports_on_the_fly: true,
172 enable_self_on_the_fly: true,
173 add_call_parenthesis: true,
174 add_call_argument_snippets: true,
175 snippet_cap: SnippetCap::new(true),
176 insert_use: InsertUseConfig {
177 granularity: ImportGranularity::Crate,
178 prefix_kind: hir::PrefixKind::ByCrate,
179 enforce_granularity: true,
181 skip_glob_imports: true,
185 FilePosition { file_id, offset: TextSize::try_from(completion_offset).unwrap() };
186 analysis.completions(&config, position).unwrap();
190 fn patch(what: &mut String, from: &str, to: &str) -> usize {
191 let idx = what.find(from).unwrap();
192 *what = what.replacen(from, to, 1);