Skip to content

Commit

Permalink
core: use trace time origin for main-thread-task time origin (#14252)
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny committed Aug 2, 2022
1 parent c07012e commit bbfeaf1
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 66 deletions.
3 changes: 2 additions & 1 deletion lighthouse-core/computed/main-thread-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class MainThreadTasks {
*/
static async compute_(trace, context) {
const {mainThreadEvents, frames, timestamps} = await ProcessedTrace.request(trace, context);
return MainThreadTasks_.getMainThreadTasks(mainThreadEvents, frames, timestamps.traceEnd);
return MainThreadTasks_.getMainThreadTasks(mainThreadEvents, frames, timestamps.traceEnd,
timestamps.timeOrigin);
}
}

Expand Down
7 changes: 4 additions & 3 deletions lighthouse-core/lib/tracehouse/main-thread-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,9 +561,10 @@ class MainThreadTasks {
* @param {LH.TraceEvent[]} mainThreadEvents
* @param {Array<{id: string, url: string}>} frames
* @param {number} traceEndTs
* @param {number} [traceStartTs] Optional time-0 ts for tasks. Tasks before this point will have negative start/end times. Defaults to the first task found.
* @return {TaskNode[]}
*/
static getMainThreadTasks(mainThreadEvents, frames, traceEndTs) {
static getMainThreadTasks(mainThreadEvents, frames, traceEndTs, traceStartTs) {
const timers = new Map();
const xhrs = new Map();
const frameURLsById = new Map();
Expand All @@ -587,8 +588,8 @@ class MainThreadTasks {
priorTaskData.lastTaskURLs = task.attributableURLs;
}

// Rebase all the times to be relative to start of trace in ms
const firstTs = (tasks[0] || {startTime: 0}).startTime;
// Rebase all the times to be relative to start of trace and covert to ms.
const firstTs = traceStartTs ?? tasks[0].startTime;
for (const task of tasks) {
task.startTime = (task.startTime - firstTs) / 1000;
task.endTime = (task.endTime - firstTs) / 1000;
Expand Down
13 changes: 13 additions & 0 deletions lighthouse-core/test/computed/main-thread-tasks-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,17 @@ describe('MainThreadTasksComputed', () => {
const tasks = await MainThreadTasks.request(pwaTrace, context);
expect(tasks.length).toEqual(4784);
});

it('uses negative timestamps for tasks before navStart', async () => {
const context = {computedCache: new Map()};
const tasks = await MainThreadTasks.request(pwaTrace, context);
expect(tasks[0]).toMatchObject({
startTime: expect.toBeApproximately(-3, 1),
endTime: expect.toBeApproximately(-3, 1),
});
expect(tasks[1]).toMatchObject({
startTime: expect.toBeApproximately(-3, 1),
endTime: expect.toBeApproximately(-2.8, 1),
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"steps": [
{
"lhr": {
"lighthouseVersion": "9.6.1",
"lighthouseVersion": "9.6.4",
"requestedUrl": "https://www.mikescerealshack.co/",
"finalUrl": "https://www.mikescerealshack.co/",
"fetchTime": "2022-03-08T15:35:57.356Z",
Expand Down Expand Up @@ -1173,43 +1173,43 @@
"items": [
{
"duration": 9.06,
"startTime": 212.665
"startTime": 203.866
},
{
"duration": 5.668,
"startTime": 221.909
"startTime": 213.11
},
{
"duration": 6.24,
"startTime": 227.679
"startTime": 218.88
},
{
"duration": 35.001,
"startTime": 285.755
"startTime": 276.956
},
{
"duration": 16.457,
"startTime": 321.262
"startTime": 312.463
},
{
"duration": 9.424,
"startTime": 357.426
"startTime": 348.627
},
{
"duration": 23.147,
"startTime": 392.512
"startTime": 383.713
},
{
"duration": 10.796,
"startTime": 2973.653
"startTime": 2964.854
},
{
"duration": 6.275,
"startTime": 3011.559
"startTime": 3002.76
},
{
"duration": 30.474,
"startTime": 3211.47
"startTime": 3202.671
}
]
}
Expand Down Expand Up @@ -7595,7 +7595,7 @@
},
{
"lhr": {
"lighthouseVersion": "9.6.1",
"lighthouseVersion": "9.6.4",
"requestedUrl": "https://www.mikescerealshack.co/",
"finalUrl": "https://www.mikescerealshack.co/search?q=call+of+duty",
"fetchTime": "2022-03-08T15:36:08.267Z",
Expand Down Expand Up @@ -8485,103 +8485,103 @@
"items": [
{
"duration": 22.89,
"startTime": 11.794
"startTime": 10.796
},
{
"duration": 11.346,
"startTime": 34.882
"startTime": 33.884
},
{
"duration": 14.707,
"startTime": 47.712
"startTime": 46.714
},
{
"duration": 11.476,
"startTime": 64.964
"startTime": 63.966
},
{
"duration": 10.211,
"startTime": 77.887
"startTime": 76.889
},
{
"duration": 7.878,
"startTime": 90.234
"startTime": 89.236
},
{
"duration": 12.607,
"startTime": 101.631
"startTime": 100.633
},
{
"duration": 9.346,
"startTime": 116.441
"startTime": 115.443
},
{
"duration": 11.39,
"startTime": 126.968
"startTime": 125.97
},
{
"duration": 11.435,
"startTime": 140.423
"startTime": 139.425
},
{
"duration": 10.084,
"startTime": 152.953
"startTime": 151.955
},
{
"duration": 10.9,
"startTime": 164.298
"startTime": 163.3
},
{
"duration": 6.346,
"startTime": 206.944
"startTime": 205.946
},
{
"duration": 6.642,
"startTime": 214.45
"startTime": 213.452
},
{
"duration": 16.871,
"startTime": 222.556
"startTime": 221.558
},
{
"duration": 35.881,
"startTime": 821.648
"startTime": 820.65
},
{
"duration": 15.437,
"startTime": 857.584
"startTime": 856.586
},
{
"duration": 9.331,
"startTime": 873.028
"startTime": 872.03
},
{
"duration": 7.848,
"startTime": 887.532
"startTime": 886.534
},
{
"duration": 6.696,
"startTime": 897.886
"startTime": 896.888
},
{
"duration": 18.004,
"startTime": 1423.407
"startTime": 1422.409
},
{
"duration": 6.862,
"startTime": 2249.903
"startTime": 2248.905
},
{
"duration": 194.901,
"startTime": 2256.773
"startTime": 2255.775
},
{
"duration": 14.028,
"startTime": 2451.719
"startTime": 2450.721
},
{
"duration": 8.949,
"startTime": 2467.956
"startTime": 2466.958
}
]
}
Expand Down Expand Up @@ -8814,7 +8814,7 @@
{
"url": "https://www.mikescerealshack.co/",
"duration": 194.901,
"startTime": 2256.773
"startTime": 2255.775
}
]
}
Expand Down Expand Up @@ -11057,7 +11057,7 @@
},
{
"lhr": {
"lighthouseVersion": "9.6.1",
"lighthouseVersion": "9.6.4",
"requestedUrl": "https://www.mikescerealshack.co/search?q=call+of+duty",
"finalUrl": "https://www.mikescerealshack.co/search?q=call+of+duty",
"fetchTime": "2022-03-08T15:36:22.894Z",
Expand Down Expand Up @@ -15865,7 +15865,7 @@
},
{
"lhr": {
"lighthouseVersion": "9.6.1",
"lighthouseVersion": "9.6.4",
"requestedUrl": "https://www.mikescerealshack.co/corrections",
"finalUrl": "https://www.mikescerealshack.co/corrections",
"fetchTime": "2022-03-08T15:36:29.745Z",
Expand Down Expand Up @@ -17022,19 +17022,19 @@
"items": [
{
"duration": 14.962,
"startTime": 151.327
"startTime": 138.885
},
{
"duration": 10.715,
"startTime": 188.398
"startTime": 175.956
},
{
"duration": 12.041,
"startTime": 213.628
"startTime": 201.186
},
{
"duration": 19.568,
"startTime": 284.111
"startTime": 271.669
}
]
}
Expand Down
31 changes: 31 additions & 0 deletions lighthouse-core/test/lib/tracehouse/main-thread-tasks-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,37 @@ describe('Main Thread Tasks', () => {
expect(tasks).toHaveLength(425);
});

it('should use the first task as time origin if no traceStartTs is given', () => {
const {mainThreadEvents, frames, timestamps} =
TraceProcessor.processTrace({traceEvents: pwaTrace});
const tasks = MainThreadTasks.getMainThreadTasks(mainThreadEvents, frames, timestamps.traceEnd);

expect(tasks[0]).toMatchObject({
startTime: 0,
endTime: expect.toBeApproximately(0.02),
});
expect(tasks[1]).toMatchObject({
startTime: expect.toBeApproximately(0.02),
endTime: expect.toBeApproximately(0.03),
});
});

it('should use traceStartTs as time origin if given', () => {
const {mainThreadEvents, frames, timestamps} =
TraceProcessor.processTrace({traceEvents: pwaTrace});
const tasks = MainThreadTasks.getMainThreadTasks(mainThreadEvents, frames, timestamps.traceEnd,
timestamps.timeOrigin);

expect(tasks[0]).toMatchObject({
startTime: expect.toBeApproximately(-15.02),
endTime: expect.toBeApproximately(-15),
});
expect(tasks[1]).toMatchObject({
startTime: expect.toBeApproximately(-15),
endTime: expect.toBeApproximately(-14.99),
});
});

it('should compute parent/child correctly', () => {
/*
An artistic rendering of the below trace:
Expand Down

0 comments on commit bbfeaf1

Please sign in to comment.