Skip to content

Commit

Permalink
Add patch to fix accumulated negative deltas
Browse files Browse the repository at this point in the history
  • Loading branch information
taneliang committed Aug 4, 2020
1 parent b26cdb5 commit 9d892fc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
18 changes: 9 additions & 9 deletions src/import/__snapshots__/chrome.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -633,16 +633,16 @@ Object {
"line": 0,
"name": "(anonymous)",
"selfWeight": 0,
"totalWeight": 2591,
"totalWeight": 2524,
},
Frame {
"col": -1,
"file": "",
"key": "Worker::-1:-1",
"line": -1,
"name": "Worker",
"selfWeight": 873,
"totalWeight": 2591,
"selfWeight": 806,
"totalWeight": 2524,
},
Frame {
"col": 29,
Expand Down Expand Up @@ -731,7 +731,7 @@ Object {
"(program) 11.20ms",
" 296.00碌s",
"(program) 1.90ms",
"(anonymous);Worker 873.00碌s",
"(anonymous);Worker 806.00碌s",
"(anonymous);Worker;(program) 1.72ms",
" 670.00碌s",
"(program) 28.45ms",
Expand Down Expand Up @@ -1305,7 +1305,7 @@ Object {
"line": 0,
"name": "(anonymous)",
"selfWeight": 542,
"totalWeight": 6218,
"totalWeight": 6115,
},
Frame {
"col": 25,
Expand All @@ -1314,16 +1314,16 @@ Object {
"line": 30,
"name": "insertTextScript",
"selfWeight": 392,
"totalWeight": 977,
"totalWeight": 874,
},
Frame {
"col": 25,
"file": "chrome-extension://denbgaamihkadbghdceggmchnflmhpmk/contentScript.js",
"key": "insertHeaderNode:chrome-extension://denbgaamihkadbghdceggmchnflmhpmk/contentScript.js:57:25",
"line": 57,
"name": "insertHeaderNode",
"selfWeight": 292,
"totalWeight": 585,
"selfWeight": 189,
"totalWeight": 482,
},
Frame {
"col": undefined,
Expand Down Expand Up @@ -1558,7 +1558,7 @@ Object {
"(anonymous);(anonymous);(anonymous);(program) 27.49ms",
"(anonymous) 542.00碌s",
"(anonymous);insertTextScript 392.00碌s",
"(anonymous);insertTextScript;insertHeaderNode 292.00碌s",
"(anonymous);insertTextScript;insertHeaderNode 189.00碌s",
"(anonymous);insertTextScript;insertHeaderNode;appendChild;(anonymous);(anonymous) 148.00碌s",
"(anonymous);insertTextScript;insertHeaderNode;appendChild 145.00碌s",
"(anonymous);listenForMessage;get webstore 148.00碌s",
Expand Down
28 changes: 20 additions & 8 deletions src/import/chrome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ export function importFromChromeCPUProfile(chromeProfile: CPUProfile): Profile {
// Ref: https://github.com/v8/v8/blob/44bd8fd7/src/inspector/js_protocol.json#L1485
let elapsed = chromeProfile.timeDeltas[0]

// Prevents negative time deltas from causing bad data.
let lastElapsed = elapsed

let lastNodeId = NaN

// The chrome CPU profile format doesn't collapse identical samples. We'll do that
Expand All @@ -232,21 +235,30 @@ export function importFromChromeCPUProfile(chromeProfile: CPUProfile): Profile {
const nodeId = chromeProfile.samples[i]
if (nodeId != lastNodeId) {
samples.push(nodeId)
sampleTimes.push(elapsed)
if (elapsed < lastElapsed) {
sampleTimes.push(lastElapsed)
} else {
sampleTimes.push(elapsed)
lastElapsed = elapsed
}
}

if (i === chromeProfile.samples.length - 1) {
if (!isNaN(lastNodeId)) {
samples.push(lastNodeId)
sampleTimes.push(elapsed)
if (elapsed < lastElapsed) {
sampleTimes.push(lastElapsed)
} else {
sampleTimes.push(elapsed)
lastElapsed = elapsed
}
}
} else {
let timeDelta = chromeProfile.timeDeltas[i + 1]
if (timeDelta < 0) {
// This is super noisy, but can be helpful when debugging strange data
// console.warn('Substituting zero for unexpected time delta:', timeDelta, 'at index', i)
timeDelta = 0
}
const timeDelta = chromeProfile.timeDeltas[i + 1]
// This is super noisy, but can be helpful when debugging strange data
// if (timeDelta < 0) {
// console.warn('Substituting zero for unexpected time delta:', timeDelta, 'at index', i)
// }

elapsed += timeDelta
lastNodeId = nodeId
Expand Down

0 comments on commit 9d892fc

Please sign in to comment.