Skip to content

Commit

Permalink
Add cargo dev repeat for profiling (#5144)
Browse files Browse the repository at this point in the history
## Summary

This adds a new subcommand that can be used as

```shell
cargo build --bin ruff_dev --profile=release-debug
perf record -g -F 999 target/release-debug/ruff_dev repeat --repeat 30 --exit-zero --no-cache path/to/cpython > /dev/null
flamegraph --perfdata perf.data
```

## Test Plan

This is a ruff internal script. I successfully used it to profile
cpython with the instructions above
  • Loading branch information
konstin authored Jun 19, 2023
1 parent be11cae commit 361d45f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ github_search*.jsonl
schemastore
.venv*
scratch.py
perf.data
perf.data.old
flamegraph.svg
# Additional target directories that don't invalidate the main compile cache when changing linker settings
/target*

###
# Rust.gitignore
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub enum Command {
},
}

#[derive(Debug, clap::Args)]
#[derive(Clone, Debug, clap::Args)]
#[allow(clippy::struct_excessive_bools, clippy::module_name_repetitions)]
pub struct CheckArgs {
/// List of files or directories to check.
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ fn format(files: &[PathBuf]) -> Result<ExitStatus> {
Ok(ExitStatus::Success)
}

fn check(args: CheckArgs, log_level: LogLevel) -> Result<ExitStatus> {
pub fn check(args: CheckArgs, log_level: LogLevel) -> Result<ExitStatus> {
let (cli, overrides) = args.partition();

// Construct the "default" settings. These are used when no `pyproject.toml`
Expand Down
24 changes: 24 additions & 0 deletions crates/ruff_dev/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
use anyhow::Result;
use clap::{Parser, Subcommand};
use ruff::logging::{set_up_logging, LogLevel};
use ruff_cli::check;

mod generate_all;
mod generate_cli_help;
Expand All @@ -27,6 +29,7 @@ struct Args {
}

#[derive(Subcommand)]
#[allow(clippy::large_enum_variant)]
enum Command {
/// Run all code and documentation generation steps.
GenerateAll(generate_all::Args),
Expand All @@ -48,6 +51,16 @@ enum Command {
PrintTokens(print_tokens::Args),
/// Run round-trip source code generation on a given Python file.
RoundTrip(round_trip::Args),
/// Run a ruff command n times for profiling/benchmarking
Repeat {
#[clap(flatten)]
args: ruff_cli::args::CheckArgs,
#[clap(flatten)]
log_level_args: ruff_cli::args::LogLevelArgs,
/// Run this many times
#[clap(long, short = 'n')]
repeat: usize,
},
}

fn main() -> Result<()> {
Expand All @@ -64,6 +77,17 @@ fn main() -> Result<()> {
Command::PrintCST(args) => print_cst::main(args)?,
Command::PrintTokens(args) => print_tokens::main(args)?,
Command::RoundTrip(args) => round_trip::main(args)?,
Command::Repeat {
args,
repeat,
log_level_args,
} => {
let log_level = LogLevel::from(log_level_args);
set_up_logging(&log_level)?;
for _ in 0..*repeat {
check(args.clone(), log_level)?;
}
}
}
Ok(())
}

0 comments on commit 361d45f

Please sign in to comment.