diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 378ea0fa6d..e6f1d46588 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -616,7 +616,7 @@ jobs: - uses: Swatinem/rust-cache@v2 - name: "Build benchmarks" - run: cargo codspeed build -p ruff_benchmark + run: cargo codspeed build --features codspeed -p ruff_benchmark - name: "Run benchmarks" uses: CodSpeedHQ/action@v3 diff --git a/Cargo.lock b/Cargo.lock index ce8c0dd58c..993a50c97f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2081,6 +2081,7 @@ name = "ruff_benchmark" version = "0.0.0" dependencies = [ "codspeed-criterion-compat", + "criterion", "mimalloc", "once_cell", "red_knot_workspace", diff --git a/Cargo.toml b/Cargo.toml index b01eeab669..90962228df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,6 +58,7 @@ console_error_panic_hook = { version = "0.1.7" } console_log = { version = "1.0.0" } countme = { version = "3.0.1" } compact_str = "0.8.0" +criterion = { version = "0.5.1", default-features = false } crossbeam = { version = "0.8.4" } dashmap = { version = "6.0.1" } drop_bomb = { version = "0.1.5" } @@ -156,7 +157,7 @@ zip = { version = "0.6.6", default-features = false, features = ["zstd"] } [workspace.lints.rust] unsafe_code = "warn" unreachable_pub = "warn" -unexpected_cfgs = { level = "warn", check-cfg = ['cfg(fuzzing)'] } +unexpected_cfgs = { level = "warn", check-cfg = ["cfg(fuzzing)", "cfg(codspeed)"] } [workspace.lints.clippy] pedantic = { level = "warn", priority = -2 } diff --git a/crates/ruff_benchmark/Cargo.toml b/crates/ruff_benchmark/Cargo.toml index 5322a1cf5e..cf4e5dbaec 100644 --- a/crates/ruff_benchmark/Cargo.toml +++ b/crates/ruff_benchmark/Cargo.toml @@ -42,7 +42,8 @@ serde = { workspace = true } serde_json = { workspace = true } url = { workspace = true } ureq = { workspace = true } -codspeed-criterion-compat = { workspace = true, default-features = false } +criterion = { workspace = true, default-features = false } +codspeed-criterion-compat = { workspace = true, default-features = false, optional = true } [dev-dependencies] ruff_db = { workspace = true } @@ -56,6 +57,9 @@ red_knot_workspace = { workspace = true } [lints] workspace = true +[features] +codspeed = ["codspeed-criterion-compat"] + [target.'cfg(target_os = "windows")'.dev-dependencies] mimalloc = { workspace = true } diff --git a/crates/ruff_benchmark/benches/formatter.rs b/crates/ruff_benchmark/benches/formatter.rs index 740fcf1fa3..af2b1caa76 100644 --- a/crates/ruff_benchmark/benches/formatter.rs +++ b/crates/ruff_benchmark/benches/formatter.rs @@ -1,9 +1,8 @@ use std::path::Path; -use codspeed_criterion_compat::{ +use ruff_benchmark::criterion::{ criterion_group, criterion_main, BenchmarkId, Criterion, Throughput, }; - use ruff_benchmark::{TestCase, TestFile, TestFileDownloadError}; use ruff_python_formatter::{format_module_ast, PreviewMode, PyFormatOptions}; use ruff_python_parser::{parse, Mode}; diff --git a/crates/ruff_benchmark/benches/lexer.rs b/crates/ruff_benchmark/benches/lexer.rs index 6e8488a552..64b68a7a35 100644 --- a/crates/ruff_benchmark/benches/lexer.rs +++ b/crates/ruff_benchmark/benches/lexer.rs @@ -1,7 +1,6 @@ -use codspeed_criterion_compat::{ +use ruff_benchmark::criterion::{ criterion_group, criterion_main, measurement::WallTime, BenchmarkId, Criterion, Throughput, }; - use ruff_benchmark::{TestCase, TestFile, TestFileDownloadError}; use ruff_python_parser::{lexer, Mode, TokenKind}; diff --git a/crates/ruff_benchmark/benches/linter.rs b/crates/ruff_benchmark/benches/linter.rs index 9da2437ced..dc27674ade 100644 --- a/crates/ruff_benchmark/benches/linter.rs +++ b/crates/ruff_benchmark/benches/linter.rs @@ -1,8 +1,6 @@ -use codspeed_criterion_compat::{ - self as criterion, criterion_group, criterion_main, BenchmarkGroup, BenchmarkId, Criterion, - Throughput, +use ruff_benchmark::criterion::{ + criterion_group, criterion_main, BenchmarkGroup, BenchmarkId, Criterion, Throughput, }; -use criterion::measurement; use ruff_benchmark::{TestCase, TestFile, TestFileDownloadError}; use ruff_linter::linter::{lint_only, ParseSource}; use ruff_linter::rule_selector::PreviewOptions; @@ -46,7 +44,7 @@ fn create_test_cases() -> Result, TestFileDownloadError> { ]) } -fn benchmark_linter(mut group: BenchmarkGroup, settings: &LinterSettings) { +fn benchmark_linter(mut group: BenchmarkGroup, settings: &LinterSettings) { let test_cases = create_test_cases().unwrap(); for case in test_cases { diff --git a/crates/ruff_benchmark/benches/parser.rs b/crates/ruff_benchmark/benches/parser.rs index f12526d072..ec2fa671c1 100644 --- a/crates/ruff_benchmark/benches/parser.rs +++ b/crates/ruff_benchmark/benches/parser.rs @@ -1,7 +1,6 @@ -use codspeed_criterion_compat::{ +use ruff_benchmark::criterion::{ criterion_group, criterion_main, measurement::WallTime, BenchmarkId, Criterion, Throughput, }; - use ruff_benchmark::{TestCase, TestFile, TestFileDownloadError}; use ruff_python_ast::statement_visitor::{walk_stmt, StatementVisitor}; use ruff_python_ast::Stmt; diff --git a/crates/ruff_benchmark/benches/red_knot.rs b/crates/ruff_benchmark/benches/red_knot.rs index 2fca43a8f2..4d1382420e 100644 --- a/crates/ruff_benchmark/benches/red_knot.rs +++ b/crates/ruff_benchmark/benches/red_knot.rs @@ -1,9 +1,8 @@ #![allow(clippy::disallowed_names)] -use codspeed_criterion_compat::{criterion_group, criterion_main, BatchSize, Criterion}; - use red_knot_workspace::db::RootDatabase; use red_knot_workspace::workspace::WorkspaceMetadata; +use ruff_benchmark::criterion::{criterion_group, criterion_main, BatchSize, Criterion}; use ruff_benchmark::TestFile; use ruff_db::files::{system_path_to_file, File}; use ruff_db::program::{ProgramSettings, SearchPathSettings, TargetVersion}; diff --git a/crates/ruff_benchmark/src/criterion.rs b/crates/ruff_benchmark/src/criterion.rs new file mode 100644 index 0000000000..528375f42c --- /dev/null +++ b/crates/ruff_benchmark/src/criterion.rs @@ -0,0 +1,13 @@ +//! This module re-exports the criterion API but picks the right backend depending on whether +//! the benchmarks are built to run locally or with codspeed. +//! The compat layer is required because codspeed doesn't support all platforms. +//! See [#12662](https://github.com/astral-sh/ruff/issues/12662) + +#[cfg(not(codspeed))] +pub use criterion::*; + +#[cfg(not(codspeed))] +pub type BenchmarkGroup<'a> = criterion::BenchmarkGroup<'a, measurement::WallTime>; + +#[cfg(codspeed)] +pub use codspeed_criterion_compat::*; diff --git a/crates/ruff_benchmark/src/lib.rs b/crates/ruff_benchmark/src/lib.rs index cf770f3a4f..5fcf31bf7e 100644 --- a/crates/ruff_benchmark/src/lib.rs +++ b/crates/ruff_benchmark/src/lib.rs @@ -1,3 +1,5 @@ +pub mod criterion; + use std::fmt::{Display, Formatter}; use std::path::PathBuf; use std::process::Command;