criterion-markdown 0.3.0

Reads criterion benchmark results and renders a markdown table.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use std::collections::BTreeMap;
use std::fmt::Write;

use crate::model::{BenchEntry, ChangeInfo};
use crate::{Emojis, Thresholds};

/// Formats all benchmark entries into a markdown string.
///
/// When `skip_title` is true, the top-level report heading is omitted
/// because the output is wrapped in a `<details><summary>` tag.
pub(crate) fn format_table(
    entries: &[BenchEntry],
    title: &str,
    skip_title: bool,
    thresholds: Thresholds,
    emojis: &Emojis,
    summary: &SummaryInfo,
) -> String {
    // Group entries by group_id, preserving discovery order
    let mut groups: BTreeMap<&str, Vec<&BenchEntry>> = BTreeMap::new();
    for entry in entries {
        groups.entry(&entry.group_id).or_default().push(entry);
    }
    let show_comparisons = entries.iter().any(|entry| entry.change.is_some());

    let mut out = String::new();
    if !skip_title {
        writeln!(out, "# {title}\n").unwrap();
    }

    write_summary(&mut out, summary);

    writeln!(out, "## Benchmark Results\n").unwrap();

    for (group_id, group_entries) in &groups {
        writeln!(out, "### {group_id}\n").unwrap();
        write_group_table(
            &mut out,
            group_entries,
            thresholds,
            emojis,
            show_comparisons,
        );
        writeln!(out).unwrap();
    }

    out
}

/// Writes a markdown table for a single benchmark group.
fn write_group_table(
    out: &mut String,
    entries: &[&BenchEntry],
    thresholds: Thresholds,
    emojis: &Emojis,
    show_comparisons: bool,
) {
    // Collect unique functions (columns) and values (rows), preserving order
    let mut functions: Vec<&str> = Vec::new();
    let mut values: Vec<Option<&str>> = Vec::new();

    for entry in entries {
        let col = entry.column();
        if !functions.contains(&col) {
            functions.push(col);
        }
        let row = entry.row();
        if !values.contains(&row) {
            values.push(row);
        }
    }

    // Build a lookup: (column, row) -> &BenchEntry
    let mut lookup: BTreeMap<(&str, Option<&str>), &BenchEntry> = BTreeMap::new();
    for entry in entries {
        lookup.insert((entry.column(), entry.row()), entry);
    }

    // Header row
    write!(out, "|").unwrap();
    // Row label column (empty header)
    write!(out, "            ").unwrap();
    for func in &functions {
        write!(out, " | `{func}`").unwrap();
    }
    writeln!(out, " |").unwrap();

    // Alignment row
    write!(out, "|:-----------|").unwrap();
    for _ in &functions {
        write!(out, ":------------------------ |").unwrap();
    }
    writeln!(out).unwrap();

    // Data rows
    for val in &values {
        let row_label = match val {
            Some(v) => format!("**`{v}`**"),
            None => String::new(),
        };
        write!(out, "| {row_label:10} ").unwrap();

        for func in &functions {
            if let Some(&entry) = lookup.get(&(*func, *val)) {
                let time_str = format_time(entry.estimate_ns);
                if show_comparisons {
                    let change_str = format_change(&entry.change, thresholds, emojis);
                    write!(out, " | `{time_str}` ({change_str}) ").unwrap();
                } else {
                    write!(out, " | `{time_str}` ").unwrap();
                }
            } else {
                write!(out, " |                          ").unwrap();
            }
        }
        writeln!(out, " |").unwrap();
    }
}

/// Formats change vs baseline with tiered emojis (matching criterion-table style).
///
/// Uses `compare = baseline / candidate` and the configured thresholds to
/// determine the indicator tier.
fn format_change(change: &Option<ChangeInfo>, thresholds: Thresholds, emojis: &Emojis) -> String {
    let Some(change) = change else {
        return "---".to_string();
    };

    let classification = classify_change(change, thresholds);
    let ChangeClassification::Valid { ratio, kind } = classification else {
        return "⚠ n/a".to_string();
    };

    let speedup_str = if ratio < 1.0 {
        format!("{:.2}x faster", 1.0 / ratio)
    } else if ratio > 1.0 {
        format!("{:.2}x slower", ratio)
    } else {
        format!("{ratio:.2}x")
    };

    match kind {
        ChangeKind::StrongImprovement => {
            format!("{} **{speedup_str}**", emojis.strong_improvement)
        }
        ChangeKind::Improvement => format!("{} **{speedup_str}**", emojis.improvement),
        ChangeKind::Neutral => format!("{} **{speedup_str}**", emojis.stable),
        ChangeKind::Regression => format!("{} *{speedup_str}*", emojis.regression),
    }
}

