Skip to content

Commit

Permalink
Increase precision of test case duration measurements. (#1793)
Browse files Browse the repository at this point in the history
  • Loading branch information
joac authored Sep 8, 2021
1 parent c87b6dc commit 0fbb0fb
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ See the [migration guide](./docs/migration.md) for details of how to migrate fro
[#1794](https://github.com/cucumber/cucumber-js/pull/1794)

### Changed
* Use performance timers for test case duration measurement.
[#1793](https://github.com/cucumber/cucumber-js/pull/1793)

### Deprecated

Expand Down
2 changes: 2 additions & 0 deletions features/support/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ export function normalizeText(text: string): string {
.replace(/\d+(.\d+)?ms/g, '<d>ms')
.replace(/\//g, path.sep)
.replace(/ +/g, ' ')
.replace(/─+/gu, '─')
.split('\n')
.map((line) => line.trim())
.join('\n')

return normalizeSummaryDuration(normalized)
}
2 changes: 1 addition & 1 deletion src/formatter/usage_formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default class UsageFormatter extends Formatter {
col2.push(
`${messages.TimeConversion.durationToMilliseconds(
match.duration
).toString()}ms`
).toFixed(2)}ms`
)
} else {
col2.push('-')
Expand Down
4 changes: 2 additions & 2 deletions src/formatter/usage_formatter_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ describe('UsageFormatter', () => {
│ Pattern / Text │ Duration │ Location │
├────────────────┼──────────┼───────────────────┤
│ /def?/ │ 1.50ms │ usage_steps.ts:16 │
│ def │ 2ms │ a.feature:3 │
│ de │ 1ms │ a.feature:4 │
│ def │ 2.00ms │ a.feature:3 │
│ de │ 1.00ms │ a.feature:4 │
├────────────────┼──────────┼───────────────────┤
│ abc │ UNUSED │ usage_steps.ts:11 │
├────────────────┼──────────┼───────────────────┤
Expand Down
4 changes: 3 additions & 1 deletion src/time.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { performance } from 'perf_hooks'
import * as messages from '@cucumber/messages'

let previousTimestamp: number
Expand All @@ -14,6 +15,7 @@ const methods: any = {
},
setInterval: setInterval.bind(global),
setTimeout: setTimeout.bind(global),
performance,
}

if (typeof setImmediate !== 'undefined') {
Expand All @@ -22,7 +24,7 @@ if (typeof setImmediate !== 'undefined') {
}

function getTimestamp(): number {
return new methods.Date().getTime()
return methods.performance.now()
}

export function durationBetweenTimestamps(
Expand Down

0 comments on commit 0fbb0fb

Please sign in to comment.