From d650c684f104b2358a1ce6f327b537a979da0f71 Mon Sep 17 00:00:00 2001 From: Bo Chen Date: Tue, 15 Feb 2022 08:11:58 -0800 Subject: [PATCH] performance-metrics: Add option "--report-file" Signed-off-by: Bo Chen --- Cargo.lock | 1 + performance-metrics/Cargo.toml | 1 + performance-metrics/src/main.rs | 38 ++++++++++++++++++++++++++++----- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a642a8f6c..ccab68ec2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -638,6 +638,7 @@ dependencies = [ "serde_derive", "serde_json", "test_infra", + "thiserror", "wait-timeout", ] diff --git a/performance-metrics/Cargo.toml b/performance-metrics/Cargo.toml index fdd6bbb57..26c43bc16 100644 --- a/performance-metrics/Cargo.toml +++ b/performance-metrics/Cargo.toml @@ -13,6 +13,7 @@ serde = { version = "1.0.136", features = ["rc"] } serde_derive = "1.0.136" serde_json = "1.0.78" test_infra = { path = "../test_infra" } +thiserror = "1.0.30" wait-timeout = "0.2.0" [build-dependencies] diff --git a/performance-metrics/src/main.rs b/performance-metrics/src/main.rs index 0909dfa5a..3847624db 100644 --- a/performance-metrics/src/main.rs +++ b/performance-metrics/src/main.rs @@ -18,11 +18,18 @@ use std::{ thread, time::Duration, }; +use thiserror::Error; -#[derive(Debug)] +#[derive(Error, Debug)] enum Error { + #[error("Error: test timed-out")] TestTimeout, + #[error("Error: test failed")] TestFailed, + #[error("Error creating log file: {0}")] + ReportFileCreation(std::io::Error), + #[error("Error writing log file: {0}")] + ReportFileWrite(std::io::Error), } #[derive(Deserialize, Serialize)] @@ -377,6 +384,12 @@ fn main() { .takes_value(false) .required(false), ) + .arg( + Arg::new("report-file") + .long("report-file") + .help("Report file. Standard error is used if not specified") + .takes_value(true), + ) .get_matches(); if cmd_arguments.is_present("list-tests") { @@ -393,6 +406,17 @@ fn main() { None => Vec::new(), }; + let mut report_file: Box = + if let Some(file) = cmd_arguments.value_of("report-file") { + Box::new( + std::fs::File::create(std::path::Path::new(file)) + .map_err(Error::ReportFileCreation) + .unwrap(), + ) + } else { + Box::new(std::io::stderr()) + }; + // Run performance tests sequentially and report results (in both readable/json format) let mut metrics_report = MetricsReport { git_human_readable: env!("GIT_HUMAN_READABLE").to_string(), @@ -421,8 +445,12 @@ fn main() { cleanup_tests(); // Todo: Report/upload to the metrics database - println!( - "\n\nTests result in json format: \n {}", - serde_json::to_string_pretty(&metrics_report).unwrap() - ); + report_file + .write( + serde_json::to_string_pretty(&metrics_report) + .unwrap() + .as_bytes(), + ) + .map_err(Error::ReportFileWrite) + .unwrap(); }