Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase precision of test case duration measurements. #1793

Merged
merged 10 commits into from
Sep 8, 2021
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.
joac marked this conversation as resolved.
Show resolved Hide resolved
[#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 {
aurelien-reeves marked this conversation as resolved.
Show resolved Hide resolved
return new methods.Date().getTime()
return methods.performance.now()
}

export function durationBetweenTimestamps(
Expand Down