From 61f15b72ac52c23148038b3867198597b345e2f6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 9 Feb 2021 21:52:34 +0300 Subject: Add parsing benchmark --- crates/ide/src/syntax_highlighting/tests.rs | 37 +++++++++++++++++++++++++---- crates/syntax/src/tests.rs | 28 +++++++++++++++++++--- crates/test_utils/src/bench_fixture.rs | 9 +++++++ 3 files changed, 66 insertions(+), 8 deletions(-) (limited to 'crates') 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 @@ use expect_test::{expect_file, ExpectFile}; +use ide_db::SymbolKind; use test_utils::{bench, bench_fixture, skip_slow_tests}; -use crate::{fixture, FileRange, TextRange}; +use crate::{fixture, FileRange, HlTag, TextRange}; #[test] fn test_highlighting() { @@ -226,7 +227,7 @@ fn bar() { } #[test] -fn benchmark_syntax_highlighting() { +fn benchmark_syntax_highlighting_long_struct() { if skip_slow_tests() { return; } @@ -235,10 +236,36 @@ fn benchmark_syntax_highlighting() { let (analysis, file_id) = fixture::file(&fixture); let hash = { - let _pt = bench("syntax highlighting"); - analysis.highlight(file_id).unwrap().len() + let _pt = bench("syntax highlighting long struct"); + analysis + .highlight(file_id) + .unwrap() + .iter() + .filter(|it| it.highlight.tag == HlTag::Symbol(SymbolKind::Struct)) + .count() }; - assert_eq!(hash, 32009); + assert_eq!(hash, 2001); +} + +#[test] +fn benchmark_syntax_highlighting_parser() { + if skip_slow_tests() { + return; + } + + let fixture = bench_fixture::glorious_old_parser(); + let (analysis, file_id) = fixture::file(&fixture); + + let hash = { + let _pt = bench("syntax highlighting parser"); + analysis + .highlight(file_id) + .unwrap() + .iter() + .filter(|it| it.highlight.tag == HlTag::Symbol(SymbolKind::Function)) + .count() + }; + assert_eq!(hash, 1629); } #[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::{ path::{Path, PathBuf}, }; +use ast::NameOwner; use expect_test::expect_file; use rayon::prelude::*; -use test_utils::project_dir; +use test_utils::{bench, bench_fixture, project_dir, skip_slow_tests}; -use crate::{fuzz, tokenize, SourceFile, SyntaxError, TextRange, TextSize, Token}; +use crate::{ast, fuzz, tokenize, AstNode, SourceFile, SyntaxError, TextRange, TextSize, Token}; #[test] fn lexer_tests() { @@ -41,6 +42,28 @@ fn main() { assert!(parse.ok().is_ok()); } +#[test] +fn benchmark_parser() { + if skip_slow_tests() { + return; + } + let data = bench_fixture::glorious_old_parser(); + let tree = { + let _b = bench("parsing"); + let p = SourceFile::parse(&data); + assert!(p.errors.is_empty()); + assert_eq!(p.tree().syntax.text_range().len(), 352474.into()); + p.tree() + }; + + { + let _b = bench("tree traversal"); + let fn_names = + tree.syntax().descendants().filter_map(ast::Fn::cast).filter_map(|f| f.name()).count(); + assert_eq!(fn_names, 268); + } +} + #[test] fn parser_tests() { dir_tests(&test_data_dir(), &["parser/inline/ok", "parser/ok"], "rast", |text, path| { @@ -128,7 +151,6 @@ fn reparse_fuzz_tests() { } /// Test that Rust-analyzer can parse and validate the rust-analyzer -/// FIXME: Use this as a benchmark #[test] fn self_hosting_parsing() { 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 @@ //! Generates large snippets of Rust code for usage in the benchmarks. +use std::fs; + use stdx::format_to; +use crate::project_dir; + pub fn big_struct() -> String { let n = 1_000; @@ -26,3 +30,8 @@ struct S{} {{ buf } + +pub fn glorious_old_parser() -> String { + let path = project_dir().join("bench_data/glorious_old_parser"); + fs::read_to_string(&path).unwrap() +} -- cgit v1.2.3