Skip to content

Commit 501c5c4

Browse files
authored
Merge pull request #1269 from rust-lang/summary-nan
Fix NaN handling in PR summary table
2 parents 6d52897 + 7e8bb10 commit 501c5c4

File tree

1 file changed

+88
-30
lines changed

1 file changed

+88
-30
lines changed

‎site/src/comparison.rs

+88-30
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ impl ComparisonSummary {
327327
.count()
328328
}
329329

330+
pub fn is_empty(&self) -> bool {
331+
self.comparisons.is_empty()
332+
}
333+
330334
fn arithmetic_mean<'a>(
331335
&'a self,
332336
changes: impl Iterator<Item = &'a TestResultComparison>,
@@ -473,7 +477,7 @@ pub fn write_summary_table(
473477

474478
writeln!(
475479
result,
476-
"| mean[^2] | {} | {} | {} | {} | {:.1}% |",
480+
"| mean[^2] | {} | {} | {} | {} | {} |",
477481
render_stat(primary.num_regressions, || Some(
478482
primary.arithmetic_mean_of_regressions()
479483
)),
@@ -486,21 +490,30 @@ pub fn write_summary_table(
486490
render_stat(secondary.num_improvements, || Some(
487491
secondary.arithmetic_mean_of_improvements()
488492
)),
489-
primary.arithmetic_mean_of_changes()
493+
if primary.is_empty() {
494+
"N/A".to_string()
495+
} else {
496+
format!("{:.1}%", primary.arithmetic_mean_of_changes())
497+
}
490498
)
491499
.unwrap();
492500

493-
let largest_change = primary
494-
.most_relevant_changes()
495-
.iter()
496-
.fold(0.0, |accum: f64, item| {
497-
let change = item.map(|v| v.relative_change() * 100.0).unwrap_or(0.0);
498-
accum.max(change)
499-
});
501+
let largest_change = if primary.is_empty() {
502+
"N/A".to_string()
503+
} else {
504+
let change = primary
505+
.most_relevant_changes()
506+
.iter()
507+
.fold(0.0, |accum: f64, item| {
508+
let change = item.map(|v| v.relative_change() * 100.0).unwrap_or(0.0);
509+
accum.max(change)
510+
});
511+
format!("{:.1}%", change)
512+
};
500513

501514
writeln!(
502515
result,
503-
"| max | {} | {} | {} | {} | {:.1}% |",
516+
"| max | {} | {} | {} | {} | {} |",
504517
render_stat(primary.num_regressions, || primary
505518
.largest_regression()
506519
.map(|r| r.relative_change() * 100.0)),
@@ -1353,6 +1366,7 @@ fn compare_link(start: &ArtifactId, end: &ArtifactId) -> String {
13531366

13541367
#[cfg(test)]
13551368
mod tests {
1369+
use collector::category::Category;
13561370
use std::collections::HashSet;
13571371

13581372
use database::{ArtifactId, Profile, Scenario};
@@ -1363,12 +1377,12 @@ mod tests {
13631377
};
13641378

13651379
#[test]
1366-
fn summary_table_only_improvements() {
1380+
fn summary_table_only_improvements_primary() {
13671381
check_table(
13681382
vec![
1369-
("primary", 5.0, 10.0),
1370-
("primary", 5.0, 12.0),
1371-
("primary", 1.0, 3.0),
1383+
(Category::Primary, 5.0, 10.0),
1384+
(Category::Primary, 5.0, 12.0),
1385+
(Category::Primary, 1.0, 3.0),
13721386
],
13731387
r#"
13741388
| | Regressions 😿 <br />(primary) | Regressions 😿 <br />(secondary) | Improvements 🎉 <br />(primary) | Improvements 🎉 <br />(secondary) | All 😿 🎉 <br />(primary) |
@@ -1385,12 +1399,12 @@ mod tests {
13851399
}
13861400

13871401
#[test]
1388-
fn summary_table_only_regressions() {
1402+
fn summary_table_only_regressions_primary() {
13891403
check_table(
13901404
vec![
1391-
("primary", 5.0, 2.0),
1392-
("primary", 5.0, 1.0),
1393-
("primary", 4.0, 1.0),
1405+
(Category::Primary, 5.0, 2.0),
1406+
(Category::Primary, 5.0, 1.0),
1407+
(Category::Primary, 4.0, 1.0),
13941408
],
13951409
r#"
13961410
| | Regressions 😿 <br />(primary) | Regressions 😿 <br />(secondary) | Improvements 🎉 <br />(primary) | Improvements 🎉 <br />(secondary) | All 😿 🎉 <br />(primary) |
@@ -1406,14 +1420,58 @@ mod tests {
14061420
);
14071421
}
14081422

1423+
#[test]
1424+
fn summary_table_only_improvements_secondary() {
1425+
check_table(
1426+
vec![
1427+
(Category::Secondary, 5.0, 2.0),
1428+
(Category::Secondary, 5.0, 1.0),
1429+
(Category::Secondary, 4.0, 1.0),
1430+
],
1431+
r#"
1432+
| | Regressions 😿 <br />(primary) | Regressions 😿 <br />(secondary) | Improvements 🎉 <br />(primary) | Improvements 🎉 <br />(secondary) | All 😿 🎉 <br />(primary) |
1433+
|:---:|:---:|:---:|:---:|:---:|:---:|
1434+
| count[^1] | 0 | 0 | 0 | 3 | 0 |
1435+
| mean[^2] | N/A | N/A | N/A | -71.7% | N/A |
1436+
| max | N/A | N/A | N/A | -80.0% | N/A |
1437+
1438+
[^1]: *number of relevant changes*
1439+
[^2]: *the arithmetic mean of the percent change*
1440+
"#
1441+
.trim_start(),
1442+
);
1443+
}
1444+
1445+
#[test]
1446+
fn summary_table_only_regressions_secondary() {
1447+
check_table(
1448+
vec![
1449+
(Category::Secondary, 5.0, 10.0),
1450+
(Category::Secondary, 5.0, 12.0),
1451+
(Category::Secondary, 1.0, 3.0),
1452+
],
1453+
r#"
1454+
| | Regressions 😿 <br />(primary) | Regressions 😿 <br />(secondary) | Improvements 🎉 <br />(primary) | Improvements 🎉 <br />(secondary) | All 😿 🎉 <br />(primary) |
1455+
|:---:|:---:|:---:|:---:|:---:|:---:|
1456+
| count[^1] | 0 | 3 | 0 | 0 | 0 |
1457+
| mean[^2] | N/A | 146.7% | N/A | N/A | N/A |
1458+
| max | N/A | 200.0% | N/A | N/A | N/A |
1459+
1460+
[^1]: *number of relevant changes*
1461+
[^2]: *the arithmetic mean of the percent change*
1462+
"#
1463+
.trim_start(),
1464+
);
1465+
}
1466+
14091467
#[test]
14101468
fn summary_table_mixed_primary() {
14111469
check_table(
14121470
vec![
1413-
("primary", 10.0, 5.0),
1414-
("primary", 5.0, 10.0),
1415-
("primary", 1.0, 3.0),
1416-
("primary", 4.0, 1.0),
1471+
(Category::Primary, 10.0, 5.0),
1472+
(Category::Primary, 5.0, 10.0),
1473+
(Category::Primary, 1.0, 3.0),
1474+
(Category::Primary, 4.0, 1.0),
14171475
],
14181476
r#"
14191477
| | Regressions 😿 <br />(primary) | Regressions 😿 <br />(secondary) | Improvements 🎉 <br />(primary) | Improvements 🎉 <br />(secondary) | All 😿 🎉 <br />(primary) |
@@ -1433,12 +1491,12 @@ mod tests {
14331491
fn summary_table_mixed_primary_secondary() {
14341492
check_table(
14351493
vec![
1436-
("primary", 10.0, 5.0),
1437-
("primary", 5.0, 10.0),
1438-
("secondary", 5.0, 10.0),
1439-
("primary", 1.0, 3.0),
1440-
("secondary", 3.0, 1.0),
1441-
("primary", 4.0, 1.0),
1494+
(Category::Primary, 10.0, 5.0),
1495+
(Category::Primary, 5.0, 10.0),
1496+
(Category::Secondary, 5.0, 10.0),
1497+
(Category::Primary, 1.0, 3.0),
1498+
(Category::Secondary, 3.0, 1.0),
1499+
(Category::Primary, 4.0, 1.0),
14421500
],
14431501
r#"
14441502
| | Regressions 😿 <br />(primary) | Regressions 😿 <br />(secondary) | Improvements 🎉 <br />(primary) | Improvements 🎉 <br />(secondary) | All 😿 🎉 <br />(primary) |
@@ -1455,11 +1513,11 @@ mod tests {
14551513
}
14561514

14571515
// (category, before, after)
1458-
fn check_table(values: Vec<(&str, f64, f64)>, expected: &str) {
1516+
fn check_table(values: Vec<(Category, f64, f64)>, expected: &str) {
14591517
let mut primary_statistics = HashSet::new();
14601518
let mut secondary_statistics = HashSet::new();
14611519
for (index, (category, before, after)) in values.into_iter().enumerate() {
1462-
let target = if category == "primary" {
1520+
let target = if category == Category::Primary {
14631521
&mut primary_statistics
14641522
} else {
14651523
&mut secondary_statistics

0 commit comments

Comments
 (0)