Skip to content

Commit 481d7ba

Browse files
authored
Merge pull request #1692 from Kobzol/runtime-group-filter
Add `--group` flag to runtime benchmark/profiling command
2 parents 0635724 + e20c433 commit 481d7ba

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

collector/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ by a selected version of rustc. You can run it using the following command:
183183
The following options alter the behaviour of the `bench_runtime_local` subcommand.
184184
- `--no-isolate`: you can use this flag to make repeated local benchmarks faster. It will cause the
185185
`collector` to reuse compiled artifacts of the runtime benchmark groups.
186+
- `--group`: Compile only the selected runtime benchmark group (i.e. only compile a crate inside the
187+
directory `collector/runtime-benchmarks/<group>`). This can be used to speed up local runtime benchmark
188+
experiments. Even with `--no-isolate`, it can take a few seconds to recompile all runtime benchmarks
189+
and discover all benchmarks within them. If you only want to run benchmark(s) from a single crate,
190+
you can use this to speed up the runtime benchmarking or profiling commands.
186191

187192
The `bench_runtime_local` command also shares some options with the `bench_local` command, notably
188193
`--id`, `--db`, `--cargo`, `--include`, `--exclude` and `--iterations`.

collector/src/bin/collector.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,14 @@ struct CompileTimeOptions {
459459
rustdoc: Option<PathBuf>,
460460
}
461461

