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