11use std:: time:: Duration ;
22
33use console:: style;
4+ use tabled:: settings:: Style ;
5+ use tabled:: { Table , Tabled } ;
46use tokio:: time:: { Instant , sleep} ;
57
68use crate :: api_client:: {
@@ -14,6 +16,26 @@ use super::run_environment::RunEnvironmentProvider;
1416const RUN_PROCESSING_MAX_DURATION : Duration = Duration :: from_secs ( 60 * 5 ) ; // 5 minutes
1517const 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) ]
1840pub 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