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

Add --measure-time flag to libtest #64714

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/libtest/formatters/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ impl<T: Write> OutputFormatter for JsonFormatter<T> {
stdout: &[u8],
state: &ConsoleTestState,
) -> io::Result<()> {
let stdout = if (state.options.display_output || *result != TrOk) && stdout.len() > 0 {
let stdout = if (state.options.display_output || !result.is_ok()) && stdout.len() > 0 {
Some(String::from_utf8_lossy(stdout))
} else {
None
};
match *result {
TrOk => self.write_event("test", desc.name.as_slice(), "ok", stdout, None),
TrOk(_) => self.write_event("test", desc.name.as_slice(), "ok", stdout, None),

TrFailed => self.write_event("test", desc.name.as_slice(), "failed", stdout, None),

Expand Down
35 changes: 32 additions & 3 deletions src/libtest/formatters/pretty.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::time::Duration;
use super::*;

pub(crate) struct PrettyFormatter<T> {
Expand All @@ -8,6 +9,8 @@ pub(crate) struct PrettyFormatter<T> {
max_name_len: usize,

is_multithreaded: bool,

test_time_params: Option<TestTimeParams>,
}

impl<T: Write> PrettyFormatter<T> {
Expand All @@ -16,12 +19,14 @@ impl<T: Write> PrettyFormatter<T> {
use_color: bool,
max_name_len: usize,
is_multithreaded: bool,
test_time_params: Option<TestTimeParams>
) -> Self {
PrettyFormatter {
out,
use_color,
max_name_len,
is_multithreaded,
test_time_params,
}
}

Expand All @@ -30,8 +35,32 @@ impl<T: Write> PrettyFormatter<T> {
&self.out
}

pub fn write_ok(&mut self) -> io::Result<()> {
self.write_short_result("ok", term::color::GREEN)
fn write_ok_with_duration(&mut self, duration: Duration) -> io::Result<()> {
self.write_pretty("ok", term::color::GREEN)?;

let time_str = format!(" <{}.{} s>", duration.as_secs(), duration.subsec_millis());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If subsec_millis returns less than three digits the output is wrong:

assert_ne!(" <1.023 s>", format!(" <{}.{} s>", 1, 23));

match &self.test_time_params {
Some(params) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use guards here instead so we can fuse the .write_plains.

if duration >= params.test_time_critical {
self.write_pretty(&time_str, term::color::RED)?;
} else if duration >= params.test_time_warn {
self.write_pretty(&time_str, term::color::YELLOW)?;
} else {
self.write_plain(&time_str)?;
}
}
None => {
self.write_plain(&time_str)?;
}
}
self.write_plain("\n")
}

pub fn write_ok(&mut self, duration: Option<Duration>) -> io::Result<()> {
match duration {
Some(d) => self.write_ok_with_duration(d),
None => self.write_short_result("ok", term::color::GREEN)
}
}

pub fn write_failed(&mut self) -> io::Result<()> {
Expand Down Expand Up @@ -174,7 +203,7 @@ impl<T: Write> OutputFormatter for PrettyFormatter<T> {
}

match *result {
TrOk => self.write_ok(),
TrOk(d) => self.write_ok(d),
TrFailed | TrFailedMsg(_) => self.write_failed(),
TrIgnored => self.write_ignored(),
TrAllowedFail => self.write_allowed_fail(),
Expand Down
2 changes: 1 addition & 1 deletion src/libtest/formatters/terse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl<T: Write> OutputFormatter for TerseFormatter<T> {
_: &ConsoleTestState,
) -> io::Result<()> {
match *result {
TrOk => self.write_ok(),
TrOk(_) => self.write_ok(),
TrFailed | TrFailedMsg(_) => self.write_failed(),
TrIgnored => self.write_ignored(),
TrAllowedFail => self.write_allowed_fail(),
Expand Down
Loading