Skip to content

Commit

Permalink
feat(bench): change --json output format (denoland#17888)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Feb 23, 2023
1 parent b15f9e6 commit d5f053d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 28 deletions.
6 changes: 6 additions & 0 deletions cli/tests/integration/bench_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
28 changes: 28 additions & 0 deletions cli/tests/testdata/bench/pass.json.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Check file:https:///[WILDCARD]testdata/bench/pass.ts
{
"runtime": "Deno/[WILDCARD]",
"cpu": "[WILDCARD]",
"benches": [
{
"origin": "file:https:///[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]
]
}
63 changes: 35 additions & 28 deletions cli/tools/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand All @@ -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()],
});
}
}
}

Expand Down

0 comments on commit d5f053d

Please sign in to comment.