1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

feat(bench): change --json output format (#17888)

Closes https://github.com/denoland/deno/issues/17775
This commit is contained in:
Bartek Iwańczuk 2023-02-23 12:59:23 +01:00 committed by GitHub
parent b15f9e60a0
commit d5f053dabf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 28 deletions

View file

@ -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()

28
cli/tests/testdata/bench/pass.json.out vendored Normal file
View file

@ -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]
]
}

View file

@ -133,41 +133,37 @@ pub trait BenchReporter {
}
#[derive(Debug, Serialize)]
struct JsonReporterResult {
struct JsonReporterOutput {
runtime: String,
cpu: String,
origin: String,
group: Option<String>,
name: String,
baseline: bool,
result: BenchResult,
benches: Vec<JsonReporterBench>,
}
impl JsonReporterResult {
fn new(
origin: String,
group: Option<String>,
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<JsonReporterResult>);
struct JsonReporterBench {
origin: String,
group: Option<String>,
name: String,
baseline: bool,
results: Vec<BenchResult>,
}
#[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()],
});
}
}
}