Skip to content

Commit

Permalink
feat(html): set up html parser and formatter benchmarks (#5155)
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc3 authored Feb 21, 2025
1 parent e21f6d0 commit 177f005
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

12 changes: 12 additions & 0 deletions xtask/bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ biome_graphql_formatter = { workspace = true }
biome_graphql_parser = { workspace = true }
biome_graphql_syntax = { workspace = true }
biome_grit_patterns = { workspace = true }
biome_html_formatter = { workspace = true }
biome_html_parser = { workspace = true }
biome_html_syntax = { workspace = true }
biome_js_analyze = { workspace = true }
biome_js_formatter = { workspace = true }
biome_js_parser = { workspace = true }
Expand Down Expand Up @@ -91,5 +94,14 @@ name = "graphql_formatter"
harness = false
name = "gritql_search"

# HTML benches
[[bench]]
harness = false
name = "html_parser"

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

[lints]
workspace = true
41 changes: 41 additions & 0 deletions xtask/bench/benches/html_formatter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::collections::HashMap;
use xtask_bench::{bench_formatter_group, TestCase};
use xtask_bench::{criterion_group, criterion_main, Criterion};

#[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;

#[cfg(all(target_env = "musl", target_os = "linux", target_arch = "aarch64"))]
#[global_allocator]
static GLOBAL: std::alloc::System = std::alloc::System;

fn bench_html_formatter(criterion: &mut Criterion) {
let mut all_suites = HashMap::new();
all_suites.insert("html", include_str!("libs-html.txt"));
let mut libs = vec![];
libs.extend(all_suites.values().flat_map(|suite| suite.lines()));

let mut group = criterion.benchmark_group("html_formatter");
for lib in libs {
let test_case = TestCase::try_from(lib);

match test_case {
Ok(test_case) => {
bench_formatter_group(&mut group, test_case);
}
Err(e) => println!("{e:?}"),
}
}
group.finish();
}

criterion_group!(html_formatter, bench_html_formatter);
criterion_main!(html_formatter);
41 changes: 41 additions & 0 deletions xtask/bench/benches/html_parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::collections::HashMap;
use xtask_bench::{bench_parser_group, TestCase};
use xtask_bench::{criterion_group, criterion_main, Criterion};

#[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;

#[cfg(all(target_env = "musl", target_os = "linux", target_arch = "aarch64"))]
#[global_allocator]
static GLOBAL: std::alloc::System = std::alloc::System;

fn bench_html_parser(criterion: &mut Criterion) {
let mut all_suites = HashMap::new();
all_suites.insert("html", include_str!("libs-html.txt"));
let mut libs = vec![];
libs.extend(all_suites.values().flat_map(|suite| suite.lines()));

let mut group = criterion.benchmark_group("html_parser");
for lib in libs {
let test_case = TestCase::try_from(lib);

match test_case {
Ok(test_case) => {
bench_parser_group(&mut group, test_case);
}
Err(e) => println!("{e:?}"),
}
}
group.finish();
}

criterion_group!(html_parser, bench_html_parser);
criterion_main!(html_parser);
1 change: 1 addition & 0 deletions xtask/bench/benches/libs-html.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://raw.githubusercontent.com/dyc3/opentogethertube/e6b44c716289a8babb2f70624e452542e45c2a1b/client/index.html
20 changes: 20 additions & 0 deletions xtask/bench/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ use biome_css_syntax::{CssRoot, CssSyntaxNode};
use biome_formatter::{FormatResult, Formatted, PrintResult, Printed};
use biome_graphql_formatter::context::{GraphqlFormatContext, GraphqlFormatOptions};
use biome_graphql_syntax::GraphqlSyntaxNode;
use biome_html_formatter::context::HtmlFormatContext;
use biome_html_formatter::HtmlFormatOptions;
use biome_html_syntax::HtmlSyntaxNode;
use biome_js_formatter::context::{JsFormatContext, JsFormatOptions};
use biome_js_parser::JsParserOptions;
use biome_js_syntax::{AnyJsRoot, JsFileSource, JsSyntaxNode};
Expand All @@ -25,6 +28,7 @@ pub enum Parse<'a> {
Json(&'a str),
Css(&'a str),
Graphql(&'a str),
Html(&'a str),
}

impl Parse<'_> {
Expand All @@ -35,6 +39,7 @@ impl Parse<'_> {
"json" => Some(Parse::Json(case.code())),
"css" => Some(Parse::Css(case.code())),
"graphql" => Some(Parse::Graphql(case.code())),
"html" => Some(Parse::Html(case.code())),
_ => None,
},
}
Expand All @@ -57,6 +62,7 @@ impl Parse<'_> {
.allow_css_modules(),
)),
Parse::Graphql(code) => Parsed::Graphql(biome_graphql_parser::parse_graphql(code)),
Parse::Html(code) => Parsed::Html(biome_html_parser::parse_html(code)),
}
}

