mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:35 +00:00
Add Formatter benchmark (#4860)
This commit is contained in:
parent
8a3a269eef
commit
33434fcb9c
7 changed files with 76 additions and 10 deletions
|
@ -1,6 +1,6 @@
|
||||||
[alias]
|
[alias]
|
||||||
dev = "run --package ruff_dev --bin ruff_dev"
|
dev = "run --package ruff_dev --bin ruff_dev"
|
||||||
benchmark = "bench -p ruff_benchmark --"
|
benchmark = "bench -p ruff_benchmark --bench linter --bench formatter --"
|
||||||
|
|
||||||
[target.'cfg(all())']
|
[target.'cfg(all())']
|
||||||
rustflags = [
|
rustflags = [
|
||||||
|
|
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1796,6 +1796,7 @@ dependencies = [
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"ruff",
|
"ruff",
|
||||||
"ruff_python_ast",
|
"ruff_python_ast",
|
||||||
|
"ruff_python_formatter",
|
||||||
"rustpython-parser",
|
"rustpython-parser",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
|
|
@ -20,6 +20,10 @@ harness = false
|
||||||
name = "parser"
|
name = "parser"
|
||||||
harness = false
|
harness = false
|
||||||
|
|
||||||
|
[[bench]]
|
||||||
|
name = "formatter"
|
||||||
|
harness = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
once_cell.workspace = true
|
once_cell.workspace = true
|
||||||
serde.workspace = true
|
serde.workspace = true
|
||||||
|
@ -30,6 +34,7 @@ ureq = "2.6.2"
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
ruff.path = "../ruff"
|
ruff.path = "../ruff"
|
||||||
ruff_python_ast.path = "../ruff_python_ast"
|
ruff_python_ast.path = "../ruff_python_ast"
|
||||||
|
ruff_python_formatter = { path = "../ruff_python_formatter" }
|
||||||
criterion = { version = "0.5.1"}
|
criterion = { version = "0.5.1"}
|
||||||
rustpython-parser.workspace = true
|
rustpython-parser.workspace = true
|
||||||
|
|
||||||
|
@ -38,3 +43,4 @@ mimalloc = "0.1.34"
|
||||||
|
|
||||||
[target.'cfg(all(not(target_os = "windows"), not(target_os = "openbsd"), any(target_arch = "x86_64", target_arch = "aarch64", target_arch = "powerpc64")))'.dev-dependencies]
|
[target.'cfg(all(not(target_os = "windows"), not(target_os = "openbsd"), any(target_arch = "x86_64", target_arch = "aarch64", target_arch = "powerpc64")))'.dev-dependencies]
|
||||||
tikv-jemallocator = "0.5.0"
|
tikv-jemallocator = "0.5.0"
|
||||||
|
|
||||||
|
|
62
crates/ruff_benchmark/benches/formatter.rs
Normal file
62
crates/ruff_benchmark/benches/formatter.rs
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
|
||||||
|
use ruff_benchmark::{TestCase, TestCaseSpeed, TestFile, TestFileDownloadError};
|
||||||
|
use ruff_python_formatter::format_module;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
#[cfg(target_os = "windows")]
|
||||||
|
#[global_allocator]
|
||||||
|
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
||||||
|
|
||||||
|
#[cfg(all(
|
||||||
|
not(target_os = "windows"),
|
||||||
|
not(target_os = "openbsd"),
|
||||||
|
any(
|
||||||
|
target_arch = "x86_64",
|
||||||
|
target_arch = "aarch64",
|
||||||
|
target_arch = "powerpc64"
|
||||||
|
)
|
||||||
|
))]
|
||||||
|
#[global_allocator]
|
||||||
|
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
|
||||||
|
|
||||||
|
fn create_test_cases() -> Result<Vec<TestCase>, TestFileDownloadError> {
|
||||||
|
Ok(vec![
|
||||||
|
TestCase::fast(TestFile::try_download("numpy/globals.py", "https://raw.githubusercontent.com/numpy/numpy/89d64415e349ca75a25250f22b874aa16e5c0973/numpy/_globals.py")?),
|
||||||
|
TestCase::normal(TestFile::try_download(
|
||||||
|
"pydantic/types.py",
|
||||||
|
"https://raw.githubusercontent.com/pydantic/pydantic/83b3c49e99ceb4599d9286a3d793cea44ac36d4b/pydantic/types.py",
|
||||||
|
)?),
|
||||||
|
TestCase::normal(TestFile::try_download("numpy/ctypeslib.py", "https://raw.githubusercontent.com/numpy/numpy/e42c9503a14d66adfd41356ef5640c6975c45218/numpy/ctypeslib.py")?),
|
||||||
|
TestCase::slow(TestFile::try_download(
|
||||||
|
"large/dataset.py",
|
||||||
|
"https://raw.githubusercontent.com/DHI/mikeio/b7d26418f4db2909b0aa965253dbe83194d7bb5b/tests/test_dataset.py",
|
||||||
|
)?),
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
fn benchmark_formatter(criterion: &mut Criterion) {
|
||||||
|
let mut group = criterion.benchmark_group("formatter");
|
||||||
|
let test_cases = create_test_cases().unwrap();
|
||||||
|
|
||||||
|
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| {
|
||||||
|
b.iter(|| format_module(case.code()).expect("Formatting to succeed"));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
group.finish();
|
||||||
|
}
|
||||||
|
|
||||||
|
criterion_group!(formatter, benchmark_formatter);
|
||||||
|
criterion_main!(formatter);
|
|
@ -183,7 +183,6 @@ mod tests {
|
||||||
use ruff_python_ast::node::AnyNode;
|
use ruff_python_ast::node::AnyNode;
|
||||||
use ruff_text_size::{TextRange, TextSize};
|
use ruff_text_size::{TextRange, TextSize};
|
||||||
use rustpython_parser::ast::{StmtBreak, StmtContinue};
|
use rustpython_parser::ast::{StmtBreak, StmtContinue};
|
||||||
use std::cell::Cell;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn debug() {
|
fn debug() {
|
||||||
|
@ -210,7 +209,7 @@ break;
|
||||||
SourceComment {
|
SourceComment {
|
||||||
slice: source_code.slice(TextRange::at(TextSize::new(0), TextSize::new(17))),
|
slice: source_code.slice(TextRange::at(TextSize::new(0), TextSize::new(17))),
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
formatted: Cell::new(false),
|
formatted: std::cell::Cell::new(false),
|
||||||
position: CommentTextPosition::OwnLine,
|
position: CommentTextPosition::OwnLine,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
@ -220,7 +219,7 @@ break;
|
||||||
SourceComment {
|
SourceComment {
|
||||||
slice: source_code.slice(TextRange::at(TextSize::new(28), TextSize::new(10))),
|
slice: source_code.slice(TextRange::at(TextSize::new(28), TextSize::new(10))),
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
formatted: Cell::new(false),
|
formatted: std::cell::Cell::new(false),
|
||||||
position: CommentTextPosition::EndOfLine,
|
position: CommentTextPosition::EndOfLine,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
@ -230,7 +229,7 @@ break;
|
||||||
SourceComment {
|
SourceComment {
|
||||||
slice: source_code.slice(TextRange::at(TextSize::new(39), TextSize::new(15))),
|
slice: source_code.slice(TextRange::at(TextSize::new(39), TextSize::new(15))),
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
formatted: Cell::new(false),
|
formatted: std::cell::Cell::new(false),
|
||||||
position: CommentTextPosition::OwnLine,
|
position: CommentTextPosition::OwnLine,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
|
@ -88,7 +88,6 @@
|
||||||
//! It is possible to add an additional optional label to [`SourceComment`] If ever the need arises to distinguish two *dangling comments* in the formatting logic,
|
//! It is possible to add an additional optional label to [`SourceComment`] If ever the need arises to distinguish two *dangling comments* in the formatting logic,
|
||||||
|
|
||||||
use rustpython_parser::ast::Mod;
|
use rustpython_parser::ast::Mod;
|
||||||
use std::cell::Cell;
|
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
@ -119,7 +118,7 @@ pub(crate) struct SourceComment {
|
||||||
|
|
||||||
/// Whether the comment has been formatted or not.
|
/// Whether the comment has been formatted or not.
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
formatted: Cell<bool>,
|
formatted: std::cell::Cell<bool>,
|
||||||
|
|
||||||
position: CommentTextPosition,
|
position: CommentTextPosition,
|
||||||
}
|
}
|
||||||
|
@ -137,7 +136,7 @@ impl SourceComment {
|
||||||
|
|
||||||
#[cfg(not(debug_assertions))]
|
#[cfg(not(debug_assertions))]
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn mark_formatted(&self) {}
|
pub(crate) fn mark_formatted(&self) {}
|
||||||
|
|
||||||
/// Marks the comment as formatted
|
/// Marks the comment as formatted
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
|
|
|
@ -5,7 +5,6 @@ use ruff_formatter::{SourceCode, SourceCodeSlice};
|
||||||
use ruff_python_ast::node::AnyNodeRef;
|
use ruff_python_ast::node::AnyNodeRef;
|
||||||
use ruff_python_ast::prelude::*;
|
use ruff_python_ast::prelude::*;
|
||||||
use ruff_python_ast::source_code::{CommentRanges, Locator};
|
use ruff_python_ast::source_code::{CommentRanges, Locator};
|
||||||
use std::cell::Cell;
|
|
||||||
// The interface is designed to only export the members relevant for iterating nodes in
|
// The interface is designed to only export the members relevant for iterating nodes in
|
||||||
// pre-order.
|
// pre-order.
|
||||||
#[allow(clippy::wildcard_imports)]
|
#[allow(clippy::wildcard_imports)]
|
||||||
|
@ -418,7 +417,7 @@ impl From<DecoratedComment<'_>> for SourceComment {
|
||||||
slice: decorated.slice,
|
slice: decorated.slice,
|
||||||
position: decorated.text_position,
|
position: decorated.text_position,
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
formatted: Cell::new(false),
|
formatted: std::cell::Cell::new(false),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue