From 0862064fd2043cda02c026ea217eaa49449d75f0 Mon Sep 17 00:00:00 2001 From: Bo Chen Date: Thu, 10 Feb 2022 18:02:03 -0800 Subject: [PATCH] performance-metrics: Generate a summary report in Json The report contains the test results with commit hash and date. Signed-off-by: Bo Chen --- Cargo.lock | 1 + performance-metrics/Cargo.toml | 4 ++++ performance-metrics/build.rs | 36 +++++++++++++++++++++++++++++++++ performance-metrics/src/main.rs | 36 ++++++++++++++++++++++++++------- 4 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 performance-metrics/build.rs diff --git a/Cargo.lock b/Cargo.lock index 9e1b27579..a642a8f6c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -631,6 +631,7 @@ dependencies = [ name = "performance-metrics" version = "0.1.0" dependencies = [ + "clap", "dirs 4.0.0", "lazy_static", "serde", diff --git a/performance-metrics/Cargo.toml b/performance-metrics/Cargo.toml index 565449660..8313e33f0 100644 --- a/performance-metrics/Cargo.toml +++ b/performance-metrics/Cargo.toml @@ -3,6 +3,7 @@ name = "performance-metrics" version = "0.1.0" authors = ["The Cloud Hypervisor Authors"] edition = "2018" +build = "build.rs" [dependencies] dirs = "4.0.0" @@ -12,3 +13,6 @@ serde_derive = "1.0.136" serde_json = "1.0.78" test_infra = { path = "../test_infra" } wait-timeout = "0.2.0" + +[build-dependencies] +clap = { version = "3.0.14", features = ["wrap_help"] } diff --git a/performance-metrics/build.rs b/performance-metrics/build.rs new file mode 100644 index 000000000..a13f3ee01 --- /dev/null +++ b/performance-metrics/build.rs @@ -0,0 +1,36 @@ +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 +// + +#[macro_use(crate_version)] +extern crate clap; + +use std::process::Command; + +fn main() { + let mut git_human_readable = "v".to_owned() + crate_version!(); + if let Ok(git_out) = Command::new("git").args(&["describe", "--dirty"]).output() { + if git_out.status.success() { + if let Ok(git_out_str) = String::from_utf8(git_out.stdout) { + git_human_readable = git_out_str; + } + } + } + + let mut git_revision = "".to_string(); + if let Ok(git_out) = Command::new("git").args(&["rev-parse", "HEAD"]).output() { + if git_out.status.success() { + if let Ok(git_out_str) = String::from_utf8(git_out.stdout) { + git_revision = git_out_str; + } + } + } + + // This println!() has a special behavior, as it will set the environment + // variable GIT_human_readable, so that it can be reused from the binary. + // Particularly, this is used from the main.rs to display the exact + // version information. + println!("cargo:rustc-env=GIT_HUMAN_READABLE={}", git_human_readable); + println!("cargo:rustc-env=GIT_REVISION={}", git_revision); +} diff --git a/performance-metrics/src/main.rs b/performance-metrics/src/main.rs index cf2ad596d..37d1074d6 100644 --- a/performance-metrics/src/main.rs +++ b/performance-metrics/src/main.rs @@ -31,6 +31,14 @@ pub struct PerformanceTestResult { min: f64, } +#[derive(Deserialize, Serialize)] +pub struct MetricsReport { + pub git_human_readable: String, + pub git_revision: String, + pub date: String, + pub results: Vec, +} + pub struct PerformanceTestControl { test_time: u32, test_iterations: u32, @@ -307,8 +315,8 @@ lazy_static! { }; } -fn run_test_with_timetout(test: &'static PerformanceTest) -> Result { - let (sender, receiver) = channel::>(); +fn run_test_with_timetout(test: &'static PerformanceTest) -> Result { + let (sender, receiver) = channel::>(); thread::spawn(move || { println!("Test '{}' running .. ({})", test.name, test.control); @@ -318,7 +326,7 @@ fn run_test_with_timetout(test: &'static PerformanceTest) -> Result Err(Error::TestFailed), }; @@ -339,18 +347,29 @@ fn run_test_with_timetout(test: &'static PerformanceTest) -> Result String { + let output = test_infra::exec_host_command_output("date"); + String::from_utf8_lossy(&output.stdout).trim().to_string() +} + fn main() { let test_filter = env::var("TEST_FILTER").map_or("".to_string(), |o| o); + // 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(), + git_revision: env!("GIT_REVISION").to_string(), + date: date(), + results: Vec::new(), + }; + init_tests(); - // Run performance tests sequentially and report results (in both readable/json format) - let mut json_output = String::new(); for test in TEST_LIST.iter() { if test.name.contains(&test_filter) { match run_test_with_timetout(test) { Ok(r) => { - json_output.push_str(&r); + metrics_report.results.push(r); } Err(e) => { eprintln!("Aborting test due to error: '{:?}'", e); @@ -363,5 +382,8 @@ fn main() { cleanup_tests(); // Todo: Report/upload to the metrics database - println!("\n\nTests result in json format: \n {}", json_output); + println!( + "\n\nTests result in json format: \n {}", + serde_json::to_string_pretty(&metrics_report).unwrap() + ); }