Repository: jlfwong/speedscope
Pull request #71 (2018-06-25 07:50:45)

Clean up Chrome profiles
This cleans up imported Chrome profiles in 2 ways:

1. It places `(program)` frames on top of the previous call stack, just like we already did for `(garbage collector)`
2. It completely removes `(root)` and `(idle)` frames, since they aren't semantically useful in the profiles

CC @evanw

coveralls: ## Pull Request Test Coverage Report for [Build 146](https://coveralls.io/builds/17669575)

* **2** of **2**   **(100.0%)**  changed or added relevant lines in **1** file are covered.
* No unchanged relevant lines lost coverage.
* Overall coverage increased (+**0.1%**) to **36.778%**

---



|  Totals | [![Coverage Status](https://coveralls.io/builds/17669575/badge)](https://coveralls.io/builds/17669575) |
| :-- | --: |
| Change from base [Build 141](https://coveralls.io/builds/17629485): |  0.1% |
| Covered Lines: | 1006 |
| Relevant Lines: | 2620 |

---
##### 💛  - [Coveralls](https://coveralls.io)


Patch:
diff --git a/import/chrome.ts b/import/chrome.ts
index 5e4056a..820164a 100644
--- a/import/chrome.ts
+++ b/import/chrome.ts
@@ -74,6 +74,14 @@ function frameInfoForCallFrame(callFrame: CPUProfileCallFrame) {
   })
 }
 
+function shouldIgnoreFunction(functionName: string) {
+  return functionName === '(root)' || functionName === '(idle)'
+}
+
+function shouldPlaceOnTopOfPreviousStack(functionName: string) {
+  return functionName === '(garbage collector)' || functionName === '(program)'
+}
+
 export function importFromChromeCPUProfile(chromeProfile: CPUProfile): Profile {
   const profile = new CallTreeProfileBuilder(chromeProfile.endTime - chromeProfile.startTime)
 
@@ -131,10 +139,9 @@ export function importFromChromeCPUProfile(chromeProfile: CPUProfile): Profile {
     for (
       lca = stackTop;
       lca && prevStack.indexOf(lca) === -1;
-      lca =
-        lca.callFrame.functionName === '(garbage collector)'
-          ? lastOf(prevStack)
-          : lca.parent || null
+      lca = shouldPlaceOnTopOfPreviousStack(lca.callFrame.functionName)
+        ? lastOf(prevStack)
+        : lca.parent || null
     ) {}
 
     // Close frames that are no longer open
@@ -148,12 +155,11 @@ export function importFromChromeCPUProfile(chromeProfile: CPUProfile): Profile {
     const toOpen: CPUProfileNode[] = []
     for (
       let node: CPUProfileNode | null = stackTop;
-      node && node != lca;
-      // Place GC calls on top of the previous call stack
-      node =
-        node.callFrame.functionName === '(garbage collector)'
-          ? lastOf(prevStack)
-          : node.parent || null
+      node && node != lca && !shouldIgnoreFunction(node.callFrame.functionName);
+      // Place Chrome internal functions on top of the previous call stack
+      node = shouldPlaceOnTopOfPreviousStack(node.callFrame.functionName)
+        ? lastOf(prevStack)
+        : node.parent || null
     ) {
       toOpen.push(node)
     }


Test patch:
diff --git a/import/__snapshots__/chrome.test.ts.snap b/import/__snapshots__/chrome.test.ts.snap
index 5620455..42b56f6 100644
--- a/import/__snapshots__/chrome.test.ts.snap
+++ b/import/__snapshots__/chrome.test.ts.snap
@@ -3,15 +3,6 @@
 exports[`importFromChromeCPUProfile 1`] = `
 Object {
   "frames": Array [
-    Frame {
-      "col": -1,
-      "file": "",
-      "key": "(root)::-1:-1",
-      "line": -1,
-      "name": "(root)",
-      "selfWeight": 0,
-      "totalWeight": 45920,
-    },
     Frame {
       "col": 0,
       "file": "",
@@ -59,9 +50,9 @@ Object {
     },
   ],
   "stacks": Array [
-    "(root);(anonymous) 11.57ms",
-    "(root);(anonymous);a;b;d 16.79ms",
-    "(root);(anonymous);a;c;d 17.56ms",
+    "(anonymous) 11.57ms",
+    "(anonymous);a;b;d 16.79ms",
+    "(anonymous);a;c;d 17.56ms",
   ],
 }
 `;
@@ -69,15 +60,6 @@ Object {
 exports[`importFromChromeTimeline 1`] = `
 Object {
   "frames": Array [
-    Frame {
-      "col": -1,
-      "file": "",
-      "key": "(root)::-1:-1",
-      "line": -1,
-      "name": "(root)",
-      "selfWeight": 0,
-      "totalWeight": 555107,
-    },
     Frame {
       "col": -1,
       "file": "",
@@ -87,15 +69,6 @@ Object {
       "selfWeight": 12244,
       "totalWeight": 12244,
     },
-    Frame {
-      "col": -1,
-      "file": "",
-      "key": "(idle)::-1:-1",
-      "line": -1,
-      "name": "(idle)",
-      "selfWeight": 509679,
-      "totalWeight": 509679,
-    },
     Frame {
       "col": 10,
       "file": "file:///Users/jlfwong/code/speedscope/sample/programs/javascript/simple.js",
@@ -125,160 +98,160 @@ Object {
     },
   ],
   "stacks": Array [
-    "(root);(program) 10.16ms",
-    "(root);(idle) 1.99ms",
-    "(root);(program) 926.00µs",
-    "(root);(idle) 85.95ms",
-    "(root);(program) 133.00µs",
-    "(root);(idle) 8.40ms",
-    "(root);(program) 134.00µs",
-    "(root);a;c 257.00µs",
-    "(root);a;b 534.00µs",
-    "(root);a;c 513.00µs",
-    "(root);a;b 513.00µs",
-    "(root);a;c 257.00µs",
-    "(root);a;b 518.00µs",
-    "(root);a;c 514.00µs",
-    "(root);a;b 261.00µs",
-    "(root);a;c 517.00µs",
-    "(root);a;b 258.00µs",
-    "(root);a;c 145.00µs",
-    "(root);a;b 134.00µs",
-    "(root);a;c 292.00µs",
-    "(root);a;b 272.00µs",
-    "(root);a;c 129.00µs",
-    "(root);a;b 145.00µs",
-    "(root);a;c 265.00µs",
-    "(root);a;b 142.00µs",
-    "(root);a;c 141.00µs",
-    "(root);a;b 139.00µs",
-    "(root);a;c 397.00µs",
-    "(root);a;b 126.00µs",
-    "(root);a;c 131.00µs",
-    "(root);a;b 126.00µs",
-    "(root);a;c 521.00µs",
-    "(root);a;b 128.00µs",
-    "(root);a;c 261.00µs",
-    "(root);a;b 127.00µs",
-    "(root);a;c 130.00µs",
-    "(root);a;b 128.00µs",
-    "(root);a;c 136.00µs",
-    "(root);a;b 272.00µs",
-    "(root);a;c 146.00µs",
-    "(root);a;b 661.00µs",
-    "(root);a;c 113.00µs",
-    "(root);a;b 135.00µs",
-    "(root);a;c 585.00µs",
-    "(root);a;b 294.00µs",
-    "(root);a;c 274.00µs",
-    "(root);a;b 129.00µs",
-    "(root);a;c 127.00µs",
-    "(root);a;b 128.00µs",
-    "(root);a;c 247.00µs",
-    "(root);a;b 526.00µs",
-    "(root);a;c 125.00µs",
-    "(root);a;b 129.00µs",
-    "(root);a;c 131.00µs",
-    "(root);a;b 253.00µs",
-    "(root);a;c 135.00µs",
-    "(root);a;b 123.00µs",
-    "(root);a;c 128.00µs",
-    "(root);a;b 257.00µs",
-    "(root);a;c 137.00µs",
-    "(root);a;b 256.00µs",
-    "(root);a;c 411.00µs",
-    "(root);a;b 892.00µs",
-    "(root);a;c 146.00µs",
-    "(root);a;b 148.00µs",
-    "(root);a;c 309.00µs",
-    "(root);a;b 140.00µs",
-    "(root);a;c 151.00µs",
-    "(root);a;b 458.00µs",
-    "(root);a;c 156.00µs",
-    "(root);a;b 170.00µs",
-    "(root);a;c 139.00µs",
-    "(root);a;b 290.00µs",
-    "(root);a;c 115.00µs",
-    "(root);a;b 281.00µs",
-    "(root);a;c 270.00µs",
-    "(root);a;b 283.00µs",
-    "(root);a;c 144.00µs",
-    "(root);a;b 146.00µs",
-    "(root);a;c 410.00µs",
-    "(root);a;b 146.00µs",
-    "(root);a;c 147.00µs",
-    "(root);a;b 312.00µs",
-    "(root);a;c 146.00µs",
-    "(root);a;b 907.00µs",
-    "(root);a;c 231.00µs",
-    "(root);a;b 138.00µs",
-    "(root);a;c 280.00µs",
-    "(root);a;b 255.00µs",
-    "(root);a;c 257.00µs",
-    "(root);a;b 130.00µs",
-    "(root);a;c 255.00µs",
-    "(root);a;b 128.00µs",
-    "(root);a;c 526.00µs",
-    "(root);a;b 128.00µs",
-    "(root);a;c 384.00µs",
-    "(root);a;b 398.00µs",
-    "(root);a;c 260.00µs",
-    "(root);a;b 664.00µs",
-    "(root);a;c 150.00µs",
-    "(root);a;b 147.00µs",
-    "(root);a;c 156.00µs",
-    "(root);a;b 137.00µs",
-    "(root);a;c 141.00µs",
-    "(root);a;b 140.00µs",
-    "(root);a;c 141.00µs",
-    "(root);a;b 141.00µs",
-    "(root);a;c 140.00µs",
-    "(root);a;b 140.00µs",
-    "(root);a;c 139.00µs",
-    "(root);a;b 129.00µs",
-    "(root);a;c 269.00µs",
-    "(root);a;b 293.00µs",
-    "(root);a;c 148.00µs",
-    "(root);a;b 293.00µs",
-    "(root);a;c 292.00µs",
-    "(root);a;b 296.00µs",
-    "(root);a;c 147.00µs",
-    "(root);a;b 140.00µs",
-    "(root);a;c 139.00µs",
-    "(root);a;b 139.00µs",
-    "(root);a;c 511.00µs",
-    "(root);a;b 515.00µs",
-    "(root);a;c 386.00µs",
-    "(root);a;b 514.00µs",
-    "(root);a;c 272.00µs",
-    "(root);a;b 242.00µs",
-    "(root);a;c 128.00µs",
-    "(root);a;b 134.00µs",
-    "(root);a;c 112.00µs",
-    "(root);a;b 288.00µs",
-    "(root);a;c 451.00µs",
-    "(root);a;b 138.00µs",
-    "(root);a;c 292.00µs",
-    "(root);a;b 145.00µs",
-    "(root);a;c 259.00µs",
-    "(root);a;b 148.00µs",
-    "(root);a;c 293.00µs",
-    "(root);a;b 140.00µs",
-    "(root);a;c 140.00µs",
-    "(root);(idle) 61.61ms",
-    "(root);(program) 118.00µs",
-    "(root);(idle) 134.00µs",
-    "(root);(program) 128.00µs",
-    "(root);(idle) 16.40ms",
-    "(root);(program) 125.00µs",
-    "(root);(idle) 16.64ms",
-    "(root);(program) 129.00µs",
-    "(root);(idle) 16.72ms",
-    "(root);(program) 130.00µs",
-    "(root);(idle) 294.15ms",
-    "(root);(program) 129.00µs",
-    "(root);(idle) 7.68ms",
-    "(root);(program) 131.00µs",
+    "(program) 10.16ms",
+    " 1.99ms",
+    "(program) 926.00µs",
+    " 85.95ms",
+    "(program) 133.00µs",
+    " 8.40ms",
+    "(program) 134.00µs",
+    "a;c 257.00µs",
+    "a;b 534.00µs",
+    "a;c 513.00µs",
+    "a;b 513.00µs",
+    "a;c 257.00µs",
+    "a;b 518.00µs",
+    "a;c 514.00µs",
+    "a;b 261.00µs",
+    "a;c 517.00µs",
+    "a;b 258.00µs",
+    "a;c 145.00µs",
+    "a;b 134.00µs",
+    "a;c 292.00µs",
+    "a;b 272.00µs",
+    "a;c 129.00µs",
+    "a;b 145.00µs",
+    "a;c 265.00µs",
+    "a;b 142.00µs",
+    "a;c 141.00µs",
+    "a;b 139.00µs",
+    "a;c 397.00µs",
+    "a;b 126.00µs",
+    "a;c 131.00µs",
+    "a;b 126.00µs",
+    "a;c 521.00µs",
+    "a;b 128.00µs",
+    "a;c 261.00µs",
+    "a;b 127.00µs",
+    "a;c 130.00µs",
+    "a;b 128.00µs",
+    "a;c 136.00µs",
+    "a;b 272.00µs",
+    "a;c 146.00µs",
+    "a;b 661.00µs",
+    "a;c 113.00µs",
+    "a;b 135.00µs",
+    "a;c 585.00µs",
+    "a;b 294.00µs",
+    "a;c 274.00µs",
+    "a;b 129.00µs",
+    "a;c 127.00µs",
+    "a;b 128.00µs",
+    "a;c 247.00µs",
+    "a;b 526.00µs",
+    "a;c 125.00µs",
+    "a;b 129.00µs",
+    "a;c 131.00µs",
+    "a;b 253.00µs",
+    "a;c 135.00µs",
+    "a;b 123.00µs",
+    "a;c 128.00µs",
+    "a;b 257.00µs",
+    "a;c 137.00µs",
+    "a;b 256.00µs",
+    "a;c 411.00µs",
+    "a;b 892.00µs",
+    "a;c 146.00µs",
+    "a;b 148.00µs",
+    "a;c 309.00µs",
+    "a;b 140.00µs",
+    "a;c 151.00µs",
+    "a;b 458.00µs",
+    "a;c 156.00µs",
+    "a;b 170.00µs",
+    "a;c 139.00µs",
+    "a;b 290.00µs",
+    "a;c 115.00µs",
+    "a;b 281.00µs",
+    "a;c 270.00µs",
+    "a;b 283.00µs",
+    "a;c 144.00µs",
+    "a;b 146.00µs",
+    "a;c 410.00µs",
+    "a;b 146.00µs",
+    "a;c 147.00µs",
+    "a;b 312.00µs",
+    "a;c 146.00µs",
+    "a;b 907.00µs",
+    "a;c 231.00µs",
+    "a;b 138.00µs",
+    "a;c 280.00µs",
+    "a;b 255.00µs",
+    "a;c 257.00µs",
+    "a;b 130.00µs",
+    "a;c 255.00µs",
+    "a;b 128.00µs",
+    "a;c 526.00µs",
+    "a;b 128.00µs",
+    "a;c 384.00µs",
+    "a;b 398.00µs",
+    "a;c 260.00µs",
+    "a;b 664.00µs",
+    "a;c 150.00µs",
+    "a;b 147.00µs",
+    "a;c 156.00µs",
+    "a;b 137.00µs",
+    "a;c 141.00µs",
+    "a;b 140.00µs",
+    "a;c 141.00µs",
+    "a;b 141.00µs",
+    "a;c 140.00µs",
+    "a;b 140.00µs",
+    "a;c 139.00µs",
+    "a;b 129.00µs",
+    "a;c 269.00µs",
+    "a;b 293.00µs",
+    "a;c 148.00µs",
+    "a;b 293.00µs",
+    "a;c 292.00µs",
+    "a;b 296.00µs",
+    "a;c 147.00µs",
+    "a;b 140.00µs",
+    "a;c 139.00µs",
+    "a;b 139.00µs",
+    "a;c 511.00µs",
+    "a;b 515.00µs",
+    "a;c 386.00µs",
+    "a;b 514.00µs",
+    "a;c 272.00µs",
+    "a;b 242.00µs",
+    "a;c 128.00µs",
+    "a;b 134.00µs",
+    "a;c 112.00µs",
+    "a;b 288.00µs",
+    "a;c 451.00µs",
+    "a;b 138.00µs",
+    "a;c 292.00µs",
+    "a;b 145.00µs",
+    "a;c 259.00µs",
+    "a;b 148.00µs",
+    "a;c 293.00µs",
+    "a;b 140.00µs",
+    "a;c 140.00µs",
+    " 61.61ms",
+    "(program) 118.00µs",
+    " 134.00µs",
+    "(program) 128.00µs",
+    " 16.40ms",
+    "(program) 125.00µs",
+    " 16.64ms",
+    "(program) 129.00µs",
+    " 16.72ms",
+    "(program) 130.00µs",
+    " 294.15ms",
+    "(program) 129.00µs",
+    " 7.68ms",
+    "(program) 131.00µs",
   ],
 }
 `;