Skip to content

Commit e0cc45d

Browse files
feat: output information about benches after a local run
1 parent 8350914 commit e0cc45d

6 files changed

Lines changed: 76 additions & 37 deletions

File tree

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ sysinfo = { version = "0.33.1", features = ["serde"] }
4646
indicatif = "0.17.8"
4747
console = "0.15.8"
4848
async-trait = "0.1.82"
49+
humantime = "2.2.0"
4950

5051
[dev-dependencies]
5152
temp-env = { version = "0.3.6", features = ["async_closure"] }

src/api_client.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,14 @@ impl Display for ReportConclusion {
108108

109109
#[derive(Debug, Deserialize, Serialize)]
110110
#[serde(rename_all = "camelCase")]
111-
pub struct FetchLocalRunReportHeadReport {
111+
pub struct FetchLocalRunReportRun {
112112
pub id: String,
113-
pub impact: Option<f64>,
114-
pub conclusion: ReportConclusion,
113+
pub status: RunStatus,
114+
pub url: String,
115+
pub head_reports: Vec<FetchLocalRunReportHeadReport>,
116+
pub results: Vec<FetchLocalRunBenchmarkResult>,
115117
}
118+
116119
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
117120
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
118121
pub enum RunStatus {
@@ -121,14 +124,26 @@ pub enum RunStatus {
121124
Pending,
122125
Processing,
123126
}
127+
124128
#[derive(Debug, Deserialize, Serialize)]
125129
#[serde(rename_all = "camelCase")]
126-
pub struct FetchLocalRunReportRun {
130+
pub struct FetchLocalRunReportHeadReport {
127131
pub id: String,
128-
pub status: RunStatus,
129-
pub url: String,
130-
pub head_reports: Vec<FetchLocalRunReportHeadReport>,
132+
pub impact: Option<f64>,
133+
pub conclusion: ReportConclusion,
134+
}
135+
136+
#[derive(Debug, Deserialize, Serialize)]
137+
pub struct FetchLocalRunBenchmarkResult {
138+
pub time: f64,
139+
pub benchmark: FetchLocalRunBenchmark,
131140
}
141+
142+
#[derive(Debug, Deserialize, Serialize)]
143+
pub struct FetchLocalRunBenchmark {
144+
pub name: String,
145+
}
146+
132147
nest! {
133148
#[derive(Debug, Deserialize, Serialize)]*
134149
#[serde(rename_all = "camelCase")]*
@@ -137,7 +152,7 @@ nest! {
137152
settings: struct FetchLocalRunReportSettings {
138153
allowed_regression: f64,
139154
},
140-
runs: Vec<FetchLocalRunReportRun>,
155+
run: FetchLocalRunReportRun,
141156
}
142157
}
143158
}
@@ -190,22 +205,10 @@ impl CodSpeedAPIClient {
190205
)
191206
.await;
192207
match response {
193-
Ok(response) => {
194-
let allowed_regression = response.repository.settings.allowed_regression;
195-
196-
match response.repository.runs.into_iter().next() {
197-
Some(run) => Ok(FetchLocalRunReportResponse {
198-
allowed_regression,
199-
run,
200-
}),
201-
None => bail!(
202-
"No runs found for owner: {}, name: {}, run_id: {}",
203-
vars.owner,
204-
vars.name,
205-
vars.run_id
206-
),
207-
}
208-
}
208+
Ok(response) => Ok(FetchLocalRunReportResponse {
209+
allowed_regression: response.repository.settings.allowed_regression,
210+
run: response.repository.run,
211+
}),
209212
Err(err) if err.contains_error_code("UNAUTHENTICATED") => {
210213
bail!("Your session has expired, please login again using `codspeed auth login`")
211214
}

src/queries/FetchLocalRunReport.gql

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@ query FetchLocalRunReport($owner: String!, $name: String!, $runId: String!) {
33
settings {
44
allowedRegression
55
}
6-
runs(where: { id: { equals: $runId } }) {
7-
id
6+
run(id: $runId) {
7+
id
88
status
99
url
1010
headReports {
1111
id
1212
impact
1313
conclusion
1414
}
15+
results {
16+
time
17+
benchmark {
18+
name
19+
}
20+
}
1521
}
1622
}
1723
}

src/run/mod.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,8 @@ pub async fn run(
206206
end_group!();
207207

208208
if provider.get_run_environment() == RunEnvironment::Local {
209-
start_group!("Fetching the results");
210-
let run_id = upload_result.run_id.clone();
211-
poll_results::poll_results(api_client, &provider, upload_result.run_id).await?;
212-
if output_json {
213-
// TODO: Refactor `log_json` to avoid having to format the json manually
214-
// We could make use of structured logging for this https://docs.rs/log/latest/log/#structured-logging
215-
log_json!(format!(
216-
"{{\"event\": \"run_finished\", \"run_id\": \"{}\"}}",
217-
run_id
218-
));
219-
}
209+
poll_results::poll_results(api_client, &provider, upload_result.run_id, output_json)
210+
.await?;
220211
end_group!();
221212
}
222213
}

src/run/poll_results.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub async fn poll_results(
1818
api_client: &CodSpeedAPIClient,
1919
provider: &Box<dyn RunEnvironmentProvider>,
2020
run_id: String,
21+
output_json: bool,
2122
) -> Result<()> {
2223
let start = Instant::now();
2324
let run_environment_metadata = provider.get_run_environment_metadata()?;
@@ -29,6 +30,7 @@ pub async fn poll_results(
2930
run_id: run_id.clone(),
3031
};
3132

33+
start_group!("Fetching the results");
3234
let response;
3335
loop {
3436
if start.elapsed() > RUN_PROCESSING_MAX_DURATION {
@@ -84,5 +86,34 @@ pub async fn poll_results(
8486
style(response.run.url).blue().bold().underlined()
8587
);
8688

89+
if output_json {
90+
// TODO: Refactor `log_json` to avoid having to format the json manually
91+
// We could make use of structured logging for this https://docs.rs/log/latest/log/#structured-logging
92+
log_json!(format!(
93+
"{{\"event\": \"run_finished\", \"run_id\": \"{}\"}}",
94+
run_id
95+
));
96+
}
97+
98+
end_group!();
99+
100+
if !response.run.results.is_empty() {
101+
start_group!("Benchmarks");
102+
for result in response.run.results {
103+
let benchmark_name = result.benchmark.name;
104+
let time: humantime::Duration = Duration::from_secs_f64(result.time).into();
105+
let time_text = style(format!("{}", time)).bold();
106+
107+
if output_json {
108+
log_json!(format!(
109+
"{{\"event\": \"benchmark_ran\", \"name\": \"{}\", \"time\": \"{}\"}}",
110+
benchmark_name, result.time,
111+
));
112+
}
113+
info!("{} - Time: {}", benchmark_name, time_text);
114+
}
115+
end_group!();
116+
}
117+
87118
Ok(())
88119
}

0 commit comments

Comments
 (0)