Skip to content

Commit a77669f

Browse files
committed
Refactor stat summaries
1 parent 98b4aba commit a77669f

File tree

1 file changed

+80
-71
lines changed

1 file changed

+80
-71
lines changed

site/src/github.rs

+80-71
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
use crate::api::github::Issue;
22
use crate::comparison::{
3-
deserves_attention_icount, write_summary_table, write_summary_table_footer,
3+
deserves_attention_icount, write_summary_table, write_summary_table_footer, ArtifactComparison,
44
ArtifactComparisonSummary, Direction, Stat,
55
};
66
use crate::load::{Config, SiteCtxt, TryCommit};
77

88
use anyhow::Context as _;
9-
use database::{ArtifactId, Benchmark, QueuedCommit};
9+
use database::{ArtifactId, QueuedCommit};
1010
use reqwest::header::USER_AGENT;
1111
use serde::{Deserialize, Serialize};
1212

13-
use collector::category::Category;
14-
15-
use std::collections::{HashMap, HashSet};
13+
use std::collections::HashSet;
1614

1715
use std::{fmt::Write, sync::Arc, time::Duration};
1816

@@ -565,7 +563,10 @@ pub async fn post_finished(ctxt: &SiteCtxt) {
565563
/// `is_master_commit` is used to differentiate messages for try runs and post-merge runs.
566564
async fn post_comparison_comment(ctxt: &SiteCtxt, commit: QueuedCommit, is_master_commit: bool) {
567565
let pr = commit.pr;
568-
let body = summarize_run(ctxt, commit, is_master_commit).await;
566+
let body = match summarize_run(ctxt, commit, is_master_commit).await {
567+
Ok(message) => message,
568+
Err(error) => error,
569+
};
569570

570571
post_comment(&ctxt.config, pr, body).await;
571572
}
@@ -579,54 +580,43 @@ fn make_comparison_url(commit: &QueuedCommit, stat: Stat) -> String {
579580
)
580581
}
581582

