Skip to content

Commit a35637a

Browse files
refactor: consolidate poll_results into upload module
Merge the two separate `poll_results` implementations from `cli/exec` and `cli/run` into a single `upload::poll_results` function controlled by `PollResultsOptions`. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent b4296d1 commit a35637a

6 files changed

Lines changed: 111 additions & 107 deletions

File tree

src/cli/exec/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ use crate::prelude::*;
77
use crate::project_config::ProjectConfig;
88
use crate::project_config::merger::ConfigMerger;
99
use crate::upload::UploadResult;
10+
use crate::upload::poll_results::{PollResultsOptions, poll_results};
1011
use clap::Args;
1112
use std::path::Path;
1213

1314
pub mod multi_targets;
14-
mod poll_results;
1515

1616
/// We temporarily force this name for all exec runs
1717
pub const DEFAULT_REPOSITORY_NAME: &str = "local-runs";
@@ -120,8 +120,9 @@ pub async fn execute_with_harness(
120120
)
121121
.await?;
122122

123+
let poll_opts = PollResultsOptions::for_exec();
123124
let poll_results_fn = async |upload_result: &UploadResult| {
124-
poll_results::poll_results(api_client, upload_result).await
125+
poll_results(api_client, upload_result, &poll_opts).await
125126
};
126127

127128
executor::execute_benchmarks(

src/cli/exec/poll_results.rs

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/cli/run/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ use crate::project_config::ProjectConfig;
88
use crate::project_config::merger::ConfigMerger;
99
use crate::run_environment::interfaces::RepositoryProvider;
1010
use crate::upload::UploadResult;
11+
use crate::upload::poll_results::{PollResultsOptions, poll_results};
1112
use clap::{Args, ValueEnum};
1213
use std::path::Path;
1314

1415
pub mod helpers;
1516
pub mod logger;
16-
mod poll_results;
1717

1818
#[derive(Args, Debug)]
1919
pub struct RunArgs {
@@ -155,8 +155,9 @@ pub async fn run(
155155
// Execute benchmarks
156156
let executor = executor::get_executor_from_mode(&execution_context.config.mode);
157157

158+
let poll_opts = PollResultsOptions::for_run(output_json);
158159
let poll_results_fn = async |upload_result: &UploadResult| {
159-
poll_results::poll_results(api_client, upload_result, output_json).await
160+
poll_results(api_client, upload_result, &poll_opts).await
160161
};
161162
executor::execute_benchmarks(
162163
executor.as_ref(),

src/cli/run/poll_results.rs

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/upload/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod interfaces;
2+
pub mod poll_results;
23
mod polling;
34
mod profile_archive;
45
mod run_index_state;

src/upload/poll_results.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
use console::style;
2+
3+
use crate::api_client::CodSpeedAPIClient;
4+
use crate::cli::run::helpers::benchmark_display::{build_benchmark_table, build_detailed_summary};
5+
use crate::prelude::*;
6+
7+
use super::{UploadResult, poll_run_report};
8+
9+
/// Options controlling poll_results display behavior.
10+
pub struct PollResultsOptions {
11+
/// If true, show impact percentage (used by `codspeed run`)
12+
pub show_impact: bool,
13+
/// If true, output JSON events (used by `codspeed run --message-format json`)
14+
pub output_json: bool,
15+
/// If true, show detailed summary for single benchmark result (used by `codspeed exec`)
16+
pub detailed_single: bool,
17+
}
18+
19+
impl PollResultsOptions {
20+
/// Options for `codspeed run`
21+
pub fn for_run(output_json: bool) -> Self {
22+
Self {
23+
show_impact: true,
24+
output_json,
25+
detailed_single: false,
26+
}
27+
}
28+
29+
/// Options for `codspeed exec`
30+
pub fn for_exec() -> Self {
31+
Self {
32+
show_impact: false,
33+
output_json: false,
34+
detailed_single: true,
35+
}
36+
}
37+
}
38+
39+
pub async fn poll_results(
40+
api_client: &CodSpeedAPIClient,
41+
upload_result: &UploadResult,
42+
options: &PollResultsOptions,
43+
) -> Result<()> {
44+
let response = poll_run_report(api_client, upload_result).await?;
45+
46+
if options.show_impact {
47+
let report = response.run.head_reports.into_iter().next();
48+
if let Some(report) = report {
49+
if let Some(impact) = report.impact {
50+
let rounded_impact = (impact * 100.0).round();
51+
let impact_text = if impact > 0.0 {
52+
style(format!("+{rounded_impact}%")).green().bold()
53+
} else {
54+
style(format!("{rounded_impact}%")).red().bold()
55+
};
56+
57+
info!(
58+
"Impact: {} (allowed regression: -{}%)",
59+
impact_text,
60+
(response.allowed_regression * 100.0).round()
61+
);
62+
} else {
63+
info!("No impact detected, reason: {}", report.conclusion);
64+
}
65+
}
66+
}
67+
68+
if options.output_json {
69+
log_json!(format!(
70+
"{{\"event\": \"run_finished\", \"run_id\": \"{}\"}}",
71+
upload_result.run_id
72+
));
73+
}
74+
75+
if !response.run.results.is_empty() {
76+
end_group!();
77+
start_group!("Benchmark results");
78+
79+
if options.detailed_single && response.run.results.len() == 1 {
80+
let summary = build_detailed_summary(&response.run.results[0]);
81+
info!("\n{summary}");
82+
} else {
83+
let table = build_benchmark_table(&response.run.results);
84+
info!("\n{table}");
85+
}
86+
87+
if options.output_json {
88+
for result in &response.run.results {
89+
log_json!(format!(
90+
"{{\"event\": \"benchmark_ran\", \"name\": \"{}\", \"time\": \"{}\"}}",
91+
result.benchmark.name, result.value
92+
));
93+
}
94+
}
95+
96+
info!(
97+
"\nTo see the full report, visit: {}",
98+
style(response.run.url).blue().bold().underlined()
99+
);
100+
end_group!();
101+
}
102+
103+
Ok(())
104+
}

0 commit comments

Comments
 (0)