Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:///[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]
]
}
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