Skip to content

Commit

Permalink
core(critical-request-chain): remove empty children from LHR (#6211)
Browse files Browse the repository at this point in the history
  • Loading branch information
exterkamp authored and brendankenny committed Oct 11, 2018
1 parent 482f81d commit 09cc741
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 53 deletions.
43 changes: 23 additions & 20 deletions lighthouse-core/audits/critical-request-chains.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ class CriticalRequestChains extends Audit {
});

// Carry on walking.
walk(child.children, depth + 1, startTime);
if (child.children) {
walk(child.children, depth + 1, startTime);
}
}, '');
}

Expand Down Expand Up @@ -132,21 +134,23 @@ class CriticalRequestChains extends Audit {
} else {
chain = {
request: simpleRequest,
children: {},
};
flattendChains[opts.id] = chain;
}

for (const chainId of Object.keys(opts.node.children)) {
// Note: cast should be Partial<>, but filled in when child node is traversed.
const childChain = /** @type {LH.Audit.SimpleCriticalRequestNode[string]} */ ({
request: {},
children: {},
});
chainMap.set(chainId, childChain);
chain.children[chainId] = childChain;
if (opts.node.children) {
for (const chainId of Object.keys(opts.node.children)) {
// Note: cast should be Partial<>, but filled in when child node is traversed.
const childChain = /** @type {LH.Audit.SimpleCriticalRequestNode[string]} */ ({
request: {},
});
chainMap.set(chainId, childChain);
if (!chain.children) {
chain.children = {};
}
chain.children[chainId] = childChain;
}
}

chainMap.set(opts.id, chain);
}

Expand All @@ -170,17 +174,16 @@ class CriticalRequestChains extends Audit {
* @param {number} depth
*/
function walk(node, depth) {
const children = Object.keys(node);

// Since a leaf node indicates the end of a chain, we can inspect the number
// of child nodes, and, if the count is zero, increment the count.
if (children.length === 0) {
chainCount++;
}
const childIds = Object.keys(node);

children.forEach(id => {
childIds.forEach(id => {
const child = node[id];
walk(child.children, depth + 1);
if (child.children) {
walk(child.children, depth + 1);
} else {
// if the node doesn't have a children field, then it is a leaf, so +1
chainCount++;
}
}, '');
}
// Convert
Expand Down
13 changes: 7 additions & 6 deletions lighthouse-core/report/html/renderer/crc-details-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class CriticalRequestChainRenderer {
const node = parent[id];
const siblings = Object.keys(parent);
const isLastChild = siblings.indexOf(id) === (siblings.length - 1);
const hasChildren = Object.keys(node.children).length > 0;
const hasChildren = !!node.children && Object.keys(node.children).length > 0;

// Copy the tree markers so that we don't change by reference.
const newTreeMarkers = Array.isArray(treeMarkers) ? treeMarkers.slice(0) : [];
Expand Down Expand Up @@ -149,11 +149,12 @@ class CriticalRequestChainRenderer {
*/
static buildTree(dom, tmpl, segment, elem, details) {
elem.appendChild(CriticalRequestChainRenderer.createChainNode(dom, tmpl, segment));

for (const key of Object.keys(segment.node.children)) {
const childSegment = CriticalRequestChainRenderer.createSegment(segment.node.children, key,
segment.startTime, segment.transferSize, segment.treeMarkers, segment.isLastChild);
CriticalRequestChainRenderer.buildTree(dom, tmpl, childSegment, elem, details);
if (segment.node.children) {
for (const key of Object.keys(segment.node.children)) {
const childSegment = CriticalRequestChainRenderer.createSegment(segment.node.children, key,
segment.startTime, segment.transferSize, segment.treeMarkers, segment.isLastChild);
CriticalRequestChainRenderer.buildTree(dom, tmpl, childSegment, elem, details);
}
}
}

Expand Down
39 changes: 13 additions & 26 deletions lighthouse-core/test/results/sample_v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,7 @@
"endTime": 185604.52588,
"responseReceivedTime": 185604.52470399998,
"transferSize": 821
},
"children": {}
}
},
"75994.4": {
"request": {
Expand All @@ -335,8 +334,7 @@
"endTime": 185604.534512,
"responseReceivedTime": 185604.532778,
"transferSize": 139
},
"children": {}
}
},
"75994.5": {
"request": {
Expand All @@ -345,8 +343,7 @@
"endTime": 185606.170588,
"responseReceivedTime": 185606.169761,
"transferSize": 821
},
"children": {}
}
},
"75994.6": {
"request": {
Expand All @@ -355,8 +352,7 @@
"endTime": 185604.541262,
"responseReceivedTime": 185604.54052399998,
"transferSize": 1108
},
"children": {}
}
},
"75994.7": {
"request": {
Expand All @@ -365,8 +361,7 @@
"endTime": 185604.549739,
"responseReceivedTime": 185604.54903999998,
"transferSize": 736
},
"children": {}
}
},
"75994.8": {
"request": {
Expand All @@ -375,8 +370,7 @@
"endTime": 185605.097653,
"responseReceivedTime": 185605.096858,
"transferSize": 733
},
"children": {}
}
},
"75994.9": {
"request": {
Expand All @@ -385,8 +379,7 @@
"endTime": 185607.537382,
"responseReceivedTime": 185607.53660999998,
"transferSize": 821
},
"children": {}
}
},
"75994.10": {
"request": {
Expand All @@ -395,8 +388,7 @@
"endTime": 185605.113307,
"responseReceivedTime": 185605.104776,
"transferSize": 1703
},
"children": {}
}
},
"75994.11": {
"request": {
Expand All @@ -405,8 +397,7 @@
"endTime": 185604.557407,
"responseReceivedTime": 185604.556719,
"transferSize": 144
},
"children": {}
}
},
"75994.21": {
"request": {
Expand All @@ -415,8 +406,7 @@
"endTime": 185607.28227,
"responseReceivedTime": 185606.742005,
"transferSize": 71654
},
"children": {}
}
},
"75994.22": {
"request": {
Expand All @@ -425,8 +415,7 @@
"endTime": 185608.117509,
"responseReceivedTime": 185607.822806,
"transferSize": 30174
},
"children": {}
}
},
"75994.28": {
"request": {
Expand All @@ -435,8 +424,7 @@
"endTime": 185607.285454,
"responseReceivedTime": 185606.82147599998,
"transferSize": 821
},
"children": {}
}
}
}
},
Expand All @@ -447,8 +435,7 @@
"endTime": 185608.857719,
"responseReceivedTime": 185608.85586200003,
"transferSize": 221
},
"children": {}
}
}
},
"longestChain": {
Expand Down
2 changes: 1 addition & 1 deletion typings/audit.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ declare global {
responseReceivedTime: number;
transferSize: number;
};
children: SimpleCriticalRequestNode;
children?: SimpleCriticalRequestNode;
}
}

Expand Down

0 comments on commit 09cc741

Please sign in to comment.