diff options
author | Aleksey Kladov <[email protected]> | 2021-02-09 18:52:34 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2021-02-09 18:52:34 +0000 |
commit | 61f15b72ac52c23148038b3867198597b345e2f6 (patch) | |
tree | 0935428e6e79a42da638a5c983ec526c64f10abb /crates | |
parent | 4b1279d0b160d98c1429ca1a52b37aa7a0af5775 (diff) |
Add parsing benchmark
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ide/src/syntax_highlighting/tests.rs | 37 | ||||
-rw-r--r-- | crates/syntax/src/tests.rs | 28 | ||||
-rw-r--r-- | crates/test_utils/src/bench_fixture.rs | 9 |
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 @@ | |||
1 | use expect_test::{expect_file, ExpectFile}; | 1 | use expect_test::{expect_file, ExpectFile}; |
2 | use ide_db::SymbolKind; | ||
2 | use test_utils::{bench, bench_fixture, skip_slow_tests}; | 3 | use test_utils::{bench, bench_fixture, skip_slow_tests}; |
3 | 4 | ||
4 | use crate::{fixture, FileRange, TextRange}; | 5 | use crate::{fixture, FileRange, HlTag, TextRange}; |
5 | 6 | ||
6 | #[test] | 7 | #[test] |
7 | fn test_highlighting() { | 8 | fn test_highlighting() { |
@@ -226,7 +227,7 @@ fn bar() { | |||
226 | } | 227 | } |
227 | 228 | ||
228 | #[test] | 229 | #[test] |
229 | fn benchmark_syntax_highlighting() { | 230 | fn 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] | ||
251 | fn 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 | ||
7 | use ast::NameOwner; | ||
7 | use expect_test::expect_file; | 8 | use expect_test::expect_file; |
8 | use rayon::prelude::*; | 9 | use rayon::prelude::*; |
9 | use test_utils::project_dir; | 10 | use test_utils::{bench, bench_fixture, project_dir, skip_slow_tests}; |
10 | 11 | ||
11 | use crate::{fuzz, tokenize, SourceFile, SyntaxError, TextRange, TextSize, Token}; | 12 | use crate::{ast, fuzz, tokenize, AstNode, SourceFile, SyntaxError, TextRange, TextSize, Token}; |
12 | 13 | ||
13 | #[test] | 14 | #[test] |
14 | fn lexer_tests() { | 15 | fn lexer_tests() { |
@@ -42,6 +43,28 @@ fn main() { | |||
42 | } | 43 | } |
43 | 44 | ||
44 | #[test] | 45 | #[test] |
46 | fn 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] | ||
45 | fn parser_tests() { | 68 | fn 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] |
133 | fn self_hosting_parsing() { | 155 | fn 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 | ||
3 | use std::fs; | ||
4 | |||
3 | use stdx::format_to; | 5 | use stdx::format_to; |
4 | 6 | ||
7 | use crate::project_dir; | ||
8 | |||
5 | pub fn big_struct() -> String { | 9 | pub 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 | |||
34 | pub fn glorious_old_parser() -> String { | ||
35 | let path = project_dir().join("bench_data/glorious_old_parser"); | ||
36 | fs::read_to_string(&path).unwrap() | ||
37 | } | ||