aboutsummaryrefslogtreecommitdiff
path: root/crates/rust-analyzer/src/integrated_benchmarks.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-05-04 12:02:23 +0100
committerAleksey Kladov <[email protected]>2021-05-04 13:14:33 +0100
commitb2f040fc9ab841e0791254bcdbf82bd0ee771242 (patch)
tree2d51f573ecb9ef4df970347eb38fda88e2766603 /crates/rust-analyzer/src/integrated_benchmarks.rs
parent41f8ae7daa6e6bab6d73807cd0803b8061f586a0 (diff)
internal: add integrated completion benchmark
Diffstat (limited to 'crates/rust-analyzer/src/integrated_benchmarks.rs')
-rw-r--r--crates/rust-analyzer/src/integrated_benchmarks.rs186
1 files changed, 186 insertions, 0 deletions
diff --git a/crates/rust-analyzer/src/integrated_benchmarks.rs b/crates/rust-analyzer/src/integrated_benchmarks.rs
new file mode 100644
index 000000000..411446b04
--- /dev/null
+++ b/crates/rust-analyzer/src/integrated_benchmarks.rs
@@ -0,0 +1,186 @@
1//! Fully integrated benchmarks for rust-analyzer, which load real cargo
2//! projects.
3//!
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.
8//!
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.
12
13use std::{convert::TryFrom, sync::Arc};
14
15use ide::{Change, CompletionConfig, FilePosition, TextSize};
16use ide_db::helpers::{insert_use::InsertUseConfig, merge_imports::MergeBehavior, SnippetCap};
17use test_utils::project_root;
18use vfs::{AbsPathBuf, VfsPath};
19
20use crate::cli::load_cargo::{load_workspace_at, LoadCargoConfig};
21
22#[test]
23fn integrated_highlighting_benchmark() {
24 // Don't run slow benchmark by default
25 if true {
26 return;
27 }
28
29 // Load rust-analyzer itself.
30 let workspace_to_load = project_root();
31 let file = "./crates/ide_db/src/apply_change.rs";
32
33 let cargo_config = Default::default();
34 let load_cargo_config = LoadCargoConfig {
35 load_out_dirs_from_check: true,
36 wrap_rustc: false,
37 with_proc_macro: false,
38 };
39
40 let (mut host, vfs, _proc_macro) = {
41 let _it = stdx::timeit("workspace loading");
42 load_workspace_at(&workspace_to_load, &cargo_config, &load_cargo_config, &|_| {}).unwrap()
43 };
44
45 let file_id = {
46 let file = workspace_to_load.join(file);
47 let path = VfsPath::from(AbsPathBuf::assert(file));
48 vfs.file_id(&path).unwrap_or_else(|| panic!("can't find virtual file for {}", path))
49 };
50
51 {
52 let _it = stdx::timeit("initial");
53 let analysis = host.analysis();
54 analysis.highlight_as_html(file_id, false).unwrap();
55 }
56
57 profile::init_from("*>100");
58 // let _s = profile::heartbeat_span();
59
60 {
61 let _it = stdx::timeit("change");
62 let mut text = host.analysis().file_text(file_id).unwrap().to_string();
63 text.push_str("\npub fn _dummy() {}\n");
64 let mut change = Change::new();
65 change.change_file(file_id, Some(Arc::new(text)));
66 host.apply_change(change);
67 }
68
69 {
70 let _it = stdx::timeit("after change");
71 let _span = profile::cpu_span();
72 let analysis = host.analysis();
73 analysis.highlight_as_html(file_id, false).unwrap();
74 }
75}
76
77#[test]
78fn integrated_completion_benchmark() {
79 // Don't run slow benchmark by default
80 if true {
81 return;
82 }
83
84 // Load rust-analyzer itself.
85 let workspace_to_load = project_root();
86 let file = "./crates/hir/src/lib.rs";
87
88 let cargo_config = Default::default();
89 let load_cargo_config = LoadCargoConfig {
90 load_out_dirs_from_check: true,
91 wrap_rustc: false,
92 with_proc_macro: false,
93 };
94
95 let (mut host, vfs, _proc_macro) = {
96 let _it = stdx::timeit("workspace loading");
97 load_workspace_at(&workspace_to_load, &cargo_config, &load_cargo_config, &|_| {}).unwrap()
98 };
99
100 let file_id = {
101 let file = workspace_to_load.join(file);
102 let path = VfsPath::from(AbsPathBuf::assert(file));
103 vfs.file_id(&path).unwrap_or_else(|| panic!("can't find virtual file for {}", path))
104 };
105
106 {
107 let _it = stdx::timeit("initial");
108 let analysis = host.analysis();
109 analysis.highlight_as_html(file_id, false).unwrap();
110 }
111
112 profile::init_from("*>5");
113 // let _s = profile::heartbeat_span();
114
115 let completion_offset = {
116 let _it = stdx::timeit("change");
117 let mut text = host.analysis().file_text(file_id).unwrap().to_string();
118 let completion_offset =
119 patch(&mut text, "db.struct_data(self.id)", "sel;\ndb.struct_data(self.id)")
120 + "sel".len();
121 let mut change = Change::new();
122 change.change_file(file_id, Some(Arc::new(text)));
123 host.apply_change(change);
124 completion_offset
125 };
126
127 {
128 let _it = stdx::timeit("unqualified path completion");
129 let _span = profile::cpu_span();
130 let analysis = host.analysis();
131 let config = CompletionConfig {
132 enable_postfix_completions: true,
133 enable_imports_on_the_fly: true,
134 add_call_parenthesis: true,
135 add_call_argument_snippets: true,
136 snippet_cap: SnippetCap::new(true),
137 insert_use: InsertUseConfig {
138 merge: Some(MergeBehavior::Full),
139 prefix_kind: hir::PrefixKind::ByCrate,
140 group: true,
141 },
142 };
143 let position =
144 FilePosition { file_id, offset: TextSize::try_from(completion_offset).unwrap() };
145 analysis.completions(&config, position).unwrap();
146 }
147
148 let completion_offset = {
149 let _it = stdx::timeit("change");
150 let mut text = host.analysis().file_text(file_id).unwrap().to_string();
151 let completion_offset =
152 patch(&mut text, "sel;\ndb.struct_data(self.id)", "self.;\ndb.struct_data(self.id)")
153 + "self.".len();
154 let mut change = Change::new();
155 change.change_file(file_id, Some(Arc::new(text)));
156 host.apply_change(change);
157 completion_offset
158 };
159
160 {
161 let _it = stdx::timeit("dot completion");
162 let _span = profile::cpu_span();
163 let analysis = host.analysis();
164 let config = CompletionConfig {
165 enable_postfix_completions: true,
166 enable_imports_on_the_fly: true,
167 add_call_parenthesis: true,
168 add_call_argument_snippets: true,
169 snippet_cap: SnippetCap::new(true),
170 insert_use: InsertUseConfig {
171 merge: Some(MergeBehavior::Full),
172 prefix_kind: hir::PrefixKind::ByCrate,
173 group: true,
174 },
175 };
176 let position =
177 FilePosition { file_id, offset: TextSize::try_from(completion_offset).unwrap() };
178 analysis.completions(&config, position).unwrap();
179 }
180}
181
182fn patch(what: &mut String, from: &str, to: &str) -> usize {
183 let idx = what.find(from).unwrap();
184 *what = what.replacen(from, to, 1);
185 idx
186}