Skip to content

Commit

Permalink
Implement --perf flag to lintcheck for benchmarking
Browse files Browse the repository at this point in the history
Turns out I was completely overcomplicating myself,
there was no need for an external tool such as becnhv2
or even the original becnh, we already had the benchmarking
infrastructure right under our noses!

This PR implements a new **lintcheck** option called
--perf, using it as a flag will mean that lintcheck
builds Clippy as a release package and hooks perf to it.

The realization that lintcheck is already 90% of what
a benchmarking tool needs came to me in a dream.
  • Loading branch information
blyxyas committed Jan 30, 2025
1 parent ad05bc0 commit d49b1ff
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
5 changes: 5 additions & 0 deletions lintcheck/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ pub(crate) struct LintcheckConfig {
/// Run clippy on the dependencies of crates specified in crates-toml
#[clap(long, conflicts_with("max_jobs"))]
pub recursive: bool,
/// Also produce a `perf.data` file, implies --jobs=1,
/// the `perf.data` file can be found at
/// `target/lintcheck/sources/<package>-<version>/perf.data`
#[clap(long)]
pub perf: bool,
#[command(subcommand)]
pub subcommand: Option<Commands>,
}
Expand Down
41 changes: 35 additions & 6 deletions lintcheck/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,25 @@ impl Crate {

clippy_args.extend(lint_levels_args.iter().map(String::as_str));

let mut cmd = Command::new("cargo");
let mut cmd;

if config.perf {
cmd = Command::new("perf");
cmd.args(&[
"record",
"-e",
"instructions", // Only count instructions
"-g", // Enable call-graph, useful for flamegraphs and produces richer reports
"--quiet", // Do not tamper with lintcheck's normal output
"-o",
"perf.data",
"--",
"cargo",
]);
} else {
cmd = Command::new("cargo");
}

cmd.arg(if config.fix { "fix" } else { "check" })
.arg("--quiet")
.current_dir(&self.path)
Expand Down Expand Up @@ -234,9 +252,15 @@ fn normalize_diag(
}

/// Builds clippy inside the repo to make sure we have a clippy executable we can use.
fn build_clippy() -> String {
fn build_clippy(release_build: bool) -> String {
let output = Command::new("cargo")
.args(["run", "--bin=clippy-driver", "--", "--version"])
.args([
"run",
"--bin=clippy-driver",
if release_build { "-r" } else { "" },
"--",
"--version",
])
.stderr(Stdio::inherit())
.output()
.unwrap();
Expand Down Expand Up @@ -270,13 +294,18 @@ fn main() {

#[allow(clippy::too_many_lines)]
fn lintcheck(config: LintcheckConfig) {
let clippy_ver = build_clippy();
let clippy_driver_path = fs::canonicalize(format!("target/debug/clippy-driver{EXE_SUFFIX}")).unwrap();
let clippy_ver = build_clippy(config.perf);
let clippy_driver_path = fs::canonicalize(format!(
"target/{}/clippy-driver{EXE_SUFFIX}",
if config.perf { "release" } else { "debug" }
))
.unwrap();

// assert that clippy is found
assert!(
clippy_driver_path.is_file(),
"target/debug/clippy-driver binary not found! {}",
"target/{}/clippy-driver binary not found! {}",
if config.perf { "release" } else { "debug" },
clippy_driver_path.display()
);

Expand Down

0 comments on commit d49b1ff

Please sign in to comment.