Skip to content

Commit

Permalink
Fix conversion of intermediary timings
Browse files Browse the repository at this point in the history
  • Loading branch information
juliandescottes committed Oct 8, 2024
1 parent 07bb8f8 commit 492ab57
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
8 changes: 7 additions & 1 deletion src/har-recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class HarRecorder {
this.networkEntries = [];
this.pageTimings = [];
this.topLevelContexts = [];

this._isInMicroseconds = undefined;
}

recordEvent(event) {
Expand Down Expand Up @@ -322,9 +324,13 @@ class HarRecorder {
// Firefox 131 or older returned timings in microseconds whereas Firefox 132
// or newer returns timings in milliseconds, as expected in the spec.
// https://bugzilla.mozilla.org/show_bug.cgi?id=1916685
if (timing > Date.now() * 100) {
if (this._isInMicroseconds === undefined) {
// If timing is at least a 100 times greater than the current date, we are
// dealing with microseconds.
this._isInMicroseconds = timing > Date.now() * 100;
}

if (this._isInMicroseconds) {
return timing / 1000;
}

Expand Down
10 changes: 6 additions & 4 deletions test/har-recorder.simple-har.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ test("HarRecorder generates simple har export", () => {

const isoEntryDate = harExport.log.entries[0].startedDateTime;
expect(new Date(isoEntryDate) * 1).toBe(startTime + 50);

const totalTime = harExport.log.entries[0].time;
expect(totalTime).toBe(650);
});

test("HarRecorder generates simple har export with microseconds", () => {
Expand Down Expand Up @@ -74,9 +77,8 @@ test("HarRecorder generates simple har export with microseconds", () => {
expect(harExport.log.entries.length).toBe(1);

const isoEntryDate = harExport.log.entries[0].startedDateTime;
expect(new Date(isoEntryDate) * 1).toBe(startTime + 50);

// Note: the request start time happens 50 microseconds after startTime
// which is going to be equal to startTime when converted to milliseconds.
// So compare directly with startTime.
expect(new Date(isoEntryDate) * 1).toBe(startTime);
const totalTime = harExport.log.entries[0].time;
expect(totalTime).toBe(650);
});
27 changes: 14 additions & 13 deletions test/resources/mock-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ function getMockEvents(startTime, options = {}) {
useLegacyHeaderFormat = false,
useMicroseconds = false,
} = options;
const highResStartTime = startTime * (useMicroseconds ? 1000 : 1);
const timeMult = useMicroseconds ? 1000 : 1;
const highResStartTime = startTime * timeMult;
const requestId = (options.requestId || gRequestId++) + "";

if (useLegacyHeaderFormat) {
Expand Down Expand Up @@ -39,7 +40,7 @@ function getMockEvents(startTime, options = {}) {
request: requestId,
timings: {
originTime: 0,
requestTime: highResStartTime + 50,
requestTime: highResStartTime + 50 * timeMult,
redirectStart: 0,
redirectEnd: 0,
fetchStart: 0,
Expand Down Expand Up @@ -80,19 +81,19 @@ function getMockEvents(startTime, options = {}) {
request: requestId,
timings: {
originTime: 0,
requestTime: highResStartTime + 50,
requestTime: highResStartTime + 50 * timeMult,
redirectStart: 0,
redirectEnd: 0,
fetchStart: highResStartTime + 100,
dnsStart: highResStartTime + 150,
dnsEnd: highResStartTime + 200,
connectStart: highResStartTime + 300,
connectEnd: highResStartTime + 400,
tlsStart: highResStartTime + 500,
tlsEnd: highResStartTime + 600,
requestStart: highResStartTime + 700,
responseStart: highResStartTime + 800,
responseEnd: highResStartTime + 900,
fetchStart: highResStartTime + 100 * timeMult,
dnsStart: highResStartTime + 150 * timeMult,
dnsEnd: highResStartTime + 200 * timeMult,
connectStart: highResStartTime + 300 * timeMult,
connectEnd: highResStartTime + 400 * timeMult,
tlsStart: highResStartTime + 500 * timeMult,
tlsEnd: highResStartTime + 600 * timeMult,
requestStart: highResStartTime + 700 * timeMult,
responseStart: highResStartTime + 800 * timeMult,
responseEnd: highResStartTime + 900 * timeMult,
},
url: options.url || "https://example.com/",
rawHeaders:
Expand Down

0 comments on commit 492ab57

Please sign in to comment.