Skip to content

Commit

Permalink
fix: Support sub-nanosecond precision on Cargo benchmarks (#246)
Browse files Browse the repository at this point in the history
* add test to cover new more precise format
* change regexp and parsing of the number to cover sub-nanosecond precision

---------

Co-authored-by: blaine-arcjet <146491715+blaine-arcjet@users.noreply.github.com>
  • Loading branch information
ktrz and blaine-arcjet committed May 19, 2024
1 parent 8e74972 commit f6ab2e2
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
## Unreleased
- **fix** Support sub-nanosecond precision on Cargo benchmarks (#246)


<a name="v1.20.1"></a>
# [v1.20.1](https://github.com/benchmark-action/github-action-benchmark/releases/tag/v1.20.1) - 02 Apr 2024
Expand Down
6 changes: 3 additions & 3 deletions src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ function extractCargoResult(output: string): BenchmarkResult[] {
const lines = output.split(/\r?\n/g);
const ret = [];
// Example:
// test bench_fib_20 ... bench: 37,174 ns/iter (+/- 7,527)
const reExtract = /^test (.+)\s+\.\.\. bench:\s+([0-9,]+) ns\/iter \(\+\/- ([0-9,]+)\)$/;
// 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 reComma = /,/g;

for (const line of lines) {
Expand All @@ -325,7 +325,7 @@ function extractCargoResult(output: string): BenchmarkResult[] {
}

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

ret.push({
Expand Down
29 changes: 29 additions & 0 deletions test/__snapshots__/extract.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,35 @@ exports[`extractResult() extracts benchmark output from cargo - cargo_output2.tx
}
`;

exports[`extractResult() extracts benchmark output from cargo - cargo_output3.txt 1`] = `
{
"benches": [
{
"name": "bench_fib_10",
"range": "± 2.21",
"unit": "ns/iter",
"value": 148.7,
},
{
"name": "bench_fib_20",
"range": "± 440.25",
"unit": "ns/iter",
"value": 18794.12,
},
],
"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 - criterion_output.txt 1`] = `
{
"benches": [
Expand Down
12 changes: 12 additions & 0 deletions test/data/extract/cargo_output3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out


running 2 tests
test bench_fib_10 ... bench: 148.70 ns/iter (+/- 2.21)
test bench_fib_20 ... bench: 18,794.12 ns/iter (+/- 440.25)

test result: ok. 0 passed; 0 failed; 0 ignored; 2 measured; 0 filtered out

4 changes: 4 additions & 0 deletions test/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ describe('extractResult()', function () {
tool: 'cargo',
file: 'cargo_output2.txt',
},
{
tool: 'cargo',
file: 'cargo_output3.txt',
},
{
tool: 'cargo',
file: 'criterion_output.txt',
Expand Down

0 comments on commit f6ab2e2

Please sign in to comment.