Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ shellexpand = { version = "3.1.1", features = ["tilde"] }
addr2line = "0.25"
gimli = "0.32"
open = "5.3.2"
tabled = "0.17"

[target.'cfg(target_os = "linux")'.dependencies]
procfs = "0.17.0"
Expand Down
78 changes: 72 additions & 6 deletions src/run/poll_results.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::time::Duration;

use console::style;
use tabled::settings::Style;
use tabled::{Table, Tabled};
use tokio::time::{Instant, sleep};

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

#[derive(Tabled)]
struct BenchmarkRow {
#[tabled(rename = "Benchmark")]
name: String,
#[tabled(rename = "Time")]
time: String,
}

fn build_benchmark_table(results: &[crate::api_client::FetchLocalRunBenchmarkResult]) -> String {
let table_rows: Vec<BenchmarkRow> = results
.iter()
.map(|result| BenchmarkRow {
name: result.benchmark.name.clone(),
time: helpers::format_duration(result.time, Some(2)),
})
.collect();

Table::new(&table_rows).with(Style::modern()).to_string()
}

#[allow(clippy::borrowed_box)]
pub async fn poll_results(
api_client: &CodSpeedAPIClient,
Expand Down Expand Up @@ -100,21 +122,65 @@ pub async fn poll_results(

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

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

if output_json {
if output_json {
for result in response.run.results {
log_json!(format!(
"{{\"event\": \"benchmark_ran\", \"name\": \"{}\", \"time\": \"{}\"}}",
benchmark_name, result.time,
result.benchmark.name, result.time,
));
}
}

end_group!();
}

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
use crate::api_client::{FetchLocalRunBenchmark, FetchLocalRunBenchmarkResult};

#[test]
fn test_benchmark_table_formatting() {
let results = vec![
FetchLocalRunBenchmarkResult {
benchmark: FetchLocalRunBenchmark {
name: "benchmark_fast".to_string(),
},
time: 0.001234, // 1.23 ms
},
FetchLocalRunBenchmarkResult {
benchmark: FetchLocalRunBenchmark {
name: "benchmark_slow".to_string(),
},
time: 1.5678, // 1.57 s
},
FetchLocalRunBenchmarkResult {
benchmark: FetchLocalRunBenchmark {
name: "benchmark_medium".to_string(),
},
time: 0.000567, // 567 µs
},
];

let table = build_benchmark_table(&results);

insta::assert_snapshot!(table, @r###"
┌──────────────────┬───────────┐
│ Benchmark │ Time │
├──────────────────┼───────────┤
│ benchmark_fast │ 1.23 ms │
├──────────────────┼───────────┤
│ benchmark_slow │ 1.57 s │
├──────────────────┼───────────┤
│ benchmark_medium │ 567.00 µs │
└──────────────────┴───────────┘
"###);
}
}