Skip to content

Commit aa77f26

Browse files
authored
Merge pull request rust-lang#1963 from s7tya/improve-compare-cmd-ui
Improve compare cmd UI
2 parents d619e6c + 9325e6d commit aa77f26

File tree

4 files changed

+205
-13
lines changed

4 files changed

+205
-13
lines changed

Cargo.lock

+107-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

collector/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ tabled = { version = "0.14.0", features = ["ansi-str"] }
4242
humansize = "2.1.3"
4343
regex = "1.7.1"
4444
analyzeme = "12.0.0"
45+
inquire = "0.7.5"
4546

4647
benchlib = { path = "benchlib" }
4748

collector/src/bin/collector.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -635,11 +635,15 @@ enum Commands {
635635
#[command(flatten)]
636636
db: DbOption,
637637

638+
/// Metric used to compare artifacts.
639+
#[arg(long)]
640+
metric: Option<database::metric::Metric>,
641+
638642
/// The name of the base artifact to be compared.
639-
base: String,
643+
base: Option<String>,
640644

641645
/// The name of the modified artifact to be compared.
642-
modified: String,
646+
modified: Option<String>,
643647
},
644648
}
645649

@@ -1200,11 +1204,16 @@ Make sure to modify `{dir}/perf-config.json` if the category/artifact don't matc
12001204
println!("Data of artifact {name} were removed");
12011205
Ok(0)
12021206
}
1203-
Commands::BenchCmp { db, base, modified } => {
1207+
Commands::BenchCmp {
1208+
db,
1209+
base,
1210+
modified,
1211+
metric,
1212+
} => {
12041213
let pool = Pool::open(&db.db);
12051214
let rt = build_async_runtime();
12061215
let conn = rt.block_on(pool.connection());
1207-
rt.block_on(compare_artifacts(conn, base, modified))?;
1216+
rt.block_on(compare_artifacts(conn, metric, base, modified))?;
12081217
Ok(0)
12091218
}
12101219
}

collector/src/compare.rs

+84-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::Arc;
1+
use std::{str::FromStr, sync::Arc};
22

33
use database::{
44
metric::Metric,
@@ -7,16 +7,95 @@ use database::{
77
};
88
use tabled::{Table, Tabled};
99

10+
static ALL_METRICS: &[Metric] = &[
11+
Metric::InstructionsUser,
12+
Metric::Cycles,
13+
Metric::WallTime,
14+
Metric::MaxRSS,
15+
Metric::LinkedArtifactSize,
16+
Metric::AssemblyFileSize,
17+
Metric::BranchMisses,
18+
Metric::CacheMisses,
19+
Metric::CodegenUnitLlvmIrCount,
20+
Metric::CodegenUnitSize,
21+
Metric::ContextSwitches,
22+
Metric::CpuClock,
23+
Metric::CpuClockUser,
24+
Metric::CrateMetadataSize,
25+
Metric::CyclesUser,
26+
Metric::DepGraphSize,
27+
Metric::DocByteSize,
28+
Metric::DwoFileSize,
29+
Metric::Faults,
30+
Metric::FaultsUser,
31+
Metric::LlvmBitcodeSize,
32+
Metric::LlvmIrSize,
33+
Metric::ObjectFileSize,
34+
Metric::QueryCacheSize,
35+
Metric::TaskClock,
36+
Metric::TaskClockUser,
37+
Metric::WorkProductIndexSize,
38+
];
39+
1040
/// Compare 2 artifacts and print the result.
1141
pub async fn compare_artifacts(
1242
mut conn: Box<dyn Connection>,
13-
base: String,
14-
modified: String,
43+
metric: Option<Metric>,
44+
base: Option<String>,
45+
modified: Option<String>,
1546
) -> anyhow::Result<()> {
1647
let index = database::Index::load(&mut *conn).await;
1748

18-
let query = CompileBenchmarkQuery::default()
19-
.metric(database::selector::Selector::One(Metric::InstructionsUser));
49+
let metric = match metric {
50+
Some(v) => v,
51+
None => {
52+
let metric_str = inquire::Select::new(
53+
"Choose metric:",
54+
ALL_METRICS.iter().map(|m| m.as_str()).collect::<Vec<_>>(),
55+
)
56+
.prompt()?;
57+
Metric::from_str(metric_str).map_err(|e| anyhow::anyhow!(e))?
58+
}
59+
};
60+
61+
let mut aids = index.artifacts().map(str::to_string).collect::<Vec<_>>();
62+
if aids.len() < 2 {
63+
return Err(anyhow::anyhow!(
64+
"There are not enough artifacts to compare, at least two are needed"
65+
));
66+
}
67+
68+
let select_artifact_id = |name: &str, aids: &Vec<String>| {
69+
anyhow::Ok(
70+
inquire::Select::new(
71+
&format!("Choose {} artifact to compare:", name),
72+
aids.clone(),
73+
)
74+
.prompt()?,
75+
)
76+
};
77+
78+
let base = match base {
79+
Some(v) => v,
80+
None => select_artifact_id("base", &aids)?.to_string(),
81+
};
82+
aids.retain(|id| id != &base);
83+
let modified = if aids.len() == 1 {
84+
let new_modified = aids[0].clone();
85+
println!(
86+
"Only 1 artifact remains, automatically selecting: {}",
87+
new_modified
88+
);
89+
90+
new_modified
91+
} else {
92+
match modified {
93+
Some(v) => v,
94+
None => select_artifact_id("modified", &aids)?.to_string(),
95+
}
96+
};
97+
98+
let query = CompileBenchmarkQuery::default().metric(database::selector::Selector::One(metric));
2099
let resp = query
21100
.execute(
22101
&mut *conn,

0 commit comments

Comments
 (0)