Skip to content

Commit 3c9b076

Browse files
committed
include test suite metadata in build metrics
1 parent 776f222 commit 3c9b076

File tree

2 files changed

+104
-22
lines changed

2 files changed

+104
-22
lines changed

src/bootstrap/metrics.rs

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl BuildMetrics {
5757
duration_excluding_children_sec: Duration::ZERO,
5858

5959
children: Vec::new(),
60-
tests: Vec::new(),
60+
test_suites: Vec::new(),
6161
});
6262
}
6363

@@ -84,19 +84,33 @@ impl BuildMetrics {
8484
}
8585
}
8686

87+
pub(crate) fn begin_test_suite(&self, metadata: TestSuiteMetadata, builder: &Builder<'_>) {
88+
// Do not record dry runs, as they'd be duplicates of the actual steps.
89+
if builder.config.dry_run() {
90+
return;
91+
}
92+
93+
let mut state = self.state.borrow_mut();
94+
let step = state.running_steps.last_mut().unwrap();
95+
step.test_suites.push(TestSuite { metadata, tests: Vec::new() });
96+
}
97+
8798
pub(crate) fn record_test(&self, name: &str, outcome: TestOutcome, builder: &Builder<'_>) {
8899
// Do not record dry runs, as they'd be duplicates of the actual steps.
89100
if builder.config.dry_run() {
90101
return;
91102
}
92103

93104
let mut state = self.state.borrow_mut();
94-
state
95-
.running_steps
96-
.last_mut()
97-
.unwrap()
98-
.tests
99-
.push(Test { name: name.to_string(), outcome });
105+
let step = state.running_steps.last_mut().unwrap();
106+
107+
if let Some(test_suite) = step.test_suites.last_mut() {
108+
test_suite.tests.push(Test { name: name.to_string(), outcome });
109+
} else {
110+
panic!(
111+
"metrics.record_test() called without calling metrics.record_test_suite() first"
112+
);
113+
}
100114
}
101115

