Skip to content

Commit

Permalink
Remove lex and parsing from formatter benchmark (#6547)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser authored Aug 14, 2023
1 parent 1a9536c commit 51ae47a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
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.

1 change: 1 addition & 0 deletions crates/ruff_benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ ureq = "2.6.2"
ruff.path = "../ruff"
ruff_python_ast.path = "../ruff_python_ast"
ruff_python_formatter = { path = "../ruff_python_formatter" }
ruff_python_index = { path = "../ruff_python_index" }
ruff_python_parser = { path = "../ruff_python_parser" }
criterion = { version = "0.5.1"}

Expand Down
38 changes: 28 additions & 10 deletions crates/ruff_benchmark/benches/formatter.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use ruff_benchmark::{TestCase, TestCaseSpeed, TestFile, TestFileDownloadError};
use ruff_python_formatter::{format_module, PyFormatOptions};
use std::path::Path;
use std::time::Duration;

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};

use ruff_benchmark::{TestCase, TestFile, TestFileDownloadError};
use ruff_python_formatter::{format_node, PyFormatOptions};
use ruff_python_index::CommentRangesBuilder;
use ruff_python_parser::lexer::lex;
use ruff_python_parser::{parse_tokens, Mode};

#[cfg(target_os = "windows")]
#[global_allocator]
Expand Down Expand Up @@ -41,19 +45,33 @@ fn benchmark_formatter(criterion: &mut Criterion) {

for case in test_cases {
group.throughput(Throughput::Bytes(case.code().len() as u64));
group.measurement_time(match case.speed() {
TestCaseSpeed::Fast => Duration::from_secs(5),
TestCaseSpeed::Normal => Duration::from_secs(10),
TestCaseSpeed::Slow => Duration::from_secs(20),
});

group.bench_with_input(
BenchmarkId::from_parameter(case.name()),
&case,
|b, case| {
let mut tokens = Vec::new();
let mut comment_ranges = CommentRangesBuilder::default();

for result in lex(case.code(), Mode::Module) {
let (token, range) = result.expect("Input to be a valid python program.");

comment_ranges.visit_token(&token, range);
tokens.push(Ok((token, range)));
}

let comment_ranges = comment_ranges.finish();

// Parse the AST.
let python_ast = parse_tokens(tokens, Mode::Module, "<filename>")
.expect("Input to be a valid python program");

b.iter(|| {
let options = PyFormatOptions::from_extension(Path::new(case.name()));
format_module(case.code(), options).expect("Formatting to succeed")
let formatted = format_node(&python_ast, &comment_ranges, case.code(), options)
.expect("Formatting to succeed");

formatted.print().expect("Printing to succeed")
});
},
);
Expand Down

0 comments on commit 51ae47a

Please sign in to comment.