Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

ci: formatter benchmarks #2029

Merged
merged 8 commits into from
Feb 1, 2022
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
3 changes: 2 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
lint = "clippy --workspace --all-targets --verbose -- --deny warnings"
format = "fmt --all --verbose"
codegen = "run -p xtask_codegen --"
benchmark = "run -p xtask_bench --release --"
bench_parser = "run -p xtask_bench --release -- --feature parser"
bench_formatter = "run -p xtask_bench --release -- --feature formatter"
coverage = "run -p xtask_coverage --release --"
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Parser benchmark, compares main and PR branch with Criterion.
# Comment with text containing `!bench`, a new result will be commented at the bottom of this PR.

name: Parser Benchmark
name: Formatter Benchmark

on:
issue_comment:
Expand All @@ -14,13 +14,13 @@ env:
jobs:
bench:
name: Bench
if: github.event.issue.pull_request && contains(github.event.comment.body, '!bench')
if: github.event.issue.pull_request && contains(github.event.comment.body, '!bench_formatter')
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
os: [ubuntu-latest]

steps:
- name: Checkout PR Branch
Expand All @@ -30,7 +30,7 @@ jobs:

- name: Support longpaths
run: git config core.longpaths true

- name: Checkout PR Branch
uses: actions/checkout@v2

Expand All @@ -50,13 +50,13 @@ jobs:
run: cargo build --release --locked -p xtask_bench

- name: Run Bench on PR Branch
run: cargo benchmark --save-baseline pr
run: cargo bench_formatter --save-baseline pr

- name: Checkout Main Branch
run: git checkout main

- name: Run Bench on Main Branch
run: cargo benchmark --save-baseline main
run: cargo bench_formatter --save-baseline main

- name: Compare Bench Results on ${{ matrix.os }}
id: bench_comparison
Expand Down
85 changes: 85 additions & 0 deletions .github/workflows/bench_parser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Parser benchmark, compares main and PR branch with Criterion.
# Comment with text containing `!bench`, a new result will be commented at the bottom of this PR.

name: Parser Benchmark

on:
issue_comment:
types: [created]

env:
RUST_LOG: info
RUST_BACKTRACE: 1

jobs:
bench:
name: Bench
if: github.event.issue.pull_request && contains(github.event.comment.body, '!bench_parser')
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]

steps:
- name: Checkout PR Branch
uses: actions/checkout@v2
with:
submodules: false

- name: Support longpaths
run: git config core.longpaths true

- name: Checkout PR Branch
uses: actions/checkout@v2

- name: Fetch Main Branch
run: git fetch --no-tags --prune --no-recurse-submodules --depth=1 origin main

- name: Install toolchain
run: rustup show

- name: Cache
uses: Swatinem/rust-cache@v1

- name: Install critcmp
run: cargo install critcmp

- name: Compile
run: cargo build --release --locked -p xtask_bench

- name: Run Bench on PR Branch
run: cargo bench_parser --save-baseline pr

- name: Checkout Main Branch
run: git checkout main

- name: Run Bench on Main Branch
run: cargo bench_parser --save-baseline main

- name: Compare Bench Results on ${{ matrix.os }}
id: bench_comparison
shell: bash
run: |
echo "### Bench results on ${{ matrix.os }} of the parser" > output
echo "\`\`\`" >> output
critcmp main pr >> output
echo "\`\`\`" >> output
cat output
comment="$(cat output)"
comment="${comment//'%'/'%25'}"
comment="${comment//$'\n'/'%0A'}"
comment="${comment//$'\r'/'%0D'}"
echo "::set-output name=comment::$comment"

- name: Write a new comment
uses: peter-evans/[email protected]
continue-on-error: true
with:
issue-number: ${{ github.event.issue.number }}
body: |
${{ steps.bench_comparison.outputs.comment }}

- name: Remove Criterion Artifact
run: rm -rf ./target/criterion
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ Here are some other scripts that you might find useful.
If you are a core contributor, and you have access to create new branches
from the main repository (not a fork), use these comments to run specific workflows:

- `!bench` benchmarks the parser's runtime performance and writes a comment with the results
- `!bench_parser` benchmarks the parser's runtime performance and writes a comment with the results;
- `!bench_formatter` benchmarks the formatter runtime performance and writes a comment with the results;

#### Naming patterns

