Skip to content

Commit

Permalink
impl compare command and benchmark command to rustc-perf-wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
s7tya committed Aug 9, 2024
1 parent 85ec341 commit 4713ef9
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 35 deletions.
2 changes: 2 additions & 0 deletions src/tools/rustc-perf-wrapper/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::fmt::{Display, Formatter};

pub const DB_PATH: &str = "./build/results.db";

#[derive(Clone, Copy, Debug, clap::ValueEnum)]
#[value(rename_all = "PascalCase")]
pub enum Profile {
Expand Down
111 changes: 76 additions & 35 deletions src/tools/rustc-perf-wrapper/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::io::{stdout, Write};
use std::path::PathBuf;
use std::process::Command;

use clap::Parser;
use config::DB_PATH;

use crate::config::{Profile, Scenario};

Expand All @@ -17,9 +19,6 @@ pub struct Args {
#[clap(subcommand)]
cmd: PerfCommand,

#[clap(flatten)]
opts: SharedOpts,

#[clap(flatten)]
ctx: BuildContext,
}
Expand All @@ -28,22 +27,36 @@ pub struct Args {
enum PerfCommand {
/// Run `profile_local eprintln`.
/// This executes the compiler on the given benchmarks and stores its stderr output.
Eprintln,
Eprintln {
#[clap(flatten)]
opts: SharedOpts,
},
/// Run `profile_local samply`
/// This executes the compiler on the given benchmarks and profiles it with `samply`.
/// You need to install `samply`, e.g. using `cargo install samply`.
Samply,
Samply {
#[clap(flatten)]
opts: SharedOpts,
},
/// Run `profile_local cachegrind`.
/// This executes the compiler on the given benchmarks under `Cachegrind`.
Cachegrind,
}

impl PerfCommand {
fn is_profiling(&self) -> bool {
match self {
PerfCommand::Eprintln | PerfCommand::Samply | PerfCommand::Cachegrind => true,
}
}
Cachegrind {
#[clap(flatten)]
opts: SharedOpts,
},
Benchmark {
id: String,

#[clap(flatten)]
opts: SharedOpts,
},
Compare {
/// The name of the base artifact to be compared.
base: String,

/// The name of the modified artifact to be compared.
modified: String,
},
}

#[derive(Debug, clap::Parser)]
Expand All @@ -52,6 +65,10 @@ struct SharedOpts {
/// If unspecified, all benchmarks will be executed.
#[clap(long, global = true, value_delimiter = ',')]
include: Vec<String>,

#[clap(long, global = true, value_delimiter = ',')]
exclude: Vec<String>,

/// Select the scenarios that should be benchmarked.
#[clap(
long,
Expand Down Expand Up @@ -88,34 +105,62 @@ fn main() {
fn run(args: Args) {
let mut cmd = Command::new(args.ctx.collector);
match &args.cmd {
PerfCommand::Eprintln => {
cmd.arg("profile_local").arg("eprintln");
PerfCommand::Eprintln { opts }
| PerfCommand::Samply { opts }
| PerfCommand::Cachegrind { opts } => {
cmd.arg("profile_local");
cmd.arg(match &args.cmd {
PerfCommand::Eprintln { .. } => "eprintln",
PerfCommand::Samply { .. } => "samply",
PerfCommand::Cachegrind { .. } => "cachegrind",
_ => unreachable!(),
});

cmd.arg("--out-dir").arg(&args.ctx.results_dir);

apply_shared_opts(&mut cmd, opts);
execute_benchmark(&mut cmd, &args.ctx.compiler);

println!("You can find the results at `{}`", args.ctx.results_dir.display());
}
PerfCommand::Samply => {
cmd.arg("profile_local").arg("samply");
PerfCommand::Benchmark { id, opts } => {
cmd.arg("bench_local");
cmd.arg("--db").arg(DB_PATH);
cmd.arg("--id").arg(id);

apply_shared_opts(&mut cmd, opts);
execute_benchmark(&mut cmd, &args.ctx.compiler);
}
PerfCommand::Cachegrind => {
cmd.arg("profile_local").arg("cachegrind");
PerfCommand::Compare { base, modified } => {
cmd.arg("bench_cmp");
cmd.arg("--db").arg(DB_PATH);
cmd.arg(base).arg(modified);

cmd.status().expect("error while running rustc-perf bench_cmp");
}
}
if args.cmd.is_profiling() {
cmd.arg("--out-dir").arg(&args.ctx.results_dir);
}
}

if !args.opts.include.is_empty() {
cmd.arg("--include").arg(args.opts.include.join(","));
fn apply_shared_opts(cmd: &mut Command, opts: &SharedOpts) {
if !opts.include.is_empty() {
cmd.arg("--include").arg(opts.include.join(","));
}
if !args.opts.profiles.is_empty() {
if !opts.exclude.is_empty() {
cmd.arg("--exclude").arg(opts.exclude.join(","));
}
if !opts.profiles.is_empty() {
cmd.arg("--profiles")
.arg(args.opts.profiles.iter().map(|p| p.to_string()).collect::<Vec<_>>().join(","));
.arg(opts.profiles.iter().map(|p| p.to_string()).collect::<Vec<_>>().join(","));
}
if !args.opts.scenarios.is_empty() {
if !opts.scenarios.is_empty() {
cmd.arg("--scenarios")
.arg(args.opts.scenarios.iter().map(|p| p.to_string()).collect::<Vec<_>>().join(","));
.arg(opts.scenarios.iter().map(|p| p.to_string()).collect::<Vec<_>>().join(","));
}
cmd.arg(&args.ctx.compiler);
}

println!("Running `rustc-perf` using `{}`", args.ctx.compiler.display());
fn execute_benchmark(cmd: &mut Command, compiler: &PathBuf) {
cmd.arg(compiler);
println!("Running `rustc-perf` using `{}`", compiler.display());

const MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR");

Expand All @@ -125,8 +170,4 @@ fn run(args: Args) {
// with compile-time benchmarks.
let cmd = cmd.current_dir(rustc_perf_dir);
cmd.status().expect("error while running rustc-perf collector");

if args.cmd.is_profiling() {
println!("You can find the results at `{}`", args.ctx.results_dir.display());
}
}

0 comments on commit 4713ef9

Please sign in to comment.