Expand Down Expand Up @@ -86,6 +92,9 @@ impl Parse<'_> {
Parse::Graphql(code) => {
Parsed::Graphql(biome_graphql_parser::parse_graphql_with_cache(code, cache))
}
Parse::Html(code) => {
Parsed::Html(biome_html_parser::parse_html_with_cache(code, cache))
}
}
}
}
Expand All @@ -95,6 +104,7 @@ pub enum Parsed {
Json(biome_json_parser::JsonParse),
Css(biome_css_parser::CssParse),
Graphql(biome_graphql_parser::GraphqlParse),
Html(biome_html_parser::HtmlParse),
}

impl Parsed {
Expand All @@ -106,6 +116,7 @@ impl Parsed {
Parsed::Json(parse) => Some(FormatNode::Json(parse.syntax())),
Parsed::Css(parse) => Some(FormatNode::Css(parse.syntax())),
Parsed::Graphql(parse) => Some(FormatNode::Graphql(parse.syntax())),
Parsed::Html(parse) => Some(FormatNode::Html(parse.syntax())),
}
}

Expand All @@ -115,6 +126,7 @@ impl Parsed {
Parsed::Json(_) => None,
Parsed::Graphql(_) => None,
Parsed::Css(parse) => Some(Analyze::Css(parse.tree())),
Parsed::Html(_) => None,
}
}

Expand All @@ -124,6 +136,7 @@ impl Parsed {
Parsed::Json(parse) => parse.into_diagnostics(),
Parsed::Css(parse) => parse.into_diagnostics(),
Parsed::Graphql(parse) => parse.into_diagnostics(),
Parsed::Html(parse) => parse.into_diagnostics(),
}
}
}
Expand All @@ -133,6 +146,7 @@ pub enum FormatNode {
Json(JsonSyntaxNode),
Css(CssSyntaxNode),
Graphql(GraphqlSyntaxNode),
Html(HtmlSyntaxNode),
}

impl FormatNode {
Expand All @@ -152,6 +166,10 @@ impl FormatNode {
biome_graphql_formatter::format_node(GraphqlFormatOptions::default(), root)
.map(FormattedNode::Graphql)
}
FormatNode::Html(root) => {
biome_html_formatter::format_node(HtmlFormatOptions::default(), root)
.map(FormattedNode::Html)
}
}
}
}
Expand All @@ -161,6 +179,7 @@ pub enum FormattedNode {
Json(Formatted<JsonFormatContext>),
Css(Formatted<CssFormatContext>),
Graphql(Formatted<GraphqlFormatContext>),
Html(Formatted<HtmlFormatContext>),
}

impl FormattedNode {
Expand All @@ -170,6 +189,7 @@ impl FormattedNode {
FormattedNode::Json(formatted) => formatted.print(),
FormattedNode::Css(formatted) => formatted.print(),
FormattedNode::Graphql(formatted) => formatted.print(),
FormattedNode::Html(formatted) => formatted.print(),
}
}
}
Expand Down

0 comments on commit 177f005

Please sign in to comment.