102116
fn collect_stats(&self, state: &mut MetricsState) {
@@ -159,11 +173,7 @@ impl BuildMetrics {
159173
fn prepare_json_step(&self, step: StepMetrics) -> JsonNode {
160174
let mut children = Vec::new();
161175
children.extend(step.children.into_iter().map(|child| self.prepare_json_step(child)));
162-
children.extend(
163-
step.tests
164-
.into_iter()
165-
.map(|test| JsonNode::Test { name: test.name, outcome: test.outcome }),
166-
);
176+
children.extend(step.test_suites.into_iter().map(|suite| JsonNode::TestSuite(suite)));
167177

168178
JsonNode::RustbuildStep {
169179
type_: step.type_,
@@ -198,12 +208,7 @@ struct StepMetrics {
198208
duration_excluding_children_sec: Duration,
199209

200210
children: Vec<StepMetrics>,
201-
tests: Vec<Test>,
202-
}
203-
204-
struct Test {
205-
name: String,
206-
outcome: TestOutcome,
211+
test_suites: Vec<TestSuite>,
207212
}
208213

209214
#[derive(Serialize, Deserialize)]
@@ -237,13 +242,41 @@ enum JsonNode {
237242

238243
children: Vec<JsonNode>,
239244
},
240-
Test {
241-
name: String,
242-
#[serde(flatten)]
243-
outcome: TestOutcome,
245+
TestSuite(TestSuite),
246+
}
247+
248+
#[derive(Serialize, Deserialize)]
249+
struct TestSuite {
250+
metadata: TestSuiteMetadata,
251+
tests: Vec<Test>,
252+
}
253+
254+
#[derive(Serialize, Deserialize)]
255+
#[serde(tag = "kind", rename_all = "snake_case")]
256+
pub(crate) enum TestSuiteMetadata {
257+
Crate {
258+
crates: Vec<String>,
259+
target: String,
260+
host: String,
261+
stage: u32,
262+
},
263+
Compiletest {
264+
suite: String,
265+
mode: String,
266+
compare_mode: Option<String>,
267+
target: String,
268+
host: String,
269+
stage: u32,
244270
},
245271
}
246272

273+
#[derive(Serialize, Deserialize)]
274+
pub(crate) struct Test {
275+
name: String,
276+
#[serde(flatten)]
277+
outcome: TestOutcome,
278+
}
279+
247280
#[derive(Serialize, Deserialize)]
248281
#[serde(tag = "outcome", rename_all = "snake_case")]
249282
pub(crate) enum TestOutcome {

src/bootstrap/test.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,17 @@ impl Step for Cargo {
317317
cargo.env("CARGO_TEST_DISABLE_NIGHTLY", "1");
318318
cargo.env("PATH", &path_for_cargo(builder, compiler));
319319

320+
#[cfg(feature = "build-metrics")]
321+
builder.metrics.begin_test_suite(
322+
crate::metrics::TestSuiteMetadata::Crate {
323+
crates: vec!["cargo".into()],
324+
target: self.host.triple.to_string(),
325+
host: self.host.triple.to_string(),
326+
stage: self.stage,
327+
},
328+
builder,
329+
);
330+
320331
let _time = util::timeit(&builder);
321332
add_flags_and_try_run_tests(builder, &mut cargo);
322333
}
@@ -1759,6 +1770,19 @@ note: if you're sure you want to do this, please open an issue as to why. In the
17591770

17601771
builder.ci_env.force_coloring_in_ci(&mut cmd);
17611772

1773+
#[cfg(feature = "build-metrics")]
1774+
builder.metrics.begin_test_suite(
1775+
crate::metrics::TestSuiteMetadata::Compiletest {
1776+
suite: suite.into(),
1777+
mode: mode.into(),
1778+
compare_mode: None,
1779+
target: self.target.triple.to_string(),
1780+
host: self.compiler.host.triple.to_string(),
1781+
stage: self.compiler.stage,
1782+
},
1783+
builder,
1784+
);
1785+
17621786
builder.info(&format!(
17631787
"Check compiletest suite={} mode={} ({} -> {})",
17641788
suite, mode, &compiler.host, target
@@ -1768,6 +1792,20 @@ note: if you're sure you want to do this, please open an issue as to why. In the
17681792

17691793
if let Some(compare_mode) = compare_mode {
17701794
cmd.arg("--compare-mode").arg(compare_mode);
1795+
1796+
#[cfg(feature = "build-metrics")]
1797+
builder.metrics.begin_test_suite(
1798+
crate::metrics::TestSuiteMetadata::Compiletest {
1799+
suite: suite.into(),
1800+
mode: mode.into(),
1801+
compare_mode: Some(compare_mode.into()),
1802+
target: self.target.triple.to_string(),
1803+
host: self.compiler.host.triple.to_string(),
1804+
stage: self.compiler.stage,
1805+
},
1806+
builder,
1807+
);
1808+
17711809
builder.info(&format!(
17721810
"Check compiletest suite={} mode={} compare_mode={} ({} -> {})",
17731811
suite, mode, compare_mode, &compiler.host, target
@@ -2094,6 +2132,17 @@ fn run_cargo_test(
20942132
let mut cargo =
20952133
prepare_cargo_test(cargo, libtest_args, crates, primary_crate, compiler, target, builder);
20962134
let _time = util::timeit(&builder);
2135+
2136+
#[cfg(feature = "build-metrics")]
2137+
builder.metrics.begin_test_suite(
2138+
crate::metrics::TestSuiteMetadata::Crate {
2139+
crates: crates.iter().map(|c| c.to_string()).collect(),
2140+
target: target.triple.to_string(),
2141+
host: compiler.host.triple.to_string(),
2142+
stage: compiler.stage,
2143+
},
2144+
builder,
2145+
);
20972146
add_flags_and_try_run_tests(builder, &mut cargo)
20982147
}
20992148

0 commit comments

Comments
 (0)