fix: handle ANSI escapes when counting exec. time

This commit is contained in:
Felix Spöttel 2021-12-31 13:05:23 +01:00
parent 01f0749c9c
commit fc9f90c9c2

View file

@ -50,19 +50,16 @@ pub fn parse_exec_time(output: &str) -> f64 {
if !l.contains("elapsed:") { if !l.contains("elapsed:") {
acc acc
} else { } else {
let timing = l let timing = l.split("(elapsed: ").last().unwrap();
.split("(elapsed: ") // use `contains` istd. of `ends_with`: string may contain ANSI escape sequences.
.last() // possible time formats: see [rust/library/core/src/time.rs](https://git.io/Jy1rI).
.unwrap(); if timing.contains("ns)") {
// see [rust/library/core/src/time.rs](https://git.io/Jy1rI)
if timing.ends_with("ns)") {
acc // range below rounding precision. acc // range below rounding precision.
} else if timing.ends_with("µs)") { } else if timing.contains("µs)") {
acc + parse_time(timing, "µs") / 1000_f64 acc + parse_time(timing, "µs") / 1000_f64
} else if timing.ends_with("ms)") { } else if timing.contains("ms)") {
acc + parse_time(timing, "ms") acc + parse_time(timing, "ms")
} else if timing.ends_with("s)") { } else if timing.contains("s)") {
acc + parse_time(timing, "s") * 1000_f64 acc + parse_time(timing, "s") * 1000_f64
} else { } else {
acc acc
@ -91,9 +88,10 @@ mod tests {
#[test] #[test]
fn test_parse_exec_time() { fn test_parse_exec_time() {
assert_approx_eq!( assert_approx_eq!(
parse_exec_time( parse_exec_time(&format!(
"🎄 Part 1 🎄\n0 (elapsed: 74.13ns)\n🎄 Part 2 🎄\n0 (elapsed: 50.00ns)" "🎄 Part 1 🎄\n0 (elapsed: 74.13ns){}\n🎄 Part 2 🎄\n0 (elapsed: 50.00ns){}",
), ANSI_RESET, ANSI_RESET
)),
0_f64 0_f64
); );