Expand Down
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.

Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ impl ToFormatElement for JsAnyLiteralExpression {
JsAnyLiteralExpression::JsNullLiteralExpression(null_literal) => {
null_literal.to_format_element(formatter)
}
JsAnyLiteralExpression::JsRegexLiteralExpression(_) => todo!(),
JsAnyLiteralExpression::JsRegexLiteralExpression(node) => {
node.to_format_element(formatter)
}
}
}
}
1 change: 1 addition & 0 deletions crates/rome_formatter/src/ts/expressions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod function_expression;
mod identifier_expression;
mod literal_expression;
mod object_expression;
mod regex_literal;
mod sequence_expression;
mod static_member_expression;
mod super_expression;
Expand Down
8 changes: 8 additions & 0 deletions crates/rome_formatter/src/ts/expressions/regex_literal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::{FormatElement, FormatResult, Formatter, ToFormatElement};
use rslint_parser::ast::JsRegexLiteralExpression;

impl ToFormatElement for JsRegexLiteralExpression {
fn to_format_element(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
formatter.format_token(&self.value_token()?)
}
}
1 change: 1 addition & 0 deletions xtask/bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ publish = false
xtask = { path = '../', version = "0.0" }
rslint_parser = { path = "../../crates/rslint_parser", version = "0.3" }
rslint_errors = { path = "../../crates/rslint_errors", version = "0.2.0" }
rome_formatter = { path = "../../crates/rome_formatter", version = "0.0.0" }

pico-args = "0.3.4"
timing = "0.2.3"
Expand Down
22 changes: 18 additions & 4 deletions xtask/bench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

This crate contains benchmark suites for the project.

Criterion is used to generate benchmark results.

## Parser Benchmark

Criterion is used to generate benchmark results,
you can use the following instruction to get nice benchmark comparison.
To get a benchmark comparison, you need to run the benchmark for `main` branch and your PR:

```bash
# (commit your code on pr branch, run)
git checkout main
cargo benchmark --save-baseline main
cargo bench_parser --save-baseline main
git checkout -
cargo benchmark --save-baseline pr
cargo bench_parser --save-baseline pr
critcmp main pr # (cargo install critcmp)
```

Expand All @@ -36,6 +37,19 @@ parser/vue.global.prod.js 1.09 28.7±6.39ms 4.2 MB/sec 1
The 1.xx column is the percentage difference, larger means worse.
For example jquery is 16% slower on main. And the pr branch performs better overall.

## Formatter benchmark

To get a benchmark comparison, you need to run the benchmark for `main` branch and your PR:

```bash
# (commit your code on pr branch, run)
git checkout main
cargo bench_formatter --save-baseline main
git checkout -
cargo bench_formatter --save-baseline pr
critcmp main pr # (cargo install critcmp)
```

## Heap Profiling using `dhat`

```bash
Expand Down
45 changes: 45 additions & 0 deletions xtask/bench/src/features/formatter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use crate::BenchmarkSummary;
use rome_formatter::{format, FormatOptions, Formatted};
use rslint_parser::SyntaxNode;
use std::fmt::{Display, Formatter};
use std::time::Duration;

#[derive(Debug, Clone)]
pub struct FormatterMeasurement {
id: String,
formatting: Duration,
}
pub fn benchmark_format_lib(id: &str, root: &SyntaxNode) -> BenchmarkSummary {
let formatter_timer = timing::start();
run_format(root);
let formatter_duration = formatter_timer.stop();

BenchmarkSummary::Formatter(FormatterMeasurement {
id: id.to_string(),
formatting: formatter_duration,
})
}

pub fn run_format(root: &SyntaxNode) -> Formatted {
format(FormatOptions::default(), root).unwrap()
}

impl FormatterMeasurement {
fn total(&self) -> Duration {
self.formatting
}

pub(crate) fn summary(&self) -> String {
format!("{}, Formatting: {:?}", self.id, self.total(),)
}
}

impl Display for FormatterMeasurement {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let _ = writeln!(f, "\tFormatting: {:>10?}", self.formatting);
let _ = writeln!(f, "\t ----------");
let _ = writeln!(f, "\tTotal: {:>10?}", self.total());

Ok(())
}
}
2 changes: 2 additions & 0 deletions xtask/bench/src/features/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod formatter;
pub mod parser;
Loading