fn format_compact_change(change: &ChangeInfo, thresholds: Thresholds, emojis: &Emojis) -> String {
    let ChangeClassification::Valid { ratio, kind } = classify_change(change, thresholds) else {
        return "⚠ n/a".to_string();
    };
    let icon = match kind {
        ChangeKind::StrongImprovement => &emojis.strong_improvement,
        ChangeKind::Improvement => &emojis.improvement,
        ChangeKind::Neutral => &emojis.stable,
        ChangeKind::Regression => &emojis.regression,
    };
    let magnitude = if ratio < 1.0 { 1.0 / ratio } else { ratio };
    format!("{icon} {magnitude:.2}x")
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum ChangeKind {
    StrongImprovement,
    Improvement,
    Neutral,
    Regression,
}

enum ChangeClassification {
    Valid { ratio: f64, kind: ChangeKind },
    Invalid,
}

fn classify_change(change: &ChangeInfo, thresholds: Thresholds) -> ChangeClassification {
    let ratio = 1.0 + change.point_estimate;
    if !ratio.is_finite() || ratio <= 0.0 {
        return ChangeClassification::Invalid;
    }

    // A verdict needs the whole interval past the threshold, so a single noisy
    // run cannot announce a change on its own. Where the interval is unusable,
    // the point estimate stands in for both ends.
    let (lower, upper) = match (change.lower_ratio, change.upper_ratio) {
        (lower, upper) if lower.is_finite() && upper.is_finite() && lower > 0.0 => (lower, upper),
        _ => (ratio, ratio),
    };
    let best = 1.0 / lower;
    let worst = 1.0 / upper;

    let kind = if worst >= thresholds.strong_improvement_ratio {
        ChangeKind::StrongImprovement
    } else if worst >= thresholds.improvement_ratio {
        ChangeKind::Improvement
    } else if best > thresholds.regression_ratio {
        ChangeKind::Neutral
    } else {
        ChangeKind::Regression
    };
    ChangeClassification::Valid { ratio, kind }
}

/// Formats a time in nanoseconds to a human-readable string with appropriate units.
fn format_time(ns: f64) -> String {
    if ns < 1_000.0 {
        format!("{:.2} ns", ns)
    } else if ns < 1_000_000.0 {
        format!("{:.2} µs", ns / 1_000.0)
    } else if ns < 1_000_000_000.0 {
        format!("{:.2} ms", ns / 1_000_000.0)
    } else {
        format!("{:.2} s", ns / 1_000_000_000.0)
    }
}

/// A benchmark change shown in the summary.
pub(crate) struct SummaryEntry {
    pub(crate) id: String,
    pub(crate) change: String,
    headline_change: String,
}

/// Summary information for the top improvements and regressions.
pub(crate) struct SummaryInfo {
    pub(crate) gains: Vec<SummaryEntry>,
    pub(crate) regressions: Vec<SummaryEntry>,
    compared_count: usize,
    enabled: bool,
}

impl SummaryInfo {
    pub(crate) fn headline(&self, emojis: &Emojis) -> Option<String> {
        if !self.enabled {
            return None;
        }
        match (self.regressions.first(), self.gains.first()) {
            (Some(regression), Some(gain)) => Some(format!(
                "{} | {}",
                regression.headline_change, gain.headline_change
            )),
            (Some(regression), None) => Some(regression.headline_change.clone()),
            (None, Some(gain)) => Some(gain.headline_change.clone()),
            (None, None) if self.compared_count > 0 => Some(format!("{} stable", emojis.stable)),
            (None, None) => None,
        }
    }
}

/// Computes the top improvements and regressions across all entries.
pub(crate) fn compute_summary(
    entries: &[BenchEntry],
    thresholds: Thresholds,
    emojis: &Emojis,
    limit: usize,
) -> SummaryInfo {
    let compared_count = entries
        .iter()
        .filter(|entry| {
            entry.change.as_ref().is_some_and(|change| {
                matches!(
                    classify_change(change, thresholds),
                    ChangeClassification::Valid { .. }
                )
            })
        })
        .count();
    let mut gains: Vec<&BenchEntry> = entries
        .iter()
        .filter(|entry| {
            entry.change.as_ref().is_some_and(|change| {
                matches!(
                    classify_change(change, thresholds),
                    ChangeClassification::Valid {
                        kind: ChangeKind::Improvement | ChangeKind::StrongImprovement,
                        ..
                    }
                )
            })
        })
        .collect();
    gains.sort_by(|a, b| {
        a.change
            .as_ref()
            .unwrap()
            .point_estimate
            .partial_cmp(&b.change.as_ref().unwrap().point_estimate)
            .unwrap()
    });

    let mut regressions: Vec<&BenchEntry> = entries
        .iter()
        .filter(|entry| {
            entry.change.as_ref().is_some_and(|change| {
                matches!(
                    classify_change(change, thresholds),
                    ChangeClassification::Valid {
                        kind: ChangeKind::Regression,
                        ..
                    }
                )
            })
        })
        .collect();
    regressions.sort_by(|a, b| {
        b.change
            .as_ref()
            .unwrap()
            .point_estimate
            .partial_cmp(&a.change.as_ref().unwrap().point_estimate)
            .unwrap()
    });

    let to_summary_entry = |entry: &BenchEntry| SummaryEntry {
        id: entry.full_id.clone(),
        change: format_change(&entry.change, thresholds, emojis),
        headline_change: format_compact_change(entry.change.as_ref().unwrap(), thresholds, emojis),
    };

    SummaryInfo {
        gains: gains
            .into_iter()
            .take(limit)
            .map(to_summary_entry)
            .collect(),
        regressions: regressions
            .into_iter()
            .take(limit)
            .map(to_summary_entry)
            .collect(),
        compared_count,
        enabled: limit > 0,
    }
}

/// Writes a summary section with the top improvements and regressions.
fn write_summary(out: &mut String, info: &SummaryInfo) {
    if !info.enabled {
        return;
    }

    if info.gains.is_empty() && info.regressions.is_empty() {
        if info.compared_count > 0 {
            writeln!(out, "No benchmark improved or regressed.").unwrap();
            writeln!(out).unwrap();
        }
        return;
    }

    if !info.gains.is_empty() {
        writeln!(out, "## Top improvements\n").unwrap();
        for entry in &info.gains {
            writeln!(out, "- `{}` — {}", entry.id, entry.change).unwrap();
        }
        writeln!(out).unwrap();
    }

    if !info.regressions.is_empty() {
        writeln!(out, "## Top regressions\n").unwrap();
        for entry in &info.regressions {
            writeln!(out, "- `{}` — {}", entry.id, entry.change).unwrap();
        }
        writeln!(out).unwrap();
    }
}

#[cfg(test)]
mod tests {
    use super::{format_compact_change, SummaryEntry, SummaryInfo};
    use crate::model::ChangeInfo;
    use crate::{Emojis, Thresholds};

    #[test]
    fn compact_change_preserves_modest_improvement_icon() {
        let change = ChangeInfo {
            point_estimate: 1.0 / 1.2 - 1.0,
            lower_ratio: 1.0 / 1.2,
            upper_ratio: 1.0 / 1.2,
        };

        assert_eq!(
            format_compact_change(&change, Thresholds::default(), &Emojis::default()),
            "↗️ 1.20x"
        );
    }

    /// A point estimate past the threshold is not enough on its own. The
    /// interval has to clear it too, otherwise a noisy run reports a change.
    #[test]
    fn compact_change_holds_back_a_verdict_the_interval_does_not_support() {
        let decided = ChangeInfo {
            point_estimate: 2.0 - 1.0,
            lower_ratio: 1.9,
            upper_ratio: 2.1,
        };
        let undecided = ChangeInfo {
            point_estimate: 2.0 - 1.0,
            lower_ratio: 0.8,
            upper_ratio: 3.2,
        };
        let thresholds = Thresholds::default();

        let emojis = Emojis::default();
        assert_eq!(
            format_compact_change(&decided, thresholds, &emojis),
            "❌ 2.00x"
        );
        assert_eq!(
            format_compact_change(&undecided, thresholds, &emojis),
            "➖ 2.00x"
        );
    }

    #[test]
    fn headline_labels_regression_and_improvement() {
        let summary = SummaryInfo {
            gains: vec![SummaryEntry {
                id: "faster".to_string(),
                change: "2.00x faster".to_string(),
                headline_change: "🚀 2.00x".to_string(),
            }],
            regressions: vec![SummaryEntry {
                id: "slower".to_string(),
                change: "1.20x slower".to_string(),
                headline_change: "❌ 1.20x".to_string(),
            }],
            compared_count: 2,
            enabled: true,
        };

        assert_eq!(
            summary.headline(&Emojis::default()).as_deref(),
            Some("❌ 1.20x | 🚀 2.00x")
        );
    }

    #[test]
    fn headline_reports_stable_when_all_comparisons_are_neutral() {
        let summary = SummaryInfo {
            gains: Vec::new(),
            regressions: Vec::new(),
            compared_count: 2,
            enabled: true,
        };

        assert_eq!(
            summary.headline(&Emojis::default().stable("⚪")).as_deref(),
            Some("⚪ stable")
        );
    }
}