Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include executed tests in the build metrics (and use a custom test display impl) #108659

Merged
merged 14 commits into from
Mar 21, 2023
Merged
22 changes: 22 additions & 0 deletions src/bootstrap/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ dependencies = [
"memchr",
]

[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]

[[package]]
name = "autocfg"
version = "1.1.0"
Expand All @@ -36,6 +47,7 @@ dependencies = [
name = "bootstrap"
version = "0.0.0"
dependencies = [
"atty",
"build_helper",
"cc",
"cmake",
Expand All @@ -59,6 +71,7 @@ dependencies = [
"walkdir",
"winapi",
"xz2",
"yansi-term",
]

[[package]]
Expand Down Expand Up @@ -800,3 +813,12 @@ name = "yansi"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec"

[[package]]
name = "yansi-term"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe5c30ade05e61656247b2e334a031dfd0cc466fadef865bdcdea8d537951bf1"
dependencies = [
"winapi",
]
2 changes: 2 additions & 0 deletions src/bootstrap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ path = "bin/sccache-plus-cl.rs"
test = false

[dependencies]
atty = "0.2.14"
build_helper = { path = "../tools/build_helper" }
cmake = "0.1.38"
fd-lock = "3.0.8"
Expand All @@ -52,6 +53,7 @@ opener = "0.5"
once_cell = "1.7.2"
xz2 = "0.1"
walkdir = "2"
yansi-term = "0.1.2"
Mark-Simulacrum marked this conversation as resolved.
Show resolved Hide resolved

# Dependencies needed by the build-metrics feature
sysinfo = { version = "0.26.0", optional = true }
Expand Down
6 changes: 6 additions & 0 deletions src/bootstrap/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ pub struct Config {
pub patch_binaries_for_nix: bool,
pub stage0_metadata: Stage0Metadata,

pub stdout_is_tty: bool,
pub stderr_is_tty: bool,

pub on_fail: Option<String>,
pub stage: u32,
pub keep_stage: Vec<u32>,
Expand Down Expand Up @@ -822,6 +825,9 @@ impl Config {
config.deny_warnings = true;
config.bindir = "bin".into();

config.stdout_is_tty = atty::is(atty::Stream::Stdout);
config.stderr_is_tty = atty::is(atty::Stream::Stderr);

// set by build.rs
config.build = TargetSelection::from_user(&env!("BUILD_TRIPLE"));

Expand Down
19 changes: 19 additions & 0 deletions src/bootstrap/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ mod format;
mod install;
mod metadata;
mod native;
mod render_tests;
mod run;
mod sanity;
mod setup;
Expand Down Expand Up @@ -88,6 +89,7 @@ pub use crate::builder::PathSet;
use crate::cache::{Interned, INTERNER};
pub use crate::config::Config;
pub use crate::flags::Subcommand;
use yansi_term::Color;

const LLVM_TOOLS: &[&str] = &[
"llvm-cov", // used to generate coverage report
Expand Down Expand Up @@ -1574,6 +1576,23 @@ to download LLVM rather than building it.

self.config.ninja_in_file
}

pub fn color_for_stdout(&self, color: Color, message: &str) -> String {
self.color_for_inner(color, message, self.config.stdout_is_tty)
}

pub fn color_for_stderr(&self, color: Color, message: &str) -> String {
self.color_for_inner(color, message, self.config.stderr_is_tty)
}

fn color_for_inner(&self, color: Color, message: &str, is_tty: bool) -> String {
let use_color = match self.config.color {
flags::Color::Always => true,
flags::Color::Never => false,
flags::Color::Auto => is_tty,
};
if use_color { color.paint(message).to_string() } else { message.to_string() }
}
}

#[cfg(unix)]
Expand Down
44 changes: 39 additions & 5 deletions src/bootstrap/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl BuildMetrics {
duration_excluding_children_sec: Duration::ZERO,

children: Vec::new(),
tests: Vec::new(),
});
}

Expand All @@ -72,6 +73,16 @@ impl BuildMetrics {
}
}

pub(crate) fn record_test(&self, name: &str, outcome: TestOutcome) {
let mut state = self.state.borrow_mut();
state
.running_steps
.last_mut()
.unwrap()
.tests
.push(Test { name: name.to_string(), outcome });
}

fn collect_stats(&self, state: &mut MetricsState) {
let step = state.running_steps.last_mut().unwrap();

Expand Down Expand Up @@ -125,6 +136,14 @@ impl BuildMetrics {
}

fn prepare_json_step(&self, step: StepMetrics) -> JsonNode {
let mut children = Vec::new();
children.extend(step.children.into_iter().map(|child| self.prepare_json_step(child)));
children.extend(
step.tests
.into_iter()
.map(|test| JsonNode::Test { name: test.name, outcome: test.outcome }),
);

JsonNode::RustbuildStep {
type_: step.type_,
debug_repr: step.debug_repr,
Expand All @@ -135,11 +154,7 @@ impl BuildMetrics {
/ step.duration_excluding_children_sec.as_secs_f64(),
},

children: step
.children
.into_iter()
.map(|child| self.prepare_json_step(child))
.collect(),
children,
}
}
}
Expand All @@ -161,6 +176,12 @@ struct StepMetrics {
duration_excluding_children_sec: Duration,

children: Vec<StepMetrics>,
tests: Vec<Test>,
}

struct Test {
name: String,
outcome: TestOutcome,
}

#[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -190,6 +211,19 @@ enum JsonNode {

children: Vec<JsonNode>,
},
Test {
name: String,
#[serde(flatten)]
outcome: TestOutcome,
},
}

#[derive(Serialize, Deserialize)]
#[serde(tag = "outcome", rename_all = "snake_case")]
pub(crate) enum TestOutcome {
Passed,
Failed,
Ignored { ignore_reason: Option<String> },
}

#[derive(Serialize, Deserialize)]
Expand Down
Loading