1use std::collections::BTreeMap;
44use std::path::Path;
45
46use crate::gate_stamp::{Note, Run, RunOutcome, NOTES_REF};
47
48pub fn now() -> u64 {
52 std::time::SystemTime::now()
53 .duration_since(std::time::UNIX_EPOCH)
54 .map(|d| d.as_secs())
55 .unwrap_or(0)
56}
57
58#[derive(Debug, Clone, Default, PartialEq, Eq)]
65pub struct History {
66 pub runs: Vec<(String, Run)>,
67}
68
69impl History {
70 pub fn is_empty(&self) -> bool {
71 self.runs.is_empty()
72 }
73
74 fn within(&self, now: u64, window_days: u64) -> Vec<(&str, &Run)> {
77 let floor = if window_days == 0 {
78 0
79 } else {
80 now.saturating_sub(window_days.saturating_mul(86_400))
81 };
82 let mut runs: Vec<(&str, &Run)> = self
83 .runs
84 .iter()
85 .filter(|(_, r)| r.at >= floor)
86 .map(|(fp, r)| (fp.as_str(), r))
87 .collect();
88 runs.sort_by(|a, b| a.1.at.cmp(&b.1.at).then_with(|| a.1.gate.cmp(&b.1.gate)));
91 runs
92 }
93}
94
95pub fn history_in(repo: &Path) -> History {
103 let Some(list) = crate::git::stdout_in(repo, &["notes", "--ref", NOTES_REF, "list"]) else {
106 return History::default();
107 };
108 let mut keyed: BTreeMap<String, String> = BTreeMap::new();
111 let mut stdin = String::new();
112 for line in list.lines() {
113 let mut t = line.split_whitespace();
114 if let (Some(blob), Some(object)) = (t.next(), t.next()) {
115 keyed.insert(blob.to_string(), object.to_string());
116 stdin.push_str(blob);
117 stdin.push('\n');
118 }
119 }
120 if keyed.is_empty() {
121 return History::default();
122 }
123 let Some(batch) = crate::git::stdout_piped_in(repo, &["cat-file", "--batch"], stdin.as_bytes())
124 else {
125 return History::default();
126 };
127 let mut history = History::default();
128 for (blob, body) in parse_batch(&batch) {
129 let Some(fingerprint) = keyed.get(&blob) else {
130 continue;
131 };
132 for run in Note::parse(&body).runs {
133 history.runs.push((fingerprint.clone(), run));
134 }
135 }
136 history
137}
138
139fn parse_batch(out: &str) -> Vec<(String, String)> {
149 let mut found = Vec::new();
150 let mut current: Option<(String, Vec<&str>)> = None;
151 for line in out.lines() {
152 let mut t = line.split_whitespace();
153 let (oid, kind, size) = (t.next(), t.next(), t.next());
154 let is_header = matches!((oid, kind, size), (Some(o), Some("blob"), Some(s))
155 if o.len() >= 40
156 && o.chars().all(|c| c.is_ascii_hexdigit())
157 && s.parse::<u64>().is_ok()
158 && t.next().is_none());
159 if is_header {
160 if let Some((oid, body)) = current.take() {
161 found.push((oid, body.join("\n")));
162 }
163 current = Some((oid.unwrap_or_default().to_string(), Vec::new()));
164 continue;
165 }
166 if let Some((_, body)) = current.as_mut() {
167 body.push(line);
168 }
169 }
170 if let Some((oid, body)) = current.take() {
171 found.push((oid, body.join("\n")));
172 }
173 found
174}
175
176#[derive(Debug, Clone, Copy, PartialEq, Eq)]
179pub struct Thresholds {
180 pub window_days: u64,
182 pub min_runs: usize,
184 pub noop_ratio_percent: u64,
186 pub noop_median_secs: u64,
189 pub fast_pass_ms: u64,
192 pub stale_runs: usize,
195 pub stale_days: u64,
197}
198
199impl Default for Thresholds {
200 fn default() -> Self {
201 Thresholds {
202 window_days: 90,
203 min_runs: 5,
204 noop_ratio_percent: 10,
205 noop_median_secs: 30,
206 fast_pass_ms: 1_000,
207 stale_runs: 10,
208 stale_days: 30,
209 }
210 }
211}
212
213#[derive(Debug, Clone, Copy, PartialEq, Eq)]
214pub enum FlagKind {
215 NoOpSuspect,
217 Flaky,
219 Stale,
221}
222
223impl FlagKind {
224 pub fn as_str(self) -> &'static str {
225 match self {
226 FlagKind::NoOpSuspect => "no-op suspect",
227 FlagKind::Flaky => "flaky",
228 FlagKind::Stale => "stale",
229 }
230 }
231}
232
233#[derive(Debug, Clone, PartialEq, Eq)]
237pub struct Flag {
238 pub kind: FlagKind,
239 pub why: String,
240}
241
242#[derive(Debug, Clone, PartialEq, Eq)]
244pub struct GateReport {
245 pub gate: String,
246 pub runs: usize,
248 pub passed: usize,
249 pub failed: usize,
250 pub unavailable: usize,
254 pub median_ms: u64,
256 pub last_ms: u64,
257 pub last_at: u64,
258 pub last_outcome: Option<RunOutcome>,
259 pub fingerprints: usize,
261 pub missed: usize,
264 pub flags: Vec<Flag>,
265 pub abstained: Option<String>,
269}
270
271impl GateReport {
272 pub fn last_age_days(&self, now: u64) -> u64 {
274 now.saturating_sub(self.last_at) / 86_400
275 }
276}
277
278pub fn summarise(history: &History, now: u64, t: &Thresholds) -> Vec<GateReport> {
285 let runs = history.within(now, t.window_days);
286 let mut by_gate: BTreeMap<&str, Vec<(&str, &Run)>> = BTreeMap::new();
287 for &(fp, run) in &runs {
288 by_gate
289 .entry(run.gate.as_str())
290 .or_default()
291 .push((fp, run));
292 }
293 by_gate
294 .into_iter()
295 .map(|(gate, mine)| one_gate(gate, &mine, &runs, now, t))
296 .collect()
297}
298
299fn one_gate(
300 gate: &str,
301 mine: &[(&str, &Run)],
302 all: &[(&str, &Run)],
303 now: u64,
304 t: &Thresholds,
305) -> GateReport {
306 let verdicts: Vec<(&str, &Run)> = mine
307 .iter()
308 .filter(|(_, r)| r.outcome.is_verdict())
309 .copied()
310 .collect();
311 let passed = verdicts
312 .iter()
313 .filter(|(_, r)| r.outcome == RunOutcome::Passed)
314 .count();
315 let failed = verdicts.len() - passed;
316 let unavailable = mine
317 .iter()
318 .filter(|(_, r)| r.outcome == RunOutcome::Unavailable)
319 .count();
320 let median_ms = median(&verdicts.iter().map(|(_, r)| r.ms).collect::<Vec<_>>());
321 let last = mine.last().copied();
322 let mut fingerprints: Vec<&str> = mine.iter().map(|(fp, _)| *fp).collect();
323 fingerprints.sort_unstable();
324 fingerprints.dedup();
325
326 let last_at = last.map(|(_, r)| r.at).unwrap_or(0);
330 let mut later: Vec<&str> = all
331 .iter()
332 .filter(|(_, r)| r.at > last_at && r.gate != gate)
333 .map(|(fp, _)| *fp)
334 .collect();
335 later.sort_unstable();
336 later.dedup();
337 let missed = later
338 .iter()
339 .filter(|fp| !fingerprints.contains(*fp))
340 .count();
341
342 let mut report = GateReport {
343 gate: gate.to_string(),
344 runs: mine.len(),
345 passed,
346 failed,
347 unavailable,
348 median_ms,
349 last_ms: last.map(|(_, r)| r.ms).unwrap_or(0),
350 last_at,
351 last_outcome: last.map(|(_, r)| r.outcome),
352 fingerprints: fingerprints.len(),
353 missed,
354 flags: Vec::new(),
355 abstained: None,
356 };
357
358 if let Some(flag) = stale_flag(&report, all, now, t) {
362 report.flags.push(flag);
363 }
364
365 if verdicts.len() < t.min_runs {
366 report.abstained = Some(format!(
367 "insufficient history ({} verdict{})",
368 verdicts.len(),
369 if verdicts.len() == 1 { "" } else { "s" }
370 ));
371 return report;
372 }
373
374 if let Some(flag) = noop_flag(&verdicts, median_ms, t) {
375 report.flags.push(flag);
376 }
377 if let Some(flag) = flaky_flag(gate, &verdicts) {
378 report.flags.push(flag);
379 }
380 report
381}
382
383fn noop_flag(verdicts: &[(&str, &Run)], median_ms: u64, t: &Thresholds) -> Option<Flag> {
391 let (_, last) = *verdicts.last()?;
392 if median_ms >= t.noop_median_secs.saturating_mul(1_000)
393 && last.ms.saturating_mul(100) < median_ms.saturating_mul(t.noop_ratio_percent)
394 {
395 return Some(Flag {
396 kind: FlagKind::NoOpSuspect,
397 why: format!(
398 "last run {} against a median of {} ({}% of it, threshold {}%)",
399 duration(last.ms),
400 duration(median_ms),
401 last.ms.saturating_mul(100) / median_ms.max(1),
402 t.noop_ratio_percent
403 ),
404 });
405 }
406 if last.outcome == RunOutcome::Passed
407 && last.ms < t.fast_pass_ms
408 && verdicts[..verdicts.len() - 1]
409 .iter()
410 .all(|(_, r)| r.ms >= t.fast_pass_ms)
411 {
412 return Some(Flag {
413 kind: FlagKind::NoOpSuspect,
414 why: format!(
415 "passed in {}, and none of the {} earlier runs ever finished under {}",
416 duration(last.ms),
417 verdicts.len() - 1,
418 duration(t.fast_pass_ms)
419 ),
420 });
421 }
422 None
423}
424
425fn flaky_flag(gate: &str, verdicts: &[(&str, &Run)]) -> Option<Flag> {
428 let mut by_fp: BTreeMap<&str, (usize, usize)> = BTreeMap::new();
429 for &(fp, run) in verdicts {
430 let e = by_fp.entry(fp).or_insert((0, 0));
431 if run.outcome == RunOutcome::Passed {
432 e.0 += 1;
433 } else {
434 e.1 += 1;
435 }
436 }
437 let split: Vec<(&str, usize, usize)> = by_fp
438 .into_iter()
439 .filter(|(_, (pass, fail))| *pass > 0 && *fail > 0)
440 .map(|(fp, (pass, fail))| (fp, pass, fail))
441 .collect();
442 let (fp, pass, fail) = *split.first()?;
443 Some(Flag {
444 kind: FlagKind::Flaky,
445 why: format!(
446 "{gate} both passed ({pass}) and failed ({fail}) on tree {}{}",
447 short(fp),
448 if split.len() > 1 {
449 format!(", and on {} other tree(s)", split.len() - 1)
450 } else {
451 String::new()
452 }
453 ),
454 })
455}
456
457fn stale_flag(report: &GateReport, all: &[(&str, &Run)], now: u64, t: &Thresholds) -> Option<Flag> {
459 if report.runs == 0 {
460 return None;
461 }
462 let newest_other = all
463 .iter()
464 .filter(|(_, r)| r.gate != report.gate)
465 .map(|(_, r)| r.at)
466 .max()?;
467 if newest_other <= report.last_at {
468 return None;
469 }
470 if report.missed >= t.stale_runs {
471 return Some(Flag {
472 kind: FlagKind::Stale,
473 why: format!(
474 "{} later tree(s) were judged by other gates and not by this one (threshold {})",
475 report.missed, t.stale_runs
476 ),
477 });
478 }
479 let age = report.last_age_days(now);
480 if age >= t.stale_days {
481 return Some(Flag {
482 kind: FlagKind::Stale,
483 why: format!(
484 "last ran {age} days ago (threshold {}), while another gate ran {} days ago",
485 t.stale_days,
486 now.saturating_sub(newest_other) / 86_400
487 ),
488 });
489 }
490 None
491}
492
493pub fn order_by_evidence(
515 gates: &[String],
516 history: &History,
517 now: u64,
518 window_days: u64,
519) -> Vec<usize> {
520 let runs = history.within(now, window_days);
521 let mut stats: BTreeMap<&str, (u64, u64, Vec<u64>)> = BTreeMap::new();
522 for (_, run) in &runs {
523 if !run.outcome.is_verdict() {
524 continue;
525 }
526 let e = stats.entry(run.gate.as_str()).or_insert((0, 0, Vec::new()));
527 e.0 += 1;
528 if run.outcome == RunOutcome::Failed {
529 e.1 += 1;
530 }
531 e.2.push(run.ms);
532 }
533 let weight = |name: &String| -> Option<(u64, u64, u64)> {
535 let (runs, fails, ms) = stats.get(name.as_str())?;
536 (*fails > 0).then(|| (*fails, *runs, median(ms).max(1)))
537 };
538 let mut order: Vec<usize> = (0..gates.len()).collect();
539 order.sort_by(|&a, &b| {
540 match (weight(&gates[a]), weight(&gates[b])) {
541 (Some((fa, ra, ma)), Some((fb, rb, mb))) => {
542 let left = u128::from(fa) * u128::from(rb) * u128::from(mb);
545 let right = u128::from(fb) * u128::from(ra) * u128::from(ma);
546 right.cmp(&left).then(a.cmp(&b))
547 }
548 (Some(_), None) => std::cmp::Ordering::Less,
549 (None, Some(_)) => std::cmp::Ordering::Greater,
550 (None, None) => a.cmp(&b),
551 }
552 });
553 order
554}
555
556fn median(values: &[u64]) -> u64 {
557 if values.is_empty() {
558 return 0;
559 }
560 let mut v = values.to_vec();
561 v.sort_unstable();
562 let mid = v.len() / 2;
563 if v.len() % 2 == 1 {
564 v[mid]
565 } else {
566 v[mid - 1]
570 }
571}
572
573pub fn duration(ms: u64) -> String {
576 if ms < 1_000 {
577 return format!("{ms} ms");
578 }
579 if ms < 60_000 {
580 return format!("{}.{} s", ms / 1000, (ms % 1000) / 100);
581 }
582 format!("{} m {:02} s", ms / 60_000, (ms % 60_000) / 1000)
583}
584
585pub(crate) fn short(oid: &str) -> &str {
587 oid.get(..8).unwrap_or(oid)
588}
589
590#[cfg(test)]
591mod tests {
592 use super::*;
593
594 fn run(at: u64, gate: &str, outcome: RunOutcome, ms: u64) -> Run {
595 Run {
596 at,
597 gate: gate.to_string(),
598 outcome,
599 ms,
600 }
601 }
602
603 fn history(rows: &[(&str, Run)]) -> History {
604 History {
605 runs: rows
606 .iter()
607 .map(|(fp, r)| ((*fp).to_string(), r.clone()))
608 .collect(),
609 }
610 }
611
612 fn owned_history(rows: Vec<(String, Run)>) -> History {
615 History { runs: rows }
616 }
617
618 const NOW: u64 = 1_800_000_000;
619 const DAY: u64 = 86_400;
620
621 fn of<'a>(reports: &'a [GateReport], gate: &str) -> &'a GateReport {
622 reports
623 .iter()
624 .find(|r| r.gate == gate)
625 .unwrap_or_else(|| panic!("no report for {gate}: {reports:?}"))
626 }
627
628 fn kinds(r: &GateReport) -> Vec<FlagKind> {
629 r.flags.iter().map(|f| f.kind).collect()
630 }
631
632 #[test]
637 fn a_suite_that_collapsed_to_milliseconds_is_a_no_op_suspect() {
638 let mut rows: Vec<(&str, Run)> = (0..8)
639 .map(|i| {
640 (
641 "tree0",
642 run(
643 NOW - (10 - i) * DAY,
644 "pre-push-cargo-test",
645 RunOutcome::Passed,
646 600_000,
647 ),
648 )
649 })
650 .collect();
651 rows.push((
652 "tree9",
653 run(NOW - DAY, "pre-push-cargo-test", RunOutcome::Passed, 400),
654 ));
655 let reports = summarise(&history(&rows), NOW, &Thresholds::default());
656 let r = of(&reports, "pre-push-cargo-test");
657 assert_eq!(kinds(r), vec![FlagKind::NoOpSuspect]);
658 assert!(
659 r.flags[0].why.contains("400 ms") && r.flags[0].why.contains("threshold 10%"),
660 "the flag must carry the comparison it made: {:?}",
661 r.flags[0].why
662 );
663 }
664
665 #[test]
669 fn a_gate_that_never_once_took_real_time_is_also_suspect() {
670 let rows: Vec<(&str, Run)> = (0..6)
671 .map(|i| {
672 (
673 "tree0",
674 run(
675 NOW - (7 - i) * DAY,
676 "pre-push-audit-python",
677 RunOutcome::Passed,
678 if i == 5 { 30 } else { 4_000 },
679 ),
680 )
681 })
682 .collect();
683 let reports = summarise(&history(&rows), NOW, &Thresholds::default());
684 assert_eq!(
685 kinds(of(&reports, "pre-push-audit-python")),
686 vec![FlagKind::NoOpSuspect]
687 );
688 }
689
690 #[test]
693 fn one_fingerprint_with_two_answers_is_flaky() {
694 let rows = [
695 (
696 "treeA",
697 run(NOW - 6 * DAY, "pre-push-pytest", RunOutcome::Passed, 90_000),
698 ),
699 (
700 "treeA",
701 run(NOW - 5 * DAY, "pre-push-pytest", RunOutcome::Failed, 88_000),
702 ),
703 (
704 "treeA",
705 run(NOW - 4 * DAY, "pre-push-pytest", RunOutcome::Passed, 91_000),
706 ),
707 (
708 "treeB",
709 run(NOW - 3 * DAY, "pre-push-pytest", RunOutcome::Passed, 92_000),
710 ),
711 (
712 "treeB",
713 run(NOW - 2 * DAY, "pre-push-pytest", RunOutcome::Passed, 90_500),
714 ),
715 ];
716 let r = summarise(&history(&rows), NOW, &Thresholds::default());
717 let r = of(&r, "pre-push-pytest");
718 assert_eq!(kinds(r), vec![FlagKind::Flaky]);
719 assert!(r.flags[0].why.contains("treeA"), "{:?}", r.flags[0].why);
720 }
721
722 #[test]
725 fn a_gate_left_behind_by_its_neighbours_is_stale() {
726 let mut rows = vec![(
727 "tree0".to_string(),
728 run(
729 NOW - 40 * DAY,
730 "pre-push-go-test",
731 RunOutcome::Passed,
732 5_000,
733 ),
734 )];
735 for i in 0..12u64 {
736 rows.push((
737 format!("tree{}", i + 1),
738 run(
739 NOW - (12 - i) * DAY,
740 "pre-push-cargo-test",
741 RunOutcome::Passed,
742 5_000,
743 ),
744 ));
745 }
746 let reports = summarise(&owned_history(rows), NOW, &Thresholds::default());
747 let stale = of(&reports, "pre-push-go-test");
748 assert!(kinds(stale).contains(&FlagKind::Stale), "{:?}", stale.flags);
749 assert!(
750 of(&reports, "pre-push-cargo-test").flags.is_empty(),
751 "the gate that kept running is not stale"
752 );
753 }
754
755 #[test]
758 fn too_little_history_abstains_out_loud() {
759 let rows = [
760 (
761 "tree0",
762 run(
763 NOW - 2 * DAY,
764 "pre-push-cargo-test",
765 RunOutcome::Passed,
766 600_000,
767 ),
768 ),
769 (
770 "tree1",
771 run(NOW - DAY, "pre-push-cargo-test", RunOutcome::Passed, 300),
772 ),
773 ];
774 let reports = summarise(&history(&rows), NOW, &Thresholds::default());
775 let r = of(&reports, "pre-push-cargo-test");
776 assert_eq!(
777 r.abstained.as_deref(),
778 Some("insufficient history (2 verdicts)")
779 );
780 assert!(
781 r.flags.is_empty(),
782 "a two-run history must not raise a flag: {:?}",
783 r.flags
784 );
785 assert_eq!((r.runs, r.passed), (2, 2), "the counts are still reported");
786 }
787
788 #[test]
792 fn an_unavailable_run_is_never_counted_as_a_pass() {
793 let rows = [
794 (
795 "tree0",
796 run(
797 NOW - 2 * DAY,
798 "pre-push-audit-go",
799 RunOutcome::Unavailable,
800 200,
801 ),
802 ),
803 (
804 "tree1",
805 run(NOW - DAY, "pre-push-audit-go", RunOutcome::Unavailable, 210),
806 ),
807 ];
808 let reports = summarise(&history(&rows), NOW, &Thresholds::default());
809 let r = of(&reports, "pre-push-audit-go");
810 assert_eq!((r.passed, r.failed, r.unavailable), (0, 0, 2));
811 }
812
813 #[test]
815 fn the_window_excludes_what_is_older_than_it() {
816 let rows = [
817 (
818 "tree0",
819 run(
820 NOW - 200 * DAY,
821 "pre-push-cargo-test",
822 RunOutcome::Failed,
823 500,
824 ),
825 ),
826 (
827 "tree1",
828 run(NOW - DAY, "pre-push-cargo-test", RunOutcome::Passed, 500),
829 ),
830 ];
831 let reports = summarise(&history(&rows), NOW, &Thresholds::default());
832 assert_eq!(of(&reports, "pre-push-cargo-test").runs, 1);
833 }
834
835 #[test]
839 fn ordering_prefers_failures_per_second_not_failure_rate() {
840 let gates: Vec<String> = ["suite", "audit"].iter().map(|s| s.to_string()).collect();
841 let mut rows: Vec<(&str, Run)> = Vec::new();
842 for i in 0..6u64 {
843 rows.push((
844 "tree0",
845 run(
846 NOW - (10 - i) * DAY,
847 "suite",
848 if i < 2 {
849 RunOutcome::Failed
850 } else {
851 RunOutcome::Passed
852 },
853 1_200_000,
854 ),
855 ));
856 rows.push((
857 "tree0",
858 run(
859 NOW - (10 - i) * DAY,
860 "audit",
861 if i < 1 {
862 RunOutcome::Failed
863 } else {
864 RunOutcome::Passed
865 },
866 5_000,
867 ),
868 ));
869 }
870 let order = order_by_evidence(&gates, &history(&rows), NOW, 90);
871 assert_eq!(order, vec![1, 0], "the cheap audit runs first");
872 }
873
874 #[test]
877 fn with_no_history_the_declared_order_is_kept() {
878 let gates: Vec<String> = ["a", "b", "c"].iter().map(|s| s.to_string()).collect();
879 assert_eq!(
880 order_by_evidence(&gates, &History::default(), NOW, 90),
881 vec![0, 1, 2]
882 );
883 }
884
885 #[test]
888 fn only_gates_that_actually_failed_are_promoted() {
889 let gates: Vec<String> = ["a", "b", "c"].iter().map(|s| s.to_string()).collect();
890 let rows = [
891 ("t", run(NOW - 3 * DAY, "a", RunOutcome::Passed, 1_000)),
892 ("t", run(NOW - 3 * DAY, "b", RunOutcome::Passed, 1_000)),
893 ("t", run(NOW - 2 * DAY, "c", RunOutcome::Failed, 1_000)),
894 ("t", run(NOW - DAY, "c", RunOutcome::Passed, 1_000)),
895 ];
896 assert_eq!(
897 order_by_evidence(&gates, &history(&rows), NOW, 90),
898 vec![2, 0, 1]
899 );
900 }
901
902 #[test]
905 fn ordering_is_a_permutation_and_never_drops_a_gate() {
906 let gates: Vec<String> = ["a", "b", "c", "d"].iter().map(|s| s.to_string()).collect();
907 let rows = [
908 ("t", run(NOW - DAY, "b", RunOutcome::Failed, 10)),
909 ("t", run(NOW - DAY, "d", RunOutcome::Failed, 10_000)),
910 ];
911 let mut order = order_by_evidence(&gates, &history(&rows), NOW, 90);
912 assert_eq!(order.len(), gates.len());
913 order.sort_unstable();
914 assert_eq!(order, vec![0, 1, 2, 3]);
915 }
916
917 #[test]
919 fn cat_file_batch_output_splits_into_bodies() {
920 let oid = "0123456789abcdef0123456789abcdef01234567";
921 let other = "89abcdef0123456789abcdef0123456789abcdef";
922 let out = format!(
923 "{oid} blob 42\namont-gate-v1 pre-push-cargo-test\nrun 10 pre-push-cargo-test pass 5\n\
924 {other} blob 14\namont-gate-v1\n"
925 );
926 let got = parse_batch(&out);
927 assert_eq!(got.len(), 2);
928 assert_eq!(got[0].0, oid);
929 assert!(got[0].1.contains("run 10"));
930 assert_eq!(got[1].0, other);
931 }
932
933 #[test]
934 fn durations_read_like_durations() {
935 assert_eq!(duration(400), "400 ms");
936 assert_eq!(duration(5_400), "5.4 s");
937 assert_eq!(duration(662_000), "11 m 02 s");
938 }
939}