diff --git a/src/write.ts b/src/write.ts index e1ad4eafd..08da8942c 100644 --- a/src/write.ts +++ b/src/write.ts @@ -148,11 +148,53 @@ function floatStr(n: number) { return n.toString(); } +const round = (n: number, places = 3): number => parseFloat(n.toFixed(places)); + +function strDuration(n: number): string { + let unit = 'ns'; + if (n >= 6e10) { + let secs = n / 1e9; + const mins = Math.floor(secs / 60); + secs %= 60; + return `${mins}m ${round(secs)}s`; + } + + if (n >= 1e9) { + unit = 's'; + n /= 1e9; + } else if (n >= 1e6) { + unit = 'ms'; + n /= 1e6; + } else if (n >= 1e3) { + unit = 'μs'; + n /= 1e3; + } + + return `${round(n)}${unit}`; +} + +const NS_RANGE_REGEX = /^±\s*([0-9]+)$/; + function strVal(b: BenchmarkResult): string { - let s = `\`${b.value}\` ${b.unit}`; - if (b.range) { - s += ` (\`${b.range}\`)`; + let s = ''; + let range = b.range; + + if (b.unit === 'ns/iter') { + s += `\`${strDuration(b.value)}/iter\``; + if (range?.match(NS_RANGE_REGEX)) { + range = '± ' + strDuration(parseInt(range.replace(NS_RANGE_REGEX, '$1'))); + } else { + // If we can't show a unit, just give up... + range = undefined; + } + } else { + s += `\`${b.value} ${b.unit}\``; } + + if (range) { + s += `
(\`${range}\`)`; + } + return s; } diff --git a/test/buildComment.test.ts b/test/buildComment.test.ts index ea9431c51..96c99559b 100644 --- a/test/buildComment.test.ts +++ b/test/buildComment.test.ts @@ -111,9 +111,9 @@ describe('buildComment', () => { | Benchmark suite | Current: testCommitIdCurrent | Previous: testCommitIdPrevious | Ratio | |-|-|-|-| - | \`TestBench<1>\` | \`0\` testUnit | \`0\` testUnit | \`1\` | - | \`TestBench<2>\` | \`1\` testUnit | \`0\` testUnit | \`+∞\` | - | \`TestBench<3>\` | \`-1\` testUnit | \`0\` testUnit | \`-∞\` | + | \`TestBench<1>\` | \`0 testUnit\` | \`0 testUnit\` | \`1\` | + | \`TestBench<2>\` | \`1 testUnit\` | \`0 testUnit\` | \`+∞\` | + | \`TestBench<3>\` | \`-1 testUnit\` | \`0 testUnit\` | \`-∞\` | @@ -197,9 +197,9 @@ describe('buildComment', () => { | Benchmark suite | Current: testCommitIdCurrent | Previous: testCommitIdPrevious | Ratio | |-|-|-|-| - | \`TestBench<1>\` | \`0\` testUnit | \`0\` testUnit | \`1\` | - | \`TestBench<2>\` | \`0\` testUnit | \`1\` testUnit | \`+∞\` | - | \`TestBench<3>\` | \`0\` testUnit | \`-1\` testUnit | \`-∞\` | + | \`TestBench<1>\` | \`0 testUnit\` | \`0 testUnit\` | \`1\` | + | \`TestBench<2>\` | \`0 testUnit\` | \`1 testUnit\` | \`+∞\` | + | \`TestBench<3>\` | \`0 testUnit\` | \`-1 testUnit\` | \`-∞\` | diff --git a/test/write.spec.ts b/test/write.spec.ts index e88543cbb..03a6b15f3 100644 --- a/test/write.spec.ts +++ b/test/write.spec.ts @@ -339,8 +339,8 @@ describe.each(['https://github.com', 'https://github.enterprise.corp'])('writeBe '', '| Benchmark suite | Current: current commit id | Previous: prev commit id | Ratio |', '|-|-|-|-|', - '| `bench_fib_10` | `210` ns/iter (`± 20`) | `100` ns/iter (`± 20`) | `2.10` |', - '| `bench_fib_20` | `25000` ns/iter (`± 20`) | `10000` ns/iter (`± 20`) | `2.50` |', + '| `bench_fib_10` | `210ns/iter`
(`± 20ns`) | `100ns/iter`
(`± 20ns`) | `2.10` |', + '| `bench_fib_20` | `25μs/iter`
(`± 20ns`) | `10μs/iter`
(`± 20ns`) | `2.50` |', '', `This comment was automatically generated by [workflow](${serverUrl}/user/repo/actions?query=workflow%3AWorkflow%20name) using [github-action-benchmark](https://github.com/marketplace/actions/continuous-benchmark).`, '', @@ -378,7 +378,7 @@ describe.each(['https://github.com', 'https://github.enterprise.corp'])('writeBe '', '| Benchmark suite | Current: current commit id | Previous: prev commit id | Ratio |', '|-|-|-|-|', - '| `benchFib10` | `20` ops/sec (`+-20`) | `100` ops/sec (`+-20`) | `5` |', + '| `benchFib10` | `20 ops/sec`
(`+-20`) | `100 ops/sec`
(`+-20`) | `5` |', '', `This comment was automatically generated by [workflow](${serverUrl}/user/repo/actions?query=workflow%3AWorkflow%20name) using [github-action-benchmark](https://github.com/marketplace/actions/continuous-benchmark).`, '', @@ -416,7 +416,7 @@ describe.each(['https://github.com', 'https://github.enterprise.corp'])('writeBe '', '| Benchmark suite | Current: current commit id | Previous: prev commit id | Ratio |', '|-|-|-|-|', - '| `bench_fib_10` | `210` ns/iter (`± 20`) | `100` ns/iter (`± 20`) | `2.10` |', + '| `bench_fib_10` | `210ns/iter`
(`± 20ns`) | `100ns/iter`
(`± 20ns`) | `2.10` |', '', `This comment was automatically generated by [workflow](${serverUrl}/user/repo/actions?query=workflow%3AWorkflow%20name) using [github-action-benchmark](https://github.com/marketplace/actions/continuous-benchmark).`, '', @@ -454,7 +454,7 @@ describe.each(['https://github.com', 'https://github.enterprise.corp'])('writeBe '', '| Benchmark suite | Current: current commit id | Previous: prev commit id | Ratio |', '|-|-|-|-|', - '| `bench_fib_10` | `210` ns/iter (`± 20`) | `100` ns/iter (`± 20`) | `2.10` |', + '| `bench_fib_10` | `210ns/iter`
(`± 20ns`) | `100ns/iter`
(`± 20ns`) | `2.10` |', '', `This comment was automatically generated by [workflow](${serverUrl}/user/repo/actions?query=workflow%3AWorkflow%20name) using [github-action-benchmark](https://github.com/marketplace/actions/continuous-benchmark).`, ], @@ -595,8 +595,8 @@ describe.each(['https://github.com', 'https://github.enterprise.corp'])('writeBe '', '| Benchmark suite | Current: current commit id | Previous: prev commit id | Ratio |', '|-|-|-|-|', - '| `bench_fib_10` | `210` ns/iter (`± 20`) | `100` ns/iter (`± 20`) | `2.10` |', - '| `bench_fib_20` | `25000` ns/iter (`± 20`) | `10000` ns/iter (`± 20`) | `2.50` |', + '| `bench_fib_10` | `210ns/iter`
(`± 20ns`) | `100ns/iter`
(`± 20ns`) | `2.10` |', + '| `bench_fib_20` | `25μs/iter`
(`± 20ns`) | `10μs/iter`
(`± 20ns`) | `2.50` |', '', `This comment was automatically generated by [workflow](${serverUrl}/user/repo/actions?query=workflow%3AWorkflow%20name) using [github-action-benchmark](https://github.com/marketplace/actions/continuous-benchmark).`, '', @@ -634,7 +634,7 @@ describe.each(['https://github.com', 'https://github.enterprise.corp'])('writeBe '', '| Benchmark suite | Current: current commit id | Previous: prev commit id | Ratio |', '|-|-|-|-|', - '| `benchFib10` | `100` ops/sec (`+-20`) | `100` ops/sec (`+-20`) | `1` |', + '| `benchFib10` | `100 ops/sec`
(`+-20`) | `100 ops/sec`
(`+-20`) | `1` |', '', `This comment was automatically generated by [workflow](${serverUrl}/user/repo/actions?query=workflow%3AWorkflow%20name) using [github-action-benchmark](https://github.com/marketplace/actions/continuous-benchmark).`, '', @@ -674,7 +674,7 @@ describe.each(['https://github.com', 'https://github.enterprise.corp'])('writeBe '', '| Benchmark suite | Current: current commit id | Previous: prev commit id | Ratio |', '|-|-|-|-|', - '| `bench_fib_10` | `350` ns/iter (`± 20`) | `100` ns/iter (`± 20`) | `3.50` |', + '| `bench_fib_10` | `350ns/iter`
(`± 20ns`) | `100ns/iter`
(`± 20ns`) | `3.50` |', '', `This comment was automatically generated by [workflow](${serverUrl}/user/repo/actions?query=workflow%3AWorkflow%20name) using [github-action-benchmark](https://github.com/marketplace/actions/continuous-benchmark).`, '', @@ -1162,7 +1162,7 @@ describe.each(['https://github.com', 'https://github.enterprise.corp'])('writeBe '', '| Benchmark suite | Current: current commit id | Previous: prev commit id | Ratio |', '|-|-|-|-|', - '| `bench_fib_10` | `210` ns/iter (`± 20`) | `100` ns/iter (`± 20`) | `2.10` |', + '| `bench_fib_10` | `210ns/iter`
(`± 20ns`) | `100ns/iter`
(`± 20ns`) | `2.10` |', '', `This comment was automatically generated by [workflow](${serverUrl}/user/repo/actions?query=workflow%3AWorkflow%20name) using [github-action-benchmark](https://github.com/marketplace/actions/continuous-benchmark).`, ],