Skip to content

Commit bfec205

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

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

src/write.ts

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,51 @@ 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+
range = range && range.match(NS_RANGE_REGEX) ?
185+
"± " + strDuration(parseInt(range.replace(NS_RANGE_REGEX, "$1"))) :
186+
// If we can't show a unit, just give up...
187+
range = undefined;
188+
} else {
189+
s += `\`${b.value} ${b.unit}\``;
155190
}
191+
192+
if (range) {
193+
s += `<br>(\`${range}\`)`;
194+
}
195+
156196
return s;
157197
}
158198

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

0 commit comments

Comments
 (0)