462+
#[derive(Debug, clap::Args)]
463+
struct RuntimeOptions {
464+
/// Select a runtime benchmark group that should be compiled and used. If not specified, all
465+
/// found groups will be compiled.
466+
#[arg(long)]
467+
group: Option<String>,
468+
}
469+
462470
#[derive(Debug, clap::Args)]
463471
struct SelfProfileOption {
464472
/// Collect self-profile data
@@ -492,6 +500,9 @@ enum Commands {
492500
#[command(flatten)]
493501
local: LocalOptions,
494502

503+
#[command(flatten)]
504+
runtime: RuntimeOptions,
505+
495506
/// How many iterations of each benchmark should be executed.
496507
#[arg(long, default_value_t = DEFAULT_RUNTIME_ITERATIONS)]
497508
iterations: u32,
@@ -507,6 +518,9 @@ enum Commands {
507518

508519
/// Profiles a runtime benchmark.
509520
ProfileRuntime {
521+
#[command(flatten)]
522+
runtime: RuntimeOptions,
523+
510524
/// Profiler to use
511525
profiler: RuntimeProfiler,
512526

@@ -660,6 +674,7 @@ fn main_result() -> anyhow::Result<i32> {
660674
match args.command {
661675
Commands::BenchRuntimeLocal {
662676
local,
677+
runtime,
663678
iterations,
664679
db,
665680
no_isolate,
@@ -680,6 +695,7 @@ fn main_result() -> anyhow::Result<i32> {
680695
conn.as_mut(),
681696
&runtime_benchmark_dir,
682697
isolation_mode,
698+
runtime.group,
683699
&toolchain,
684700
&artifact_id,
685701
))?;
@@ -697,6 +713,7 @@ fn main_result() -> anyhow::Result<i32> {
697713
Ok(0)
698714
}
699715
Commands::ProfileRuntime {
716+
runtime,
700717
profiler,
701718
rustc,
702719
benchmark,
@@ -707,6 +724,7 @@ fn main_result() -> anyhow::Result<i32> {
707724
&toolchain,
708725
&runtime_benchmark_dir,
709726
CargoIsolationMode::Cached,
727+
runtime.group,
710728
// Compile with debuginfo to have filenames and line numbers available in the
711729
// generated profiles.
712730
RuntimeCompilationOpts::default().debug_info("1"),
@@ -843,6 +861,7 @@ fn main_result() -> anyhow::Result<i32> {
843861
conn.as_mut(),
844862
&runtime_benchmark_dir,
845863
CargoIsolationMode::Isolated,
864+
None,
846865
&toolchain,
847866
&artifact_id,
848867
))?;
@@ -1050,6 +1069,7 @@ async fn load_runtime_benchmarks(
10501069
conn: &mut dyn Connection,
10511070
benchmark_dir: &Path,
10521071
isolation_mode: CargoIsolationMode,
1072+
group: Option<String>,
10531073
toolchain: &Toolchain,
10541074
artifact_id: &ArtifactId,
10551075
) -> anyhow::Result<BenchmarkSuite> {
@@ -1060,6 +1080,7 @@ async fn load_runtime_benchmarks(
10601080
toolchain,
10611081
benchmark_dir,
10621082
isolation_mode,
1083+
group,
10631084
RuntimeCompilationOpts::default(),
10641085
)?;
10651086

@@ -1185,6 +1206,7 @@ fn bench_published_artifact(
11851206
connection.as_mut(),
11861207
dirs.runtime,
11871208
CargoIsolationMode::Isolated,
1209+
None,
11881210
&toolchain,
11891211
&artifact_id,
11901212
))?;

collector/src/runtime/benchmark.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,16 @@ impl RuntimeCompilationOpts {
143143
/// We assume that each binary defines a benchmark suite using `benchlib`.
144144
/// We then execute each benchmark suite with the `list-benchmarks` command to find out its
145145
/// benchmark names.
146+
///
147+
/// If `group` is not `None`, only the benchmark group with the given name will be compiled.
146148
pub fn prepare_runtime_benchmark_suite(
147149
toolchain: &Toolchain,
148150
benchmark_dir: &Path,
149151
isolation_mode: CargoIsolationMode,
152+
group: Option<String>,
150153
opts: RuntimeCompilationOpts,
151154
) -> anyhow::Result<BenchmarkSuiteCompilation> {
152-
let benchmark_crates = get_runtime_benchmark_groups(benchmark_dir)?;
155+
let benchmark_crates = get_runtime_benchmark_groups(benchmark_dir, group)?;
153156

154157
let temp_dir: Option<TempDir> = match isolation_mode {
155158
CargoIsolationMode::Cached => None,
@@ -167,7 +170,7 @@ pub fn prepare_runtime_benchmark_suite(
167170
};
168171

169172
let group_count = benchmark_crates.len();
170-
println!("Compiling {group_count} runtime benchmark groups");
173+
println!("Compiling {group_count} runtime benchmark group(s)");
171174

172175
let mut groups = Vec::new();
173176
let mut failed_to_compile = HashMap::new();
@@ -347,7 +350,10 @@ fn gather_benchmarks(binary: &Path) -> anyhow::Result<Vec<String>> {
347350
}
348351

349352
/// Finds all runtime benchmarks (crates) in the given directory.
350-
fn get_runtime_benchmark_groups(directory: &Path) -> anyhow::Result<Vec<BenchmarkGroupCrate>> {
353+
fn get_runtime_benchmark_groups(
354+
directory: &Path,
355+
group: Option<String>,
356+
) -> anyhow::Result<Vec<BenchmarkGroupCrate>> {
351357
let mut groups = Vec::new();
352358
for entry in std::fs::read_dir(directory).with_context(|| {
353359
anyhow::anyhow!("Failed to list benchmark dir '{}'", directory.display())
@@ -363,6 +369,12 @@ fn get_runtime_benchmark_groups(directory: &Path) -> anyhow::Result<Vec<Benchmar
363369
.ok_or_else(|| anyhow::anyhow!("Cannot get filename of {}", path.display()))?
364370
.to_string();
365371

372+
if let Some(ref group) = group {
373+
if group != &name {
374+
continue;
375+
}
376+
}
377+
366378
groups.push(BenchmarkGroupCrate { name, path });
367379
}
368380
groups.sort_unstable_by(|a, b| a.name.cmp(&b.name));

0 commit comments

Comments
 (0)