Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bench): add CSS analyzer to the benchmarks #2762

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion xtask/bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -38,7 +39,7 @@ codspeed = ["codspeed-criterion-compat"]

[[bench]]
harness = false
name = "analyzer"
name = "js_analyzer"

# JavaScript benches
[[bench]]
Expand Down Expand Up @@ -67,5 +68,9 @@ name = "css_parser"
harness = false
name = "css_formatter"

[[bench]]
harness = false
name = "css_analyzer"

[lints]
workspace = true
8 changes: 8 additions & 0 deletions xtask/bench/benches/analyzer-libs-css.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.css
https://cdn.jsdelivr.net/npm/[email protected]/dist/full.css
https://cdn.jsdelivr.net/npm/[email protected]/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
60 changes: 60 additions & 0 deletions xtask/bench/benches/css_analyzer.rs
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
18 changes: 15 additions & 3 deletions xtask/bench/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -150,6 +149,7 @@ impl FormattedNode {

pub enum Analyze {
JavaScript(AnyJsRoot),
Css(CssRoot),
}

impl Analyze {
Expand All @@ -161,7 +161,7 @@ impl Analyze {
..AnalysisFilter::default()
};
let options = AnalyzerOptions::default();
analyze(
biome_js_analyze::analyze(
root,
filter,
&options,
Expand All @@ -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::<Never>::Continue(())
});
}
}
}
}
Loading