Skip to content

Commit cf3c7ae

Browse files
authored
feat: improve results display when running locally (#136)
1 parent 4134736 commit cf3c7ae

3 files changed

Lines changed: 122 additions & 7 deletions

File tree

Cargo.lock

Lines changed: 49 additions & 1 deletion
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
@@ -60,6 +60,7 @@ shellexpand = { version = "3.1.1", features = ["tilde"] }
6060
addr2line = "0.25"
6161
gimli = "0.32"
6262
open = "5.3.2"
63+
tabled = "0.17"
6364

6465
[target.'cfg(target_os = "linux")'.dependencies]
6566
procfs = "0.17.0"

src/run/poll_results.rs

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::time::Duration;
22

33
use console::style;
4+
use tabled::settings::Style;
5+
use tabled::{Table, Tabled};
46
use tokio::time::{Instant, sleep};
57

68
use crate::api_client::{
@@ -14,6 +16,26 @@ use super::run_environment::RunEnvironmentProvider;
1416
const RUN_PROCESSING_MAX_DURATION: Duration = Duration::from_secs(60 * 5); // 5 minutes
1517
const POLLING_INTERVAL: Duration = Duration::from_secs(1);
1618

19+
#[derive(Tabled)]
20+
struct BenchmarkRow {
21+
#[tabled(rename = "Benchmark")]
22+
name: String,
23+
#[tabled(rename = "Time")]
24+
time: String,
25+
}
26+
27+
fn build_benchmark_table(results: &[crate::api_client::FetchLocalRunBenchmarkResult]) -> String {
28+
let table_rows: Vec<BenchmarkRow> = results
29+
.iter()
30+
.map(|result| BenchmarkRow {
31+
name: result.benchmark.name.clone(),
32+
time: helpers::format_duration(result.time, Some(2)),
33+
})
34+
.collect();
35+
36+
Table::new(&table_rows).with(Style::modern()).to_string()
37+
}
38+
1739
#[allow(clippy::borrowed_box)]
1840
pub async fn poll_results(
1941
api_client: &CodSpeedAPIClient,
@@ -100,21 +122,65 @@ pub async fn poll_results(
100122

101123
if !response.run.results.is_empty() {
102124
start_group!("Benchmark results");
103-
for result in response.run.results {
104-
let benchmark_name = result.benchmark.name;
105-
let time = helpers::format_duration(result.time, Some(2));
106125

107-
info!("{}: {}", benchmark_name, style(time).bold());
126+
let table = build_benchmark_table(&response.run.results);
127+
info!("\n{table}");
108128

109-
if output_json {
129+
if output_json {
130+
for result in response.run.results {
110131
log_json!(format!(
111132
"{{\"event\": \"benchmark_ran\", \"name\": \"{}\", \"time\": \"{}\"}}",
112-
benchmark_name, result.time,
133+
result.benchmark.name, result.time,
113134
));
114135
}
115136
}
137+
116138
end_group!();
117139
}
118140

119141
Ok(())
120142
}
143+
144+
#[cfg(test)]
145+
mod tests {
146+
use super::*;
147+
use crate::api_client::{FetchLocalRunBenchmark, FetchLocalRunBenchmarkResult};
148+
149+
#[test]
150+
fn test_benchmark_table_formatting() {
151+
let results = vec![
152+
FetchLocalRunBenchmarkResult {
153+
benchmark: FetchLocalRunBenchmark {
154+
name: "benchmark_fast".to_string(),
155+
},
156+
time: 0.001234, // 1.23 ms
157+
},
158+
FetchLocalRunBenchmarkResult {
159+
benchmark: FetchLocalRunBenchmark {
160+
name: "benchmark_slow".to_string(),
161+
},
162+
time: 1.5678, // 1.57 s
163+
},
164+
FetchLocalRunBenchmarkResult {
165+
benchmark: FetchLocalRunBenchmark {
166+
name: "benchmark_medium".to_string(),
167+
},
168+
time: 0.000567, // 567 µs
169+
},
170+
];
171+
172+
let table = build_benchmark_table(&results);
173+
174+
insta::assert_snapshot!(table, @r###"
175+
┌──────────────────┬───────────┐
176+
│ Benchmark │ Time │
177+
├──────────────────┼───────────┤
178+
│ benchmark_fast │ 1.23 ms │
179+
├──────────────────┼───────────┤
180+
│ benchmark_slow │ 1.57 s │
181+
├──────────────────┼───────────┤
182+
│ benchmark_medium │ 567.00 µs │
183+
└──────────────────┴───────────┘
184+
"###);
185+
}
186+
}

0 commit comments

Comments
 (0)