diff --git a/CHANGELOG.md b/CHANGELOG.md index d1d221a32..adbe0eded 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,6 @@ ## Unreleased +- **fix** Support sub-nanosecond precision on Cargo benchmarks (#246) + # [v1.20.1](https://github.com/benchmark-action/github-action-benchmark/releases/tag/v1.20.1) - 02 Apr 2024 diff --git a/src/extract.ts b/src/extract.ts index 1bfb47ac2..a582e8907 100644 --- a/src/extract.ts +++ b/src/extract.ts @@ -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) { @@ -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({ diff --git a/test/__snapshots__/extract.spec.ts.snap b/test/__snapshots__/extract.spec.ts.snap index d37f347a7..19f473cf9 100644 --- a/test/__snapshots__/extract.spec.ts.snap +++ b/test/__snapshots__/extract.spec.ts.snap @@ -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": [ diff --git a/test/data/extract/cargo_output3.txt b/test/data/extract/cargo_output3.txt new file mode 100644 index 000000000..631ec60b0 --- /dev/null +++ b/test/data/extract/cargo_output3.txt @@ -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 + diff --git a/test/extract.spec.ts b/test/extract.spec.ts index 2e8817e2b..b49567bec 100644 --- a/test/extract.spec.ts +++ b/test/extract.spec.ts @@ -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',