From af3c2ad0f9649a6dc9916056d600ace24b373fd8 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Wed, 8 May 2024 11:35:11 +0100 Subject: [PATCH] feat(bench): add CSS analyzer to the benchmarks --- Cargo.lock | 1 + xtask/bench/Cargo.toml | 7 ++- xtask/bench/benches/analyzer-libs-css.txt | 8 +++ xtask/bench/benches/css_analyzer.rs | 60 +++++++++++++++++++ .../benches/{analyzer.rs => js_analyzer.rs} | 6 +- xtask/bench/src/language.rs | 18 +++++- 6 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 xtask/bench/benches/analyzer-libs-css.txt create mode 100644 xtask/bench/benches/css_analyzer.rs rename xtask/bench/benches/{analyzer.rs => js_analyzer.rs} (93%) diff --git a/Cargo.lock b/Cargo.lock index 09432f138f32..0aaad6675322 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4038,6 +4038,7 @@ version = "0.0.0" dependencies = [ "ansi_rgb", "biome_analyze", + "biome_css_analyze", "biome_css_formatter", "biome_css_parser", "biome_css_syntax", diff --git a/xtask/bench/Cargo.toml b/xtask/bench/Cargo.toml index d2dbf940c293..086becc6279f 100644 --- a/xtask/bench/Cargo.toml +++ b/xtask/bench/Cargo.toml @@ -6,6 +6,7 @@ version = "0.0.0" [dependencies] biome_analyze = { workspace = true } +biome_css_analyze = { workspace = true } biome_css_formatter = { workspace = true } biome_css_parser = { workspace = true } biome_css_syntax = { workspace = true } @@ -38,7 +39,7 @@ codspeed = ["codspeed-criterion-compat"] [[bench]] harness = false -name = "analyzer" +name = "js_analyzer" # JavaScript benches [[bench]] @@ -67,5 +68,9 @@ name = "css_parser" harness = false name = "css_formatter" +[[bench]] +harness = false +name = "css_analyzer" + [lints] workspace = true diff --git a/xtask/bench/benches/analyzer-libs-css.txt b/xtask/bench/benches/analyzer-libs-css.txt new file mode 100644 index 000000000000..251d0d80d43f --- /dev/null +++ b/xtask/bench/benches/analyzer-libs-css.txt @@ -0,0 +1,8 @@ +https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.css +https://cdn.jsdelivr.net/npm/daisyui@3.9.2/dist/full.css +https://cdn.jsdelivr.net/npm/purecss@3.0.0/build/pure.css +https://raw.githubusercontent.com/Dogfalo/materialize/v1-dev/dist/css/materialize.css +https://raw.githubusercontent.com/jgthms/bulma/master/css/bulma.css +https://raw.githubusercontent.com/foundation/foundation-sites/develop/dist/css/foundation.css +https://raw.githubusercontent.com/Semantic-Org/Semantic-UI/master/dist/semantic.css +https://raw.githubusercontent.com/tachyons-css/tachyons/main/css/tachyons.css \ No newline at end of file diff --git a/xtask/bench/benches/css_analyzer.rs b/xtask/bench/benches/css_analyzer.rs new file mode 100644 index 000000000000..b26b3775b621 --- /dev/null +++ b/xtask/bench/benches/css_analyzer.rs @@ -0,0 +1,60 @@ +use std::collections::HashMap; +use xtask_bench::{criterion_group, criterion_main, BenchmarkId, Criterion}; +use xtask_bench::{Parse, TestCase}; +#[cfg(target_os = "windows")] +#[global_allocator] +static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; + +#[cfg(all( + any(target_os = "macos", target_os = "linux"), + not(target_env = "musl"), +))] +#[global_allocator] +static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; + +// Jemallocator does not work on aarch64 with musl, so we'll use the system allocator instead +#[cfg(all(target_env = "musl", target_os = "linux", target_arch = "aarch64"))] +#[global_allocator] +static GLOBAL: std::alloc::System = std::alloc::System; +fn bench_analyzer(criterion: &mut Criterion) { + let mut all_suites = HashMap::new(); + all_suites.insert("css", include_str!("analyzer-libs-css.txt")); + let mut libs = vec![]; + libs.extend(all_suites.values().flat_map(|suite| suite.lines())); + + let mut group = criterion.benchmark_group("css_analyzer"); + + for lib in libs { + let test_case = TestCase::try_from(lib); + + match test_case { + Ok(test_case) => { + let code = test_case.code(); + group.throughput(criterion::Throughput::Bytes(code.len() as u64)); + group.bench_with_input( + BenchmarkId::from_parameter(test_case.filename()), + code, + |b, _| { + let parse = Parse::try_from_case(&test_case).expect("Supported language"); + + let parsed = parse.parse(); + + match parsed.analyze() { + None => {} + Some(analyze) => b.iter(|| { + analyze.analyze(); + criterion::black_box(()); + }), + } + }, + ); + } + Err(e) => println!("{:?}", e), + } + } + + group.finish(); +} + +criterion_group!(css_analyzer, bench_analyzer); +criterion_main!(css_analyzer); diff --git a/xtask/bench/benches/analyzer.rs b/xtask/bench/benches/js_analyzer.rs similarity index 93% rename from xtask/bench/benches/analyzer.rs rename to xtask/bench/benches/js_analyzer.rs index ac5043421ea5..52cbb498f43a 100644 --- a/xtask/bench/benches/analyzer.rs +++ b/xtask/bench/benches/js_analyzer.rs @@ -23,7 +23,7 @@ fn bench_analyzer(criterion: &mut Criterion) { let mut libs = vec![]; libs.extend(all_suites.values().flat_map(|suite| suite.lines())); - let mut group = criterion.benchmark_group("analyzer"); + let mut group = criterion.benchmark_group("js_analyzer"); for lib in libs { let test_case = TestCase::try_from(lib); @@ -57,5 +57,5 @@ fn bench_analyzer(criterion: &mut Criterion) { group.finish(); } -criterion_group!(analyzer, bench_analyzer); -criterion_main!(analyzer); +criterion_group!(js_analyzer, bench_analyzer); +criterion_main!(js_analyzer); diff --git a/xtask/bench/src/language.rs b/xtask/bench/src/language.rs index 14ba2ec29e51..7cf0ce39412f 100644 --- a/xtask/bench/src/language.rs +++ b/xtask/bench/src/language.rs @@ -2,9 +2,8 @@ use crate::test_case::TestCase; use biome_analyze::{AnalysisFilter, AnalyzerOptions, ControlFlow, Never, RuleCategories}; use biome_css_formatter::context::{CssFormatContext, CssFormatOptions}; use biome_css_parser::CssParserOptions; -use biome_css_syntax::CssSyntaxNode; +use biome_css_syntax::{CssRoot, CssSyntaxNode}; use biome_formatter::{FormatResult, Formatted, PrintResult, Printed}; -use biome_js_analyze::analyze; use biome_js_formatter::context::{JsFormatContext, JsFormatOptions}; use biome_js_parser::JsParserOptions; use biome_js_syntax::{AnyJsRoot, JsFileSource, JsSyntaxNode}; @@ -150,6 +149,7 @@ impl FormattedNode { pub enum Analyze { JavaScript(AnyJsRoot), + Css(CssRoot), } impl Analyze { @@ -161,7 +161,7 @@ impl Analyze { ..AnalysisFilter::default() }; let options = AnalyzerOptions::default(); - analyze( + biome_js_analyze::analyze( root, filter, &options, @@ -174,6 +174,18 @@ impl Analyze { }, ); } + Analyze::Css(root) => { + let filter = AnalysisFilter { + categories: RuleCategories::SYNTAX | RuleCategories::LINT, + ..AnalysisFilter::default() + }; + let options = AnalyzerOptions::default(); + biome_css_analyze::analyze(root, filter, &options, |event| { + black_box(event.diagnostic()); + black_box(event.actions()); + ControlFlow::::Continue(()) + }); + } } } }