chore: align monotonicTime across processes (#35130)

This commit is contained in:
Pavel Feldman 2025-03-10 19:19:20 -07:00 committed by GitHub
parent 43ee924087
commit ec4c66133e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 20 additions and 5 deletions

View File

@ -14,6 +14,18 @@
* limitations under the License.
*/
export function monotonicTime(): number {
return Math.floor(performance.now() * 1000) / 1000;
let _timeOrigin = performance.timeOrigin;
let _timeShift = 0;
export function setTimeOrigin(origin: number) {
_timeOrigin = origin;
_timeShift = performance.timeOrigin - origin;
}
export function timeOrigin(): number {
return _timeOrigin;
}
export function monotonicTime(): number {
return Math.floor((performance.now() + _timeShift) * 1000) / 1000;
}

View File

@ -55,6 +55,7 @@ export type SerializedConfig = {
};
export type ProcessInitParams = {
timeOrigin: number;
processName: string;
};

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
import { startProfiling, stopProfiling } from 'playwright-core/lib/utils';
import { setTimeOrigin, startProfiling, stopProfiling } from 'playwright-core/lib/utils';
import { serializeError } from '../util';
import { registerESMLoader } from './esmLoaderHost';
@ -69,6 +69,7 @@ process.on('message', async (message: any) => {
if (message.method === '__init__') {
const { processParams, runnerParams, runnerScript } = message.params as { processParams: ProcessInitParams, runnerParams: any, runnerScript: string };
void startProfiling();
setTimeOrigin(processParams.timeOrigin);
const { create } = require(runnerScript);
processRunner = create(runnerParams) as ProcessRunner;
processName = processParams.processName;

View File

@ -17,7 +17,7 @@
import child_process from 'child_process';
import { EventEmitter } from 'events';
import { assert } from 'playwright-core/lib/utils';
import { assert, timeOrigin } from 'playwright-core/lib/utils';
import { debug } from 'playwright-core/lib/utilsBundle';
import { esmLoaderRegistered } from '../common/esmLoaderHost';
@ -115,7 +115,8 @@ export class ProcessHost extends EventEmitter {
return error;
const processParams: ProcessInitParams = {
processName: this._processName
processName: this._processName,
timeOrigin: timeOrigin(),
};
this.send({