Skip to content

Commit e069778

Browse files
committed
Use human readable durations
1 parent 5378675 commit e069778

File tree

3 files changed

+61
-19
lines changed

3 files changed

+61
-19
lines changed

src/write.ts

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,53 @@ function floatStr(n: number) {
148148
return n.toString();
149149
}
150150

151+
const round = (n: number, places = 3): number => parseFloat(n.toFixed(places));
152+
153+
function strDuration(n: number): string {
154+
let unit = 'ns';
155+
if (n >= 6e10) {
156+
let secs = n / 1e9;
157+
const mins = Math.floor(secs / 60);
158+
secs %= 60;
159+
return `${mins}m ${round(secs)}s`;
160+
}
161+
162+
if (n >= 1e9) {
163+
unit = 's';
164+
n /= 1e9;
165+
} else if (n >= 1e6) {
166+
unit = 'ms';
167+
n /= 1e6;
168+
} else if (n >= 1e3) {
169+
unit = 'μs';
170+
n /= 1e3;
171+
}
172+
173+
return `${round(n)}${unit}`;
174+
}
175+
176+
const NS_RANGE_REGEX = /^±\s*([0-9]+)$/;
177+
151178
function strVal(b: BenchmarkResult): string {
152-
let s = `\`${b.value}\` ${b.unit}`;
153-
if (b.range) {
154-
s += ` (\`${b.range}\`)`;
179+
let s = '';
180+
let range = b.range;
181+
182+
if (b.unit === 'ns/iter') {
183+
s += `\`${strDuration(b.value)}/iter\``;
184+
if (range?.match(NS_RANGE_REGEX)) {
185+
range = '± ' + strDuration(parseInt(range.replace(NS_RANGE_REGEX, '$1')));
186+
} else {
187+
// If we can't show a unit, just give up...
188+
range = undefined;
189+
}
190+
} else {
191+
s += `\`${b.value} ${b.unit}\``;
155192
}
193+
194+
if (range) {
195+
s += `<br>(\`${range}\`)`;
196+
}
197+
156198
return s;
157199
}
158200

test/buildComment.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ describe('buildComment', () => {
111111
112112
| Benchmark suite | Current: testCommitIdCurrent | Previous: testCommitIdPrevious | Ratio |
113113
|-|-|-|-|
114-
| \`TestBench<1>\` | \`0\` testUnit | \`0\` testUnit | \`1\` |
115-
| \`TestBench<2>\` | \`1\` testUnit | \`0\` testUnit | \`+∞\` |
116-
| \`TestBench<3>\` | \`-1\` testUnit | \`0\` testUnit | \`-∞\` |
114+
| \`TestBench<1>\` | \`0 testUnit\` | \`0 testUnit\` | \`1\` |
115+
| \`TestBench<2>\` | \`1 testUnit\` | \`0 testUnit\` | \`+∞\` |
116+
| \`TestBench<3>\` | \`-1 testUnit\` | \`0 testUnit\` | \`-∞\` |
117117
118118
</details>
119119
@@ -197,9 +197,9 @@ describe('buildComment', () => {
197197
198198
| Benchmark suite | Current: testCommitIdCurrent | Previous: testCommitIdPrevious | Ratio |
199199
|-|-|-|-|
200-
| \`TestBench<1>\` | \`0\` testUnit | \`0\` testUnit | \`1\` |
201-
| \`TestBench<2>\` | \`0\` testUnit | \`1\` testUnit | \`+∞\` |
202-
| \`TestBench<3>\` | \`0\` testUnit | \`-1\` testUnit | \`-∞\` |
200+
| \`TestBench<1>\` | \`0 testUnit\` | \`0 testUnit\` | \`1\` |
201+
| \`TestBench<2>\` | \`0 testUnit\` | \`1 testUnit\` | \`+∞\` |
202+
| \`TestBench<3>\` | \`0 testUnit\` | \`-1 testUnit\` | \`-∞\` |
203203
204204
</details>
205205

test/write.spec.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@ describe.each(['https://github.com', 'https://github.enterprise.corp'])('writeBe
339339
'',
340340
'| Benchmark suite | Current: current commit id | Previous: prev commit id | Ratio |',
341341
'|-|-|-|-|',
342-
'| `bench_fib_10` | `210` ns/iter (`± 20`) | `100` ns/iter (`± 20`) | `2.10` |',
343-
'| `bench_fib_20` | `25000` ns/iter (`± 20`) | `10000` ns/iter (`± 20`) | `2.50` |',
342+
'| `bench_fib_10` | `210ns/iter`<br>(`± 20ns`) | `100ns/iter`<br>(`± 20ns`) | `2.10` |',
343+
'| `bench_fib_20` | `25μs/iter`<br>(`± 20ns`) | `10μs/iter`<br>(`± 20ns`) | `2.50` |',
344344
'',
345345
`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).`,
346346
'',
@@ -378,7 +378,7 @@ describe.each(['https://github.com', 'https://github.enterprise.corp'])('writeBe
378378
'',
379379
'| Benchmark suite | Current: current commit id | Previous: prev commit id | Ratio |',
380380
'|-|-|-|-|',
381-
'| `benchFib10` | `20` ops/sec (`+-20`) | `100` ops/sec (`+-20`) | `5` |',
381+
'| `benchFib10` | `20 ops/sec`<br>(`+-20`) | `100 ops/sec`<br>(`+-20`) | `5` |',
382382
'',
383383
`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).`,
384384
'',
@@ -416,7 +416,7 @@ describe.each(['https://github.com', 'https://github.enterprise.corp'])('writeBe
416416
'',
417417
'| Benchmark suite | Current: current commit id | Previous: prev commit id | Ratio |',
418418
'|-|-|-|-|',
419-
'| `bench_fib_10` | `210` ns/iter (`± 20`) | `100` ns/iter (`± 20`) | `2.10` |',
419+
'| `bench_fib_10` | `210ns/iter`<br>(`± 20ns`) | `100ns/iter`<br>(`± 20ns`) | `2.10` |',
420420
'',
421421
`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).`,
422422
'',
@@ -454,7 +454,7 @@ describe.each(['https://github.com', 'https://github.enterprise.corp'])('writeBe
454454
'',
455455
'| Benchmark suite | Current: current commit id | Previous: prev commit id | Ratio |',
456456
'|-|-|-|-|',
457-
'| `bench_fib_10` | `210` ns/iter (`± 20`) | `100` ns/iter (`± 20`) | `2.10` |',
457+
'| `bench_fib_10` | `210ns/iter`<br>(`± 20ns`) | `100ns/iter`<br>(`± 20ns`) | `2.10` |',
458458
'',
459459
`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).`,
460460
],
@@ -595,8 +595,8 @@ describe.each(['https://github.com', 'https://github.enterprise.corp'])('writeBe
595595
'',
596596
'| Benchmark suite | Current: current commit id | Previous: prev commit id | Ratio |',
597597
'|-|-|-|-|',
598-
'| `bench_fib_10` | `210` ns/iter (`± 20`) | `100` ns/iter (`± 20`) | `2.10` |',
599-
'| `bench_fib_20` | `25000` ns/iter (`± 20`) | `10000` ns/iter (`± 20`) | `2.50` |',
598+
'| `bench_fib_10` | `210ns/iter`<br>(`± 20ns`) | `100ns/iter`<br>(`± 20ns`) | `2.10` |',
599+
'| `bench_fib_20` | `25μs/iter`<br>(`± 20ns`) | `10μs/iter`<br>(`± 20ns`) | `2.50` |',
600600
'',
601601
`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).`,
602602
'',
@@ -634,7 +634,7 @@ describe.each(['https://github.com', 'https://github.enterprise.corp'])('writeBe
634634
'',
635635
'| Benchmark suite | Current: current commit id | Previous: prev commit id | Ratio |',
636636
'|-|-|-|-|',
637-
'| `benchFib10` | `100` ops/sec (`+-20`) | `100` ops/sec (`+-20`) | `1` |',
637+
'| `benchFib10` | `100 ops/sec`<br>(`+-20`) | `100 ops/sec`<br>(`+-20`) | `1` |',
638638
'',
639639
`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).`,
640640
'',
@@ -674,7 +674,7 @@ describe.each(['https://github.com', 'https://github.enterprise.corp'])('writeBe
674674
'',
675675
'| Benchmark suite | Current: current commit id | Previous: prev commit id | Ratio |',
676676
'|-|-|-|-|',
677-
'| `bench_fib_10` | `350` ns/iter (`± 20`) | `100` ns/iter (`± 20`) | `3.50` |',
677+
'| `bench_fib_10` | `350ns/iter`<br>(`± 20ns`) | `100ns/iter`<br>(`± 20ns`) | `3.50` |',
678678
'',
679679
`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).`,
680680
'',
@@ -1162,7 +1162,7 @@ describe.each(['https://github.com', 'https://github.enterprise.corp'])('writeBe
11621162
'',
11631163
'| Benchmark suite | Current: current commit id | Previous: prev commit id | Ratio |',
11641164
'|-|-|-|-|',
1165-
'| `bench_fib_10` | `210` ns/iter (`± 20`) | `100` ns/iter (`± 20`) | `2.10` |',
1165+
'| `bench_fib_10` | `210ns/iter`<br>(`± 20ns`) | `100ns/iter`<br>(`± 20ns`) | `2.10` |',
11661166
'',
11671167
`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).`,
11681168
],

0 commit comments

Comments
 (0)