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: allow to parse generic cargo bench/criterion units #280

Merged
merged 2 commits into from
Dec 9, 2024
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
9 changes: 4 additions & 5 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
}

interface PullRequest {
[key: string]: any;

Check warning on line 32 in src/extract.ts

View workflow job for this annotation

GitHub Actions / Run linting and formatting check

Unexpected any. Specify a different type
number: number;
html_url?: string;
body?: string;
Expand Down Expand Up @@ -313,9 +313,7 @@
function extractCargoResult(output: string): BenchmarkResult[] {
const lines = output.split(/\r?\n/g);
const ret = [];
// Example:
// test bench_fib_20 ... bench: 37,174.25 ns/iter (+/- 7,527.43)
const reExtract = /^test (.+)\s+\.\.\. bench:\s+([0-9,.]+) ns\/iter \(\+\/- ([0-9,.]+)\)$/;
const reExtract = /^test (.+)\s+\.\.\. bench:\s+([0-9,.]+) (\w+\/\w+) \(\+\/- ([0-9,.]+)\)$/;
const reComma = /,/g;

for (const line of lines) {
Expand All @@ -326,13 +324,14 @@

const name = m[1].trim();
const value = parseFloat(m[2].replace(reComma, ''));
const range = m[3].replace(reComma, '');
const unit = m[3].trim();
const range = m[4].replace(reComma, '');

ret.push({
name,
value,
range: `± ${range}`,
unit: 'ns/iter',
unit: unit,
});
}

Expand Down Expand Up @@ -437,7 +436,7 @@
const extra = `mean: ${mean} ${meanUnit}\nrounds: ${stats.rounds}`;
return { name, value, unit, range, extra };
});
} catch (err: any) {

Check warning on line 439 in src/extract.ts

View workflow job for this annotation

GitHub Actions / Run linting and formatting check

Unexpected any. Specify a different type
throw new Error(
`Output file for 'pytest' must be JSON file generated by --benchmark-json option: ${err.message}`,
);
Expand All @@ -448,7 +447,7 @@
let json: GoogleCppBenchmarkJson;
try {
json = JSON.parse(output);
} catch (err: any) {

Check warning on line 450 in src/extract.ts

View workflow job for this annotation

GitHub Actions / Run linting and formatting check

Unexpected any. Specify a different type
throw new Error(
`Output file for 'googlecpp' must be JSON file generated by --benchmark_format=json option: ${err.message}`,
);
Expand Down Expand Up @@ -576,7 +575,7 @@
return ret;
}

function extractJuliaBenchmarkHelper([_, bench]: JuliaBenchmarkGroup, labels: string[] = []): BenchmarkResult[] {

Check warning on line 578 in src/extract.ts

View workflow job for this annotation

GitHub Actions / Run linting and formatting check

'_' is defined but never used
const res: BenchmarkResult[] = [];
for (const key in bench.data) {
const value = bench.data[key];
Expand Down Expand Up @@ -607,7 +606,7 @@
let json: JuliaBenchmarkJson;
try {
json = JSON.parse(output);
} catch (err: any) {

Check warning on line 609 in src/extract.ts

View workflow job for this annotation

GitHub Actions / Run linting and formatting check

Unexpected any. Specify a different type
throw new Error(
`Output file for 'julia' must be JSON file generated by BenchmarkTools.save("output.json", suit::BenchmarkGroup) : ${err.message}`,
);
Expand All @@ -625,7 +624,7 @@
let json: JmhBenchmarkJson[];
try {
json = JSON.parse(output);
} catch (err: any) {

Check warning on line 627 in src/extract.ts

View workflow job for this annotation

GitHub Actions / Run linting and formatting check

Unexpected any. Specify a different type
throw new Error(`Output file for 'jmh' must be JSON file generated by -rf json option: ${err.message}`);
}
return json.map((b) => {
Expand All @@ -642,7 +641,7 @@
let json: BenchmarkDotNetBenchmarkJson;
try {
json = JSON.parse(output);
} catch (err: any) {

Check warning on line 644 in src/extract.ts

View workflow job for this annotation

GitHub Actions / Run linting and formatting check

Unexpected any. Specify a different type
throw new Error(
`Output file for 'benchmarkdotnet' must be JSON file generated by '--exporters json' option or by adding the JsonExporter to your run config: ${err.message}`,
);
Expand All @@ -663,7 +662,7 @@
return json.map(({ name, value, unit, range, extra }) => {
return { name, value, unit, range, extra };
});
} catch (err: any) {

Check warning on line 665 in src/extract.ts

View workflow job for this annotation

GitHub Actions / Run linting and formatting check

Unexpected any. Specify a different type
throw new Error(
`Output file for 'custom-(bigger|smaller)-is-better' must be JSON file containing an array of entries in BenchmarkResult format: ${err.message}`,
);
Expand Down
47 changes: 47 additions & 0 deletions test/__snapshots__/extract.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,53 @@ exports[`extractResult() extracts benchmark output from cargo - cargo_output.txt
}
`;

exports[`extractResult() extracts benchmark output from cargo - cargo_output_units.txt 1`] = `
{
"benches": [
{
"name": "cmov",
"range": "± 14",
"unit": "cycles/iter",
"value": 2835,
},
{
"name": "cmov2",
"range": "± 19",
"unit": "cycles/iter",
"value": 2845,
},
{
"name": "mov",
"range": "± 17",
"unit": "cycles/iter",
"value": 1508,
},
{
"name": "upload",
"range": "± 420",
"unit": "MS/s",
"value": 1911,
},
{
"name": "download",
"range": "± 69",
"unit": "MS/s",
"value": 9001,
},
],
"commit": {
"author": null,
"committer": null,
"id": "123456789abcdef",
"message": "this is dummy",
"timestamp": "dummy timestamp",
"url": "https://github.com/dummy/repo",
},
"date": 1712131503296,
"tool": "cargo",
}
`;

exports[`extractResult() extracts benchmark output from cargo - cargo_output2.txt 1`] = `
{
"benches": [
Expand Down
9 changes: 9 additions & 0 deletions test/data/extract/cargo_output_units.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
test cmov ... bench: 2835 cycles/iter (+/- 14)

test cmov2 ... bench: 2845 cycles/iter (+/- 19)

test mov ... bench: 1508 cycles/iter (+/- 17)

test upload ... bench: 1911 MS/s (+/- 420)

test download ... bench: 9001 MS/s (+/- 69)
4 changes: 4 additions & 0 deletions test/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ describe('extractResult()', function () {
tool: 'cargo',
file: 'cargo_output3.txt',
},
{
tool: 'cargo',
file: 'cargo_output_units.txt',
},
{
tool: 'cargo',
file: 'criterion_output.txt',
Expand Down
Loading