582-
async fn summarize_run(ctxt: &SiteCtxt, commit: QueuedCommit, is_master_commit: bool) -> String {
583+
async fn calculate_stat_comparison(
584+
ctxt: &SiteCtxt,
585+
commit: &QueuedCommit,
586+
stat: Stat,
587+
) -> Result<ArtifactComparison, String> {
588+
match crate::comparison::compare(
589+
collector::Bound::Commit(commit.parent_sha.clone()),
590+
collector::Bound::Commit(commit.sha.clone()),
591+
stat,
592+
ctxt,
593+
)
594+
.await
595+
{
596+
Ok(Some(c)) => Ok(c),
597+
_ => Err("ERROR categorizing benchmark run!".to_owned()),
598+
}
599+
}
600+
601+
async fn summarize_run(
602+
ctxt: &SiteCtxt,
603+
commit: QueuedCommit,
604+
is_master_commit: bool,
605+
) -> Result<String, String> {
583606
let benchmark_map = ctxt.get_benchmark_category_map().await;
584607

585608
let mut message = format!(
586609
"Finished benchmarking commit ({sha}): [comparison url]({comparison_url}).
587610
588-
**Summary**: ",
611+
**Summary**:\n\n",
589612
sha = commit.sha,
590613
comparison_url = make_comparison_url(&commit, Stat::Instructions)
591614
);
592615

593-
let mut table_written = false;
594-
let stats = vec![
595-
("Instruction count", Stat::Instructions, false),
596-
("Max RSS (memory usage)", Stat::MaxRSS, true),
597-
("Cycles", Stat::Cycles, true),
598-
];
616+
let inst_comparison = calculate_stat_comparison(ctxt, &commit, Stat::Instructions).await?;
599617

600-
for (title, stat, hidden) in stats {
601-
table_written |= write_stat_summary(
602-
&benchmark_map,
603-
&commit,
604-
ctxt,
605-
&title,
606-
stat,
607-
hidden,
608-
&mut message,
609-
)
610-
.await;
611-
}
612-
613-
if table_written {
614-
write_summary_table_footer(&mut message);
615-
}
616-
617-
let comparison = match crate::comparison::compare(
618-
collector::Bound::Commit(commit.parent_sha.clone()),
619-
collector::Bound::Commit(commit.sha.clone()),
620-
Stat::Instructions,
621-
ctxt,
622-
)
623-
.await
624-
{
625-
Ok(Some(c)) => c,
626-
_ => return String::from("ERROR categorizing benchmark run!"),
627-
};
628-
let errors = if !comparison.newly_failed_benchmarks.is_empty() {
629-
let benchmarks = comparison
618+
let errors = if !inst_comparison.newly_failed_benchmarks.is_empty() {
619+
let benchmarks = inst_comparison
630620
.newly_failed_benchmarks
631621
.iter()
632622
.map(|(benchmark, _)| format!("- {benchmark}"))
@@ -636,46 +626,65 @@ async fn summarize_run(ctxt: &SiteCtxt, commit: QueuedCommit, is_master_commit:
636626
} else {
637627
String::new()
638628
};
639-
let (primary, secondary) = comparison.summarize_by_category(&benchmark_map);
629+
let (inst_primary, inst_secondary) = inst_comparison
630+
.clone()
631+
.summarize_by_category(&benchmark_map);
632+
633+
let mut table_written = false;
634+
let stats = vec![
635+
(
636+
"Instruction count",
637+
Stat::Instructions,
638+
false,
639+
inst_comparison,
640+
),
641+
(
642+
"Max RSS (memory usage)",
643+
Stat::MaxRSS,
644+
true,
645+
calculate_stat_comparison(ctxt, &commit, Stat::MaxRSS).await?,
646+
),
647+
(
648+
"Cycles",
649+
Stat::Cycles,
650+
true,
651+
calculate_stat_comparison(ctxt, &commit, Stat::Cycles).await?,
652+
),
653+
];
654+
655+
for (title, stat, hidden, comparison) in stats {
656+
message.push_str(&format!(
657+
"## [{title}]({})\n",
658+
make_comparison_url(&commit, stat)
659+
));
660+
661+
let (primary, secondary) = comparison.summarize_by_category(&benchmark_map);
662+
table_written |= write_stat_summary(primary, secondary, hidden, &mut message).await;
663+
}
664+
665+
if table_written {
666+
write_summary_table_footer(&mut message);
667+
}
640668

641669
const DISAGREEMENT: &str = "If you disagree with this performance assessment, \
642670
please file an issue in [rust-lang/rustc-perf](https://github.com/rust-lang/rustc-perf/issues/new).";
643671
let footer = format!("{DISAGREEMENT}{errors}");
644672

645-
let direction = primary.direction().or(secondary.direction());
646-
let next_steps = next_steps(primary, secondary, direction, is_master_commit);
673+
let direction = inst_primary.direction().or(inst_secondary.direction());
674+
let next_steps = next_steps(inst_primary, inst_secondary, direction, is_master_commit);
675+
647676
write!(&mut message, "\n{footer}\n{next_steps}").unwrap();
648677

649-
message
678+
Ok(message)
650679
}
651680

681+
/// Returns true if a summary table was written to `message`.
652682
async fn write_stat_summary(
653-
benchmark_map: &HashMap<Benchmark, Category>,
654-
commit: &QueuedCommit,
655-
ctxt: &SiteCtxt,
656-
title: &str,
657-
stat: Stat,
683+
primary: ArtifactComparisonSummary,
684+
secondary: ArtifactComparisonSummary,
658685
hidden: bool,
659686
message: &mut String,
660687
) -> bool {
661-
let comparison = match crate::comparison::compare(
662-
collector::Bound::Commit(commit.parent_sha.clone()),
663-
collector::Bound::Commit(commit.sha.clone()),
664-
stat,
665-
ctxt,
666-
)
667-
.await
668-
{
669-
Ok(Some(c)) => c,
670-
_ => panic!(), //return Err("ERROR categorizing benchmark run!".to_owned()),
671-
};
672-
673-
message.push_str(&format!(
674-
"## [{title}]({})\n",
675-
make_comparison_url(commit, stat)
676-
));
677-
678-
let (primary, secondary) = comparison.summarize_by_category(benchmark_map);
679688
if !primary.is_relevant() && !secondary.is_relevant() {
680689
message
681690
.push_str("This benchmark run did not return any relevant results for this metric.\n");

0 commit comments

Comments
 (0)