@memlab/core 1.1.16 → 1.1.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -23,7 +23,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
24
  const fs_1 = __importDefault(require("fs"));
25
25
  const babar_1 = __importDefault(require("babar"));
26
- const chalk_1 = __importDefault(require("chalk"));
27
26
  const LeakClusterLogger_1 = __importDefault(require("../logger/LeakClusterLogger"));
28
27
  const LeakTraceDetailsLogger_1 = __importDefault(require("../logger/LeakTraceDetailsLogger"));
29
28
  const TraceFinder_1 = __importDefault(require("../paths/TraceFinder"));
@@ -42,156 +41,6 @@ class MemoryAnalyst {
42
41
  return yield this.detectMemoryLeaks();
43
42
  });
44
43
  }
45
- checkUnbound(options = {}) {
46
- return __awaiter(this, void 0, void 0, function* () {
47
- this.visualizeMemoryUsage(options);
48
- Utils_1.default.checkSnapshots(options);
49
- yield this.detectUnboundGrowth(options);
50
- });
51
- }
52
- breakDownMemoryByShapes(options = {}) {
53
- return __awaiter(this, void 0, void 0, function* () {
54
- const opt = { buildNodeIdIndex: true, verbose: true };
55
- const file = options.file ||
56
- Utils_1.default.getSnapshotFilePathWithTabType(/.*/) ||
57
- '<EMPTY_FILE_PATH>';
58
- const snapshot = yield Utils_1.default.getSnapshotFromFile(file, opt);
59
- this.preparePathFinder(snapshot);
60
- const heapInfo = this.getOverallHeapInfo(snapshot, { force: true });
61
- if (heapInfo) {
62
- this.printHeapInfo(heapInfo);
63
- }
64
- this.breakDownSnapshotByShapes(snapshot);
65
- });
66
- }
67
- // find any objects that keeps growing
68
- detectUnboundGrowth(options = {}) {
69
- return __awaiter(this, void 0, void 0, function* () {
70
- const nodeInfo = Object.create(null);
71
- let hasCheckedFirstSnapshot = false;
72
- let snapshot = null;
73
- const isValidNode = (node) => node.type === 'object' ||
74
- node.type === 'closure' ||
75
- node.type === 'regexp';
76
- const initNodeInfo = (node) => {
77
- if (!isValidNode(node)) {
78
- return;
79
- }
80
- const n = node.retainedSize;
81
- nodeInfo[node.id] = {
82
- type: node.type,
83
- name: node.name,
84
- min: n,
85
- max: n,
86
- history: [n],
87
- node,
88
- };
89
- };
90
- const updateNodeInfo = (node) => {
91
- const item = nodeInfo[node.id];
92
- if (!item) {
93
- return;
94
- }
95
- if (node.name !== item.name || node.type !== item.type) {
96
- nodeInfo[node.id] = null;
97
- return;
98
- }
99
- const n = node.retainedSize;
100
- // only monotonic increase?
101
- if (Config_1.default.monotonicUnboundGrowthOnly && n < item.max) {
102
- nodeInfo[node.id] = null;
103
- return;
104
- }
105
- item.history.push(n);
106
- item.max = Math.max(item.max, n);
107
- item.min = Math.min(item.min, n);
108
- };
109
- // summarize the heap objects info in current heap snapshot
110
- // this is mainly used for better understanding of the % of
111
- // objects released and allocated over time
112
- const maybeSummarizeNodeInfo = () => {
113
- if (!Config_1.default.verbose) {
114
- return;
115
- }
116
- let n = 0;
117
- for (const k in nodeInfo) {
118
- if (nodeInfo[k]) {
119
- ++n;
120
- }
121
- }
122
- Console_1.default.lowLevel(`Objects tracked: ${n}`);
123
- };
124
- Console_1.default.overwrite('Checking unbounded objects...');
125
- const snapshotFiles = options.snapshotDir
126
- ? // load snapshots from a directory
127
- Utils_1.default.getSnapshotFilesInDir(options.snapshotDir)
128
- : // load snapshots based on the visit sequence meta data
129
- Utils_1.default.getSnapshotFilesFromTabsOrder();
130
- for (const file of snapshotFiles) {
131
- // force GC before loading each snapshot
132
- if (global.gc) {
133
- global.gc();
134
- }
135
- // load and preprocess heap snapshot
136
- const opt = { buildNodeIdIndex: true, verbose: true };
137
- snapshot = yield Utils_1.default.getSnapshotFromFile(file, opt);
138
- this.calculateRetainedSizes(snapshot);
139
- // keep track of heap objects
140
- if (!hasCheckedFirstSnapshot) {
141
- // record Ids in the snapshot
142
- snapshot.nodes.forEach(initNodeInfo);
143
- hasCheckedFirstSnapshot = true;
144
- }
145
- else {
146
- snapshot.nodes.forEach(updateNodeInfo);
147
- maybeSummarizeNodeInfo();
148
- }
149
- }
150
- // exit if no heap snapshot found
151
- if (!hasCheckedFirstSnapshot) {
152
- return;
153
- }
154
- // post process and print the unbounded objects
155
- const idsInLastSnapshot = new Set();
156
- snapshot === null || snapshot === void 0 ? void 0 : snapshot.nodes.forEach(node => {
157
- idsInLastSnapshot.add(node.id);
158
- });
159
- let ids = [];
160
- for (const key in nodeInfo) {
161
- const id = parseInt(key, 10);
162
- const item = nodeInfo[id];
163
- if (!item) {
164
- continue;
165
- }
166
- if (!idsInLastSnapshot.has(id)) {
167
- continue;
168
- }
169
- if (item.min === item.max) {
170
- continue;
171
- }
172
- // filter out non-significant leaks
173
- if (item.history[item.history.length - 1] < Config_1.default.unboundSizeThreshold) {
174
- continue;
175
- }
176
- ids.push(Object.assign({ id }, item));
177
- }
178
- if (ids.length === 0) {
179
- Console_1.default.midLevel('No increasing objects found.');
180
- return;
181
- }
182
- ids = ids
183
- .sort((o1, o2) => o2.history[o2.history.length - 1] - o1.history[o1.history.length - 1])
184
- .slice(0, 20);
185
- // print on terminal
186
- const str = Serializer_1.default.summarizeUnboundedObjects(ids, { color: true });
187
- Console_1.default.topLevel('Top growing objects in sizes:');
188
- Console_1.default.lowLevel(' (Use `memlab trace --node-id=@ID` to get trace)');
189
- Console_1.default.topLevel('\n' + str);
190
- // save results to file
191
- const csv = Serializer_1.default.summarizeUnboundedObjectsToCSV(ids);
192
- fs_1.default.writeFileSync(Config_1.default.unboundObjectCSV, csv, 'UTF-8');
193
- });
194
- }
195
44
  // find all unique pattern of leaks
196
45
  detectMemoryLeaks() {
197
46
  return __awaiter(this, void 0, void 0, function* () {
@@ -368,11 +217,6 @@ class MemoryAnalyst {
368
217
  };
369
218
  });
370
219
  }
371
- calculateRetainedSizes(snapshot) {
372
- const finder = new TraceFinder_1.default();
373
- // dominator and retained size
374
- finder.calculateAllNodesRetainedSizes(snapshot);
375
- }
376
220
  // initialize the path finder
377
221
  preparePathFinder(snapshot) {
378
222
  const finder = new TraceFinder_1.default();
@@ -442,14 +286,6 @@ class MemoryAnalyst {
442
286
  Console_1.default.midLevel(`${leakedNodeIds.size} Fiber nodes and Detached elements`);
443
287
  }
444
288
  }
445
- aggregateDominatorMetrics(ids, snapshot, checkNodeCb, nodeMetricsCb) {
446
- let ret = 0;
447
- const dominators = Utils_1.default.getConditionalDominatorIds(ids, snapshot, checkNodeCb);
448
- Utils_1.default.applyToNodes(dominators, snapshot, node => {
449
- ret += nodeMetricsCb(node);
450
- });
451
- return ret;
452
- }
453
289
  getOverallHeapInfo(snapshot, options = {}) {
454
290
  if (!Config_1.default.verbose && !options.force) {
455
291
  return;
@@ -457,22 +293,19 @@ class MemoryAnalyst {
457
293
  Console_1.default.overwrite('summarizing heap info...');
458
294
  const allIds = Utils_1.default.getNodesIdSet(snapshot);
459
295
  const heapInfo = {
460
- fiberNodeSize: this.aggregateDominatorMetrics(allIds, snapshot, Utils_1.default.isFiberNode, this.getRetainedSize),
461
- regularFiberNodeSize: this.aggregateDominatorMetrics(allIds, snapshot, Utils_1.default.isRegularFiberNode, this.getRetainedSize),
462
- detachedFiberNodeSize: this.aggregateDominatorMetrics(allIds, snapshot, Utils_1.default.isDetachedFiberNode, this.getRetainedSize),
463
- alternateFiberNodeSize: this.aggregateDominatorMetrics(allIds, snapshot, Utils_1.default.isAlternateNode, this.getRetainedSize),
464
- error: this.aggregateDominatorMetrics(allIds, snapshot, node => node.name === 'Error', this.getRetainedSize),
296
+ fiberNodeSize: Utils_1.default.aggregateDominatorMetrics(allIds, snapshot, Utils_1.default.isFiberNode, Utils_1.default.getRetainedSize),
297
+ regularFiberNodeSize: Utils_1.default.aggregateDominatorMetrics(allIds, snapshot, Utils_1.default.isRegularFiberNode, Utils_1.default.getRetainedSize),
298
+ detachedFiberNodeSize: Utils_1.default.aggregateDominatorMetrics(allIds, snapshot, Utils_1.default.isDetachedFiberNode, Utils_1.default.getRetainedSize),
299
+ alternateFiberNodeSize: Utils_1.default.aggregateDominatorMetrics(allIds, snapshot, Utils_1.default.isAlternateNode, Utils_1.default.getRetainedSize),
300
+ error: Utils_1.default.aggregateDominatorMetrics(allIds, snapshot, node => node.name === 'Error', Utils_1.default.getRetainedSize),
465
301
  };
466
302
  return heapInfo;
467
303
  }
468
- getRetainedSize(node) {
469
- return node.retainedSize;
470
- }
471
304
  getOverallLeakInfo(leakedNodeIds, snapshot) {
472
305
  if (!Config_1.default.verbose) {
473
306
  return;
474
307
  }
475
- const leakInfo = Object.assign(Object.assign({}, this.getOverallHeapInfo(snapshot)), { leakedSize: this.aggregateDominatorMetrics(leakedNodeIds, snapshot, () => true, this.getRetainedSize), leakedFiberNodeSize: this.aggregateDominatorMetrics(leakedNodeIds, snapshot, Utils_1.default.isFiberNode, this.getRetainedSize), leakedAlternateFiberNodeSize: this.aggregateDominatorMetrics(leakedNodeIds, snapshot, Utils_1.default.isAlternateNode, this.getRetainedSize) });
308
+ const leakInfo = Object.assign(Object.assign({}, this.getOverallHeapInfo(snapshot)), { leakedSize: Utils_1.default.aggregateDominatorMetrics(leakedNodeIds, snapshot, () => true, Utils_1.default.getRetainedSize), leakedFiberNodeSize: Utils_1.default.aggregateDominatorMetrics(leakedNodeIds, snapshot, Utils_1.default.isFiberNode, Utils_1.default.getRetainedSize), leakedAlternateFiberNodeSize: Utils_1.default.aggregateDominatorMetrics(leakedNodeIds, snapshot, Utils_1.default.isAlternateNode, Utils_1.default.getRetainedSize) });
476
309
  return leakInfo;
477
310
  }
478
311
  printHeapInfo(leakInfo) {
@@ -485,111 +318,6 @@ class MemoryAnalyst {
485
318
  Console_1.default.topLevel(`· ${name}: ${value}`);
486
319
  });
487
320
  }
488
- breakDownSnapshotByShapes(snapshot) {
489
- Console_1.default.overwrite('Breaking down memory by shapes...');
490
- const breakdown = Object.create(null);
491
- const population = Object.create(null);
492
- // group objects based on their shapes
493
- snapshot.nodes.forEach(node => {
494
- if ((node.type !== 'object' && !Utils_1.default.isStringNode(node)) ||
495
- Config_1.default.nodeIgnoreSetInShape.has(node.name)) {
496
- return;
497
- }
498
- const key = Serializer_1.default.summarizeNodeShape(node);
499
- breakdown[key] = breakdown[key] || new Set();
500
- breakdown[key].add(node.id);
501
- if (population[key] === undefined) {
502
- population[key] = { examples: [], n: 0 };
503
- }
504
- ++population[key].n;
505
- // retain the top 5 examples
506
- const examples = population[key].examples;
507
- examples.push(node);
508
- examples.sort((n1, n2) => n2.retainedSize - n1.retainedSize);
509
- if (examples.length > 5) {
510
- examples.pop();
511
- }
512
- });
513
- // calculate and sort based on retained sizes
514
- const ret = [];
515
- for (const key in breakdown) {
516
- const size = this.aggregateDominatorMetrics(breakdown[key], snapshot, () => true, this.getRetainedSize);
517
- ret.push({ key, retainedSize: size });
518
- }
519
- ret.sort((o1, o2) => o2.retainedSize - o1.retainedSize);
520
- Console_1.default.topLevel('Object shapes with top retained sizes:');
521
- Console_1.default.lowLevel(' (Use `memlab trace --node-id=@ID` to get trace)\n');
522
- const topList = ret.slice(0, 40);
523
- // print settings
524
- const opt = { color: true, compact: true };
525
- const dot = chalk_1.default.grey('· ');
526
- const colon = chalk_1.default.grey(': ');
527
- // print the shapes with the biggest retained size
528
- for (const o of topList) {
529
- const referrerInfo = this.breakDownByReferrers(breakdown[o.key], snapshot);
530
- const { examples, n } = population[o.key];
531
- const shapeStr = Serializer_1.default.summarizeNodeShape(examples[0], opt);
532
- const bytes = Utils_1.default.getReadableBytes(o.retainedSize);
533
- const examplesStr = examples
534
- .map(e => `@${e.id} [${Utils_1.default.getReadableBytes(e.retainedSize)}]`)
535
- .join(' | ');
536
- const meta = chalk_1.default.grey(` (N: ${n}, Examples: ${examplesStr})`);
537
- Console_1.default.topLevel(`${dot}${shapeStr}${colon}${bytes}${meta}`);
538
- Console_1.default.lowLevel(referrerInfo + '\n');
539
- }
540
- }
541
- isTrivialEdgeForBreakDown(edge) {
542
- const source = edge.fromNode;
543
- return (source.type === 'array' ||
544
- source.name === '(object elements)' ||
545
- source.name === 'system' ||
546
- edge.name_or_index === '__proto__' ||
547
- edge.name_or_index === 'prototype');
548
- }
549
- breakDownByReferrers(ids, snapshot) {
550
- const edgeNames = Object.create(null);
551
- for (const id of ids) {
552
- const node = snapshot.getNodeById(id);
553
- for (const edge of (node === null || node === void 0 ? void 0 : node.referrers) || []) {
554
- const source = edge.fromNode;
555
- if (!Utils_1.default.isMeaningfulEdge(edge) ||
556
- this.isTrivialEdgeForBreakDown(edge)) {
557
- continue;
558
- }
559
- const sourceName = Serializer_1.default.summarizeNodeName(source, {
560
- color: false,
561
- });
562
- const edgeName = Serializer_1.default.summarizeEdgeName(edge, {
563
- color: false,
564
- abstract: true,
565
- });
566
- const edgeKey = `[${sourceName}] --${edgeName}--> `;
567
- edgeNames[edgeKey] = edgeNames[edgeKey] || {
568
- numberOfEdgesToNode: 0,
569
- source,
570
- edge,
571
- };
572
- ++edgeNames[edgeKey].numberOfEdgesToNode;
573
- }
574
- }
575
- const referrerInfo = Object.entries(edgeNames)
576
- .sort((i1, i2) => i2[1].numberOfEdgesToNode - i1[1].numberOfEdgesToNode)
577
- .slice(0, 4)
578
- .map(i => {
579
- const meta = i[1];
580
- const source = Serializer_1.default.summarizeNodeName(meta.source, {
581
- color: true,
582
- });
583
- const edgeName = Serializer_1.default.summarizeEdgeName(meta.edge, {
584
- color: true,
585
- abstract: true,
586
- });
587
- const edgeSummary = `${source} --${edgeName}-->`;
588
- return ` · ${edgeSummary}: ${meta.numberOfEdgesToNode}`;
589
- })
590
- .join('\n');
591
- return referrerInfo;
592
- }
593
321
  printHeapAndLeakInfo(leakedNodeIds, snapshot) {
594
322
  // write page interaction summary to the leaks text file
595
323
  this.dumpPageInteractionSummary();
@@ -616,10 +344,6 @@ class MemoryAnalyst {
616
344
  this.printHeapAndLeakInfo(leakedNodeIds, snapshot);
617
345
  // get all leaked objects
618
346
  this.filterLeakedObjects(leakedNodeIds, snapshot);
619
- if (Config_1.default.verbose) {
620
- // show a breakdown of different object structures
621
- this.breakDownSnapshotByShapes(snapshot);
622
- }
623
347
  const nodeIdInPaths = new Set();
624
348
  const paths = [];
625
349
  let numOfLeakedObjects = 0;
@@ -642,7 +366,7 @@ class MemoryAnalyst {
642
366
  Console_1.default.midLevel(`${numOfLeakedObjects} leaked objects`);
643
367
  }
644
368
  // cluster traces from the current run
645
- const clusters = TraceBucket_1.default.clusterPaths(paths, snapshot, this.aggregateDominatorMetrics, {
369
+ const clusters = TraceBucket_1.default.clusterPaths(paths, snapshot, Utils_1.default.aggregateDominatorMetrics, {
646
370
  strategy: Config_1.default.isMLClustering
647
371
  ? new MLTraceSimilarityStrategy_1.default()
648
372
  : undefined,
@@ -651,7 +375,7 @@ class MemoryAnalyst {
651
375
  yield this.serializeClusterUpdate(clusters);
652
376
  if (Config_1.default.logUnclassifiedClusters) {
653
377
  // cluster traces from the current run
654
- const clustersUnclassified = TraceBucket_1.default.generateUnClassifiedClusters(paths, snapshot, this.aggregateDominatorMetrics);
378
+ const clustersUnclassified = TraceBucket_1.default.generateUnClassifiedClusters(paths, snapshot, Utils_1.default.aggregateDominatorMetrics);
655
379
  LeakClusterLogger_1.default.logUnclassifiedClusters(clustersUnclassified);
656
380
  }
657
381
  return {
@@ -682,7 +406,7 @@ class MemoryAnalyst {
682
406
  }
683
407
  }, { reverse: true });
684
408
  // cluster traces from the current run
685
- const clusters = TraceBucket_1.default.clusterPaths(paths, snapshot, this.aggregateDominatorMetrics, {
409
+ const clusters = TraceBucket_1.default.clusterPaths(paths, snapshot, Utils_1.default.aggregateDominatorMetrics, {
686
410
  strategy: Config_1.default.isMLClustering
687
411
  ? new MLTraceSimilarityStrategy_1.default()
688
412
  : undefined,
@@ -8,7 +8,7 @@
8
8
  * @oncall web_perf_infra
9
9
  */
10
10
  import { ParsedArgs } from 'minimist';
11
- import type { LaunchOptions, Page } from 'puppeteer';
11
+ import type { LaunchOptions, Page as PuppeteerPage } from 'puppeteer';
12
12
  import type { ErrorHandling, MemLabConfig } from './Config';
13
13
  /** @internal */
14
14
  export declare type AnyValue = any;
@@ -43,6 +43,12 @@ export declare type HaltOrThrowOptions = {
43
43
  printCallback?: () => void;
44
44
  };
45
45
  /** @internal */
46
+ export declare type FileOption = {
47
+ workDir?: Optional<string>;
48
+ clear?: boolean;
49
+ transient?: boolean;
50
+ };
51
+ /** @internal */
46
52
  export declare type CLIOptions = {
47
53
  cliArgs: ParsedArgs;
48
54
  configFromOptions?: AnyRecord;
@@ -56,6 +62,12 @@ export declare type XvfbType = {
56
62
  display: () => string;
57
63
  };
58
64
  /** @internal */
65
+ export declare type ShellOptions = {
66
+ dir?: Optional<string>;
67
+ ignoreError?: Optional<boolean>;
68
+ disconnectStdio?: Optional<boolean>;
69
+ };
70
+ /** @internal */
59
71
  export declare type CLIArgs = {
60
72
  verbose: boolean;
61
73
  app: string;
@@ -85,6 +97,57 @@ export declare type CLIArgs = {
85
97
  'local-puppeteer': boolean;
86
98
  'snapshot-dir': string;
87
99
  };
100
+ /**
101
+ * This is the puppeteer [`Page`](https://pptr.dev/api/puppeteer.page)
102
+ * class used by MemLab. The puppeteer `Page` class instance provides
103
+ * APIs to interact with the web browser.
104
+ *
105
+ * The puppeteer `Page` type can be incompatible across different versions.
106
+ * Your local npm-installed puppeteer version may be different from
107
+ * the puppeteer used by MemLab. This may cause some type errors, for example:
108
+ *
109
+ * ```typescript
110
+ * import type {Page} from 'puppeteer';
111
+ * import type {RunOptions} from '@memlab/api';
112
+ *
113
+ * const runOptions: RunOptions = {
114
+ * scenario: {
115
+ * // initial page load url: Google Maps
116
+ * url: () => {
117
+ * return "https://www.google.com/maps/@37.386427,-122.0428214,11z";
118
+ * },
119
+ * // type error here if your local puppeeter version is different
120
+ * // from the puppeteer used by MemLab
121
+ * action: async function (page: Page) {
122
+ * await page.click('button[aria-label="Hotels"]');
123
+ * },
124
+ * },
125
+ * };
126
+ * ```
127
+ *
128
+ * To avoid the type error in the code example above, MemLab exports the
129
+ * puppeteer `Page` type used by MemLab so that your code can import it
130
+ * when necessary:
131
+ *
132
+ * ```typescript
133
+ * import type {Page} from '@memlab/core' // import Page type from memlab
134
+ * import type {RunOptions} from 'memlab';
135
+ *
136
+ * const runOptions: RunOptions = {
137
+ * scenario: {
138
+ * // initial page load url: Google Maps
139
+ * url: () => {
140
+ * return "https://www.google.com/maps/@37.386427,-122.0428214,11z";
141
+ * },
142
+ * // no type error here
143
+ * action: async function (page: Page) {
144
+ * await page.click('button[aria-label="Hotels"]');
145
+ * },
146
+ * },
147
+ * };
148
+ * ```
149
+ */
150
+ export declare type Page = PuppeteerPage;
88
151
  /**
89
152
  * the predicate callback is used to decide if a
90
153
  * entity of type `T`.
@@ -338,7 +401,8 @@ export declare type LeakFilterCallback = (node: IHeapNode, snapshot: IHeapSnapsh
338
401
  * For concrete examples, check out {@link action} or {@link back}.
339
402
  *
340
403
  * @param page the puppeteer [`Page`](https://pptr.dev/api/puppeteer.page)
341
- * object, which provides APIs to interact with the web browser
404
+ * object, which provides APIs to interact with the web browser.
405
+ * To import this type, check out {@link Page}.
342
406
  * @returns no return value
343
407
  */
344
408
  export declare type InteractionsCallback = (page: Page, args?: OperationArgs) => Promise<void>;
@@ -420,7 +484,8 @@ export interface IScenario {
420
484
  *
421
485
  * * **Parameters**:
422
486
  * * page: `Page` | the puppeteer [`Page`](https://pptr.dev/api/puppeteer.page)
423
- * object, which provides APIs to interact with the web browser
487
+ * object, which provides APIs to interact with the web browser. To import
488
+ * this type, check out {@link Page}.
424
489
  *
425
490
  * * **Examples**:
426
491
  * ```typescript
@@ -467,7 +532,8 @@ export interface IScenario {
467
532
  *
468
533
  * * **Parameters**:
469
534
  * * page: `Page` | the puppeteer [`Page`](https://pptr.dev/api/puppeteer.page)
470
- * object, which provides APIs to interact with the web browser
535
+ * object, which provides APIs to interact with the web browser. To import
536
+ * this type, check out {@link Page}.
471
537
  *
472
538
  * * **Examples**:
473
539
  * ```typescript
@@ -496,7 +562,8 @@ export interface IScenario {
496
562
  *
497
563
  * * **Parameters**:
498
564
  * * page: `Page` | the puppeteer [`Page`](https://pptr.dev/api/puppeteer.page)
499
- * object, which provides APIs to interact with the web browser
565
+ * object, which provides APIs to interact with the web browser. To import
566
+ * this type, check out {@link Page}.
500
567
  *
501
568
  * * **Examples**:
502
569
  * ```typescript
@@ -539,7 +606,8 @@ export interface IScenario {
539
606
  *
540
607
  * * **Parameters**:
541
608
  * * page: `Page` | the puppeteer [`Page`](https://pptr.dev/api/puppeteer.page)
542
- * object, which provides APIs to interact with the web browser
609
+ * object, which provides APIs to interact with the web browser. To import
610
+ * this type, check out {@link Page}.
543
611
  *
544
612
  * * **Examples**:
545
613
  * ```typescript
@@ -582,7 +650,8 @@ export interface IScenario {
582
650
  *
583
651
  * * **Parameters**:
584
652
  * * page: `Page` | the puppeteer [`Page`](https://pptr.dev/api/puppeteer.page)
585
- * object, which provides APIs to interact with the web browser
653
+ * object, which provides APIs to interact with the web browser. To import
654
+ * this type, check out {@link Page}.
586
655
  * * **Returns**: a boolean value, if it returns `true`, memlab will consider
587
656
  * the navigation completes, if it returns `false`, memlab will keep calling
588
657
  * this callback until it returns `true`. This is an async callback, you can
@@ -766,6 +835,7 @@ export interface IDataBuilder {
766
835
  * Callback function to provide if the page is loaded.
767
836
  * For concrete example, check out {@link isPageLoaded}.
768
837
  * @param page - puppeteer's [Page](https://pptr.dev/api/puppeteer.page/) object.
838
+ * To import this type, check out {@link Page}.
769
839
  * @returns a boolean value, if it returns `true`, memlab will consider
770
840
  * the navigation completes, if it returns `false`, memlab will keep calling
771
841
  * this callback until it returns `true`. This is an async callback, you can
@@ -773,6 +843,8 @@ export interface IDataBuilder {
773
843
  */
774
844
  export declare type CheckPageLoadCallback = (page: Page) => Promise<boolean>;
775
845
  /** @internal */
846
+ export declare type PageSetupCallback = (page: Page) => Promise<void>;
847
+ /** @internal */
776
848
  export interface IE2EScenarioVisitPlan {
777
849
  name: string;
778
850
  appName: string;
@@ -786,6 +858,7 @@ export interface IE2EScenarioVisitPlan {
786
858
  dataBuilder: Optional<IDataBuilder>;
787
859
  isPageLoaded?: CheckPageLoadCallback;
788
860
  scenario?: IScenario;
861
+ pageSetup?: PageSetupCallback;
789
862
  }
790
863
  /** @internal */
791
864
  export declare type OperationArgs = {
@@ -7,7 +7,7 @@
7
7
  * @format
8
8
  * @oncall web_perf_infra
9
9
  */
10
- import type { HaltOrThrowOptions } from './Types';
10
+ import type { HaltOrThrowOptions, HeapNodeIdSet, ShellOptions } from './Types';
11
11
  import type { Browser, Page } from 'puppeteer';
12
12
  import type { AnyAyncFunction, AnyOptions, E2EStepInfo, IHeapSnapshot, IHeapNode, IHeapEdge, IScenario, ILeakFilter, LeakTracePathItem, RunMetaInfo, RawHeapSnapshot, Nullable, Optional } from './Types';
13
13
  declare function isHermesInternalObject(node: IHeapNode): boolean;
@@ -122,7 +122,11 @@ declare function getError(maybeError: unknown): Error;
122
122
  declare function isNodeDominatedByDeletionsArray(node: IHeapNode): boolean;
123
123
  declare function getUniqueID(): string;
124
124
  declare function getClosureSourceUrl(node: IHeapNode): Nullable<string>;
125
+ export declare function runShell(command: string, options?: ShellOptions): Nullable<string>;
126
+ export declare function getRetainedSize(node: IHeapNode): number;
127
+ export declare function aggregateDominatorMetrics(ids: HeapNodeIdSet, snapshot: IHeapSnapshot, checkNodeCb: (node: IHeapNode) => boolean, nodeMetricsCb: (node: IHeapNode) => number): number;
125
128
  declare const _default: {
129
+ aggregateDominatorMetrics: typeof aggregateDominatorMetrics;
126
130
  applyToNodes: typeof applyToNodes;
127
131
  callAsync: typeof callAsync;
128
132
  camelCaseToReadableString: typeof camelCaseToReadableString;
@@ -148,6 +152,7 @@ declare const _default: {
148
152
  getReadableBytes: typeof getReadableBytes;
149
153
  getReadablePercent: typeof getReadablePercent;
150
154
  getReadableTime: typeof getReadableTime;
155
+ getRetainedSize: typeof getRetainedSize;
151
156
  getRunMetaFilePath: typeof getRunMetaFilePath;
152
157
  getScenarioName: typeof getScenarioName;
153
158
  getSingleSnapshotFileForAnalysis: typeof getSingleSnapshotFileForAnalysis;
@@ -215,6 +220,7 @@ declare const _default: {
215
220
  repeat: typeof repeat;
216
221
  resolveFilePath: typeof resolveFilePath;
217
222
  resolveSnapshotFilePath: typeof resolveSnapshotFilePath;
223
+ runShell: typeof runShell;
218
224
  setIsAlternateNode: typeof setIsAlternateNode;
219
225
  setIsRegularFiberNode: typeof setIsRegularFiberNode;
220
226
  shouldShowMoreInfo: typeof shouldShowMoreInfo;
package/dist/lib/Utils.js CHANGED
@@ -44,9 +44,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
44
44
  return (mod && mod.__esModule) ? mod : { "default": mod };
45
45
  };
46
46
  Object.defineProperty(exports, "__esModule", { value: true });
47
- exports.resolveSnapshotFilePath = void 0;
47
+ exports.aggregateDominatorMetrics = exports.getRetainedSize = exports.runShell = exports.resolveSnapshotFilePath = void 0;
48
48
  const fs_1 = __importDefault(require("fs"));
49
49
  const path_1 = __importDefault(require("path"));
50
+ const child_process_1 = __importDefault(require("child_process"));
50
51
  const process_1 = __importDefault(require("process"));
51
52
  const Config_1 = __importStar(require("./Config"));
52
53
  const Console_1 = __importDefault(require("./Console"));
@@ -1751,7 +1752,52 @@ function getClosureSourceUrl(node) {
1751
1752
  const url = (_b = (_a = urlNode === null || urlNode === void 0 ? void 0 : urlNode.toStringNode()) === null || _a === void 0 ? void 0 : _a.stringValue) !== null && _b !== void 0 ? _b : null;
1752
1753
  return url;
1753
1754
  }
1755
+ function runShell(command, options = {}) {
1756
+ var _a, _b, _c;
1757
+ const runningDir = (_b = (_a = options.dir) !== null && _a !== void 0 ? _a : Config_1.default.workDir) !== null && _b !== void 0 ? _b : FileManager_1.default.getTmpDir();
1758
+ const execOptions = {
1759
+ cwd: runningDir,
1760
+ stdio: options.disconnectStdio
1761
+ ? []
1762
+ : [process_1.default.stdin, process_1.default.stdout, process_1.default.stderr],
1763
+ };
1764
+ if (process_1.default.platform !== 'win32') {
1765
+ execOptions.shell = '/bin/bash';
1766
+ }
1767
+ let ret = null;
1768
+ try {
1769
+ ret = child_process_1.default.execSync(command, execOptions);
1770
+ }
1771
+ catch (ex) {
1772
+ if (Config_1.default.verbose) {
1773
+ if (ex instanceof Error) {
1774
+ Console_1.default.lowLevel(ex.message);
1775
+ Console_1.default.lowLevel((_c = ex.stack) !== null && _c !== void 0 ? _c : '');
1776
+ }
1777
+ }
1778
+ if (options.ignoreError === true) {
1779
+ return '';
1780
+ }
1781
+ __1.utils.haltOrThrow(`Error when executing command: ${command}`);
1782
+ }
1783
+ return ret && ret.toString('UTF-8');
1784
+ }
1785
+ exports.runShell = runShell;
1786
+ function getRetainedSize(node) {
1787
+ return node.retainedSize;
1788
+ }
1789
+ exports.getRetainedSize = getRetainedSize;
1790
+ function aggregateDominatorMetrics(ids, snapshot, checkNodeCb, nodeMetricsCb) {
1791
+ let ret = 0;
1792
+ const dominators = __1.utils.getConditionalDominatorIds(ids, snapshot, checkNodeCb);
1793
+ __1.utils.applyToNodes(dominators, snapshot, node => {
1794
+ ret += nodeMetricsCb(node);
1795
+ });
1796
+ return ret;
1797
+ }
1798
+ exports.aggregateDominatorMetrics = aggregateDominatorMetrics;
1754
1799
  exports.default = {
1800
+ aggregateDominatorMetrics,
1755
1801
  applyToNodes,
1756
1802
  callAsync,
1757
1803
  camelCaseToReadableString,
@@ -1777,6 +1823,7 @@ exports.default = {
1777
1823
  getReadableBytes,
1778
1824
  getReadablePercent,
1779
1825
  getReadableTime,
1826
+ getRetainedSize,
1780
1827
  getRunMetaFilePath,
1781
1828
  getScenarioName,
1782
1829
  getSingleSnapshotFileForAnalysis,
@@ -1844,6 +1891,7 @@ exports.default = {
1844
1891
  repeat,
1845
1892
  resolveFilePath,
1846
1893
  resolveSnapshotFilePath,
1894
+ runShell,
1847
1895
  setIsAlternateNode,
1848
1896
  setIsRegularFiberNode,
1849
1897
  shouldShowMoreInfo,
@@ -257,10 +257,9 @@ class HeapNode {
257
257
  get location() {
258
258
  const heapSnapshot = this.heapSnapshot;
259
259
  const locationIdx = heapSnapshot._nodeIdx2LocationIdx[this.idx];
260
- if (locationIdx == null) {
261
- return null;
262
- }
263
- return new HeapLocation_1.default(heapSnapshot, locationIdx);
260
+ return locationIdx == null
261
+ ? null
262
+ : new HeapLocation_1.default(heapSnapshot, locationIdx);
264
263
  }
265
264
  // search reference by edge name and edge type
266
265
  getReference(edgeName, edgeType) {