From 731956c4edd92244ec0b1d7b96e40ea2080bf597 Mon Sep 17 00:00:00 2001 From: Chris Trzesniewski Date: Sun, 19 May 2024 11:31:07 +0200 Subject: [PATCH] add tests and fix issue with parsing number as int instead of float --- src/extract.ts | 4 ++-- test/__snapshots__/extract.spec.ts.snap | 29 +++++++++++++++++++++++++ test/data/extract/cargo_output3.txt | 12 ++++++++++ test/extract.spec.ts | 4 ++++ 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 test/data/extract/cargo_output3.txt diff --git a/src/extract.ts b/src/extract.ts index 73879f945..a582e8907 100644 --- a/src/extract.ts +++ b/src/extract.ts @@ -314,7 +314,7 @@ 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) + // 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; @@ -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',