aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-02-09 18:53:27 +0000
committerGitHub <[email protected]>2021-02-09 18:53:27 +0000
commit4f9a5287bfea124b76571424ce3eb4a91aec337a (patch)
treecfe365b8382bfae9f8d0bfc9ebc9c35cc6e58307 /crates
parent96a9ab725093b5f6501ed086973906ebb77805ff (diff)
parent61f15b72ac52c23148038b3867198597b345e2f6 (diff)
Merge #7615
7615: Add parsing benchmark r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/ide/src/syntax_highlighting/tests.rs37
-rw-r--r--crates/syntax/src/tests.rs28
-rw-r--r--crates/test_utils/src/bench_fixture.rs9
3 files changed, 66 insertions, 8 deletions
diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs
index 4a1229a31..9d0cd1af5 100644
--- a/crates/ide/src/syntax_highlighting/tests.rs
+++ b/crates/ide/src/syntax_highlighting/tests.rs
@@ -1,7 +1,8 @@
1use expect_test::{expect_file, ExpectFile}; 1use expect_test::{expect_file, ExpectFile};
2use ide_db::SymbolKind;
2use test_utils::{bench, bench_fixture, skip_slow_tests}; 3use test_utils::{bench, bench_fixture, skip_slow_tests};
3 4
4use crate::{fixture, FileRange, TextRange}; 5use crate::{fixture, FileRange, HlTag, TextRange};
5 6
6#[test] 7#[test]
7fn test_highlighting() { 8fn test_highlighting() {
@@ -226,7 +227,7 @@ fn bar() {
226} 227}
227 228
228#[test] 229#[test]
229fn benchmark_syntax_highlighting() { 230fn benchmark_syntax_highlighting_long_struct() {
230 if skip_slow_tests() { 231 if skip_slow_tests() {
231 return; 232 return;
232 } 233 }
@@ -235,10 +236,36 @@ fn benchmark_syntax_highlighting() {
235 let (analysis, file_id) = fixture::file(&fixture); 236 let (analysis, file_id) = fixture::file(&fixture);
236 237
237 let hash = { 238 let hash = {
238 let _pt = bench("syntax highlighting"); 239 let _pt = bench("syntax highlighting long struct");
239 analysis.highlight(file_id).unwrap().len() 240 analysis
241 .highlight(file_id)
242 .unwrap()
243 .iter()
244 .filter(|it| it.highlight.tag == HlTag::Symbol(SymbolKind::Struct))
245 .count()
240 }; 246 };
241 assert_eq!(hash, 32009); 247 assert_eq!(hash, 2001);
248}
249
250#[test]
251fn benchmark_syntax_highlighting_parser() {
252 if skip_slow_tests() {
253 return;
254 }
255
256 let fixture = bench_fixture::glorious_old_parser();
257 let (analysis, file_id) = fixture::file(&fixture);
258
259 let hash = {
260 let _pt = bench("syntax highlighting parser");
261 analysis
262 .highlight(file_id)
263 .unwrap()
264 .iter()
265 .filter(|it| it.highlight.tag == HlTag::Symbol(SymbolKind::Function))
266 .count()
267 };
268 assert_eq!(hash, 1629);
242} 269}
243 270
244#[test] 271#[test]
diff --git a/crates/syntax/src/tests.rs b/crates/syntax/src/tests.rs
index 9d3433c9d..b2c06e24f 100644
--- a/crates/syntax/src/tests.rs
+++ b/crates/syntax/src/tests.rs
@@ -4,11 +4,12 @@ use std::{
4 path::{Path, PathBuf}, 4 path::{Path, PathBuf},
5}; 5};
6 6
7use ast::NameOwner;
7use expect_test::expect_file; 8use expect_test::expect_file;
8use rayon::prelude::*; 9use rayon::prelude::*;
9use test_utils::project_dir; 10use test_utils::{bench, bench_fixture, project_dir, skip_slow_tests};
10 11
11use crate::{fuzz, tokenize, SourceFile, SyntaxError, TextRange, TextSize, Token}; 12use crate::{ast, fuzz, tokenize, AstNode, SourceFile, SyntaxError, TextRange, TextSize, Token};
12 13
13#[test] 14#[test]
14fn lexer_tests() { 15fn lexer_tests() {
@@ -42,6 +43,28 @@ fn main() {
42} 43}
43 44
44#[test] 45#[test]
46fn benchmark_parser() {
47 if skip_slow_tests() {
48 return;
49 }
50 let data = bench_fixture::glorious_old_parser();
51 let tree = {
52 let _b = bench("parsing");
53 let p = SourceFile::parse(&data);
54 assert!(p.errors.is_empty());
55 assert_eq!(p.tree().syntax.text_range().len(), 352474.into());
56 p.tree()
57 };
58
59 {
60 let _b = bench("tree traversal");
61 let fn_names =
62 tree.syntax().descendants().filter_map(ast::Fn::cast).filter_map(|f| f.name()).count();
63 assert_eq!(fn_names, 268);
64 }
65}
66
67#[test]
45fn parser_tests() { 68fn parser_tests() {
46 dir_tests(&test_data_dir(), &["parser/inline/ok", "parser/ok"], "rast", |text, path| { 69 dir_tests(&test_data_dir(), &["parser/inline/ok", "parser/ok"], "rast", |text, path| {
47 let parse = SourceFile::parse(text); 70 let parse = SourceFile::parse(text);
@@ -128,7 +151,6 @@ fn reparse_fuzz_tests() {
128} 151}
129 152
130/// Test that Rust-analyzer can parse and validate the rust-analyzer 153/// Test that Rust-analyzer can parse and validate the rust-analyzer
131/// FIXME: Use this as a benchmark
132#[test] 154#[test]
133fn self_hosting_parsing() { 155fn self_hosting_parsing() {
134 let dir = project_dir().join("crates"); 156 let dir = project_dir().join("crates");
diff --git a/crates/test_utils/src/bench_fixture.rs b/crates/test_utils/src/bench_fixture.rs
index 41fcca635..aa1bea9bb 100644
--- a/crates/test_utils/src/bench_fixture.rs
+++ b/crates/test_utils/src/bench_fixture.rs
@@ -1,7 +1,11 @@
1//! Generates large snippets of Rust code for usage in the benchmarks. 1//! Generates large snippets of Rust code for usage in the benchmarks.
2 2
3use std::fs;
4
3use stdx::format_to; 5use stdx::format_to;
4 6
7use crate::project_dir;
8
5pub fn big_struct() -> String { 9pub fn big_struct() -> String {
6 let n = 1_000; 10 let n = 1_000;
7 11
@@ -26,3 +30,8 @@ struct S{} {{
26 30
27 buf 31 buf
28} 32}
33
34pub fn glorious_old_parser() -> String {
35 let path = project_dir().join("bench_data/glorious_old_parser");
36 fs::read_to_string(&path).unwrap()
37}