From d5f053dabfadb2a62a4cc6b89a6b7bf64ce3c460 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 23 Feb 2023 12:59:23 +0100 Subject: [PATCH] feat(bench): change --json output format (#17888) Closes https://github.com/denoland/deno/issues/17775 --- cli/tests/integration/bench_tests.rs | 6 +++ cli/tests/testdata/bench/pass.json.out | 28 ++++++++++++ cli/tools/bench.rs | 63 ++++++++++++++------------ 3 files changed, 69 insertions(+), 28 deletions(-) create mode 100644 cli/tests/testdata/bench/pass.json.out diff --git a/cli/tests/integration/bench_tests.rs b/cli/tests/integration/bench_tests.rs index 7953bef343..15a86ee9d0 100644 --- a/cli/tests/integration/bench_tests.rs +++ b/cli/tests/integration/bench_tests.rs @@ -178,6 +178,12 @@ itest!(bench_with_malformed_config { output: "bench/collect_with_malformed_config.out", }); +itest!(json_output { + args: "bench --json bench/pass.ts", + exit_code: 0, + output: "bench/pass.json.out", +}); + #[test] fn recursive_permissions_pledge() { let output = util::deno_cmd() diff --git a/cli/tests/testdata/bench/pass.json.out b/cli/tests/testdata/bench/pass.json.out new file mode 100644 index 0000000000..73daa72028 --- /dev/null +++ b/cli/tests/testdata/bench/pass.json.out @@ -0,0 +1,28 @@ +Check file:///[WILDCARD]testdata/bench/pass.ts +{ + "runtime": "Deno/[WILDCARD]", + "cpu": "[WILDCARD]", + "benches": [ + { + "origin": "file:///[WILDCARD]testdata/bench/pass.ts", + "group": null, + "name": "bench0", + "baseline": false, + "results": [ + { + "ok": { + "n": [WILDCARD], + "min": [WILDCARD], + "max": [WILDCARD], + "avg": [WILDCARD], + "p75": [WILDCARD], + "p99": [WILDCARD], + "p995": [WILDCARD], + "p999": [WILDCARD] + } + } + ] + }, +[WILDCARD] + ] +} diff --git a/cli/tools/bench.rs b/cli/tools/bench.rs index cd6e91c9f6..578b72adfb 100644 --- a/cli/tools/bench.rs +++ b/cli/tools/bench.rs @@ -133,41 +133,37 @@ pub trait BenchReporter { } #[derive(Debug, Serialize)] -struct JsonReporterResult { +struct JsonReporterOutput { runtime: String, cpu: String, - origin: String, - group: Option, - name: String, - baseline: bool, - result: BenchResult, + benches: Vec, } -impl JsonReporterResult { - fn new( - origin: String, - group: Option, - name: String, - baseline: bool, - result: BenchResult, - ) -> Self { +impl Default for JsonReporterOutput { + fn default() -> Self { Self { runtime: format!("{} {}", get_user_agent(), env!("TARGET")), cpu: mitata::cpu::name(), - origin, - group, - name, - baseline, - result, + benches: vec![], } } } #[derive(Debug, Serialize)] -struct JsonReporter(Vec); +struct JsonReporterBench { + origin: String, + group: Option, + name: String, + baseline: bool, + results: Vec, +} + +#[derive(Debug, Serialize)] +struct JsonReporter(JsonReporterOutput); + impl JsonReporter { fn new() -> Self { - Self(vec![]) + Self(Default::default()) } } @@ -190,13 +186,24 @@ impl BenchReporter for JsonReporter { fn report_output(&mut self, _output: &str) {} fn report_result(&mut self, desc: &BenchDescription, result: &BenchResult) { - self.0.push(JsonReporterResult::new( - desc.origin.clone(), - desc.group.clone(), - desc.name.clone(), - desc.baseline, - result.clone(), - )); + let maybe_bench = self.0.benches.iter_mut().find(|bench| { + bench.origin == desc.origin + && bench.group == desc.group + && bench.name == desc.name + && bench.baseline == desc.baseline + }); + + if let Some(bench) = maybe_bench { + bench.results.push(result.clone()); + } else { + self.0.benches.push(JsonReporterBench { + origin: desc.origin.clone(), + group: desc.group.clone(), + name: desc.name.clone(), + baseline: desc.baseline, + results: vec![result.clone()], + }); + } } }