forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory-helpers.ts
412 lines (361 loc) · 14.8 KB
/
memory-helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {assert} from 'chai';
import type * as puppeteer from 'puppeteer-core';
import {
$,
$$,
click,
clickElement,
getBrowserAndPages,
pasteText,
platform,
waitFor,
waitForAria,
waitForElementWithTextContent,
waitForFunction,
waitForNone,
} from '../../shared/helper.js';
const NEW_HEAP_SNAPSHOT_BUTTON = 'devtools-button[aria-label="Take heap snapshot"]';
const MEMORY_PANEL_CONTENT = 'div[aria-label="Memory panel"]';
const PROFILE_TREE_SIDEBAR = 'div.profiles-tree-sidebar';
export const MEMORY_TAB_ID = '#tab-heap-profiler';
const CLASS_FILTER_INPUT = 'div[aria-placeholder="Filter by class"]';
const SELECTED_RESULT = '#profile-views table.data tr.data-grid-data-grid-node.revealed.parent.selected';
export async function navigateToMemoryTab() {
await click(MEMORY_TAB_ID);
await waitFor(MEMORY_PANEL_CONTENT);
await waitFor(PROFILE_TREE_SIDEBAR);
}
export async function takeDetachedElementsProfile() {
const radioButton = await $('//label[text()="Detached elements"]', undefined, 'xpath');
await clickElement(radioButton);
await click('devtools-button[aria-label="Obtain detached elements"]');
await waitForNone('.heap-snapshot-sidebar-tree-item.wait');
await waitFor('.heap-snapshot-sidebar-tree-item.selected');
}
export async function takeAllocationProfile() {
const radioButton = await $('//label[text()="Allocation sampling"]', undefined, 'xpath');
await clickElement(radioButton);
await click('devtools-button[aria-label="Start heap profiling"]');
await new Promise(r => setTimeout(r, 200));
await click('devtools-button[aria-label="Stop heap profiling"]');
await waitForNone('.heap-snapshot-sidebar-tree-item.wait');
await waitFor('.heap-snapshot-sidebar-tree-item.selected');
}
export async function takeAllocationTimelineProfile({recordStacks}: {recordStacks: boolean} = {
recordStacks: false,
}) {
const radioButton = await $('//label[text()="Allocations on timeline"]', undefined, 'xpath');
await clickElement(radioButton);
if (recordStacks) {
await click('[title="Allocation stack traces (more overhead)"]');
}
await click('devtools-button[aria-label="Start recording heap profile"]');
await new Promise(r => setTimeout(r, 200));
await click('devtools-button[aria-label="Stop recording heap profile"]');
await waitForNone('.heap-snapshot-sidebar-tree-item.wait');
await waitFor('.heap-snapshot-sidebar-tree-item.selected');
}
export async function takeHeapSnapshot(name: string = 'Snapshot 1') {
await click(NEW_HEAP_SNAPSHOT_BUTTON);
await waitForNone('.heap-snapshot-sidebar-tree-item.wait');
await waitForFunction(async () => {
const selected = await waitFor('.heap-snapshot-sidebar-tree-item.selected');
const title = await waitFor('span.title', selected);
return (await title.evaluate(e => e.textContent)) === name ? title : undefined;
});
}
export async function waitForHeapSnapshotData() {
await waitFor('#profile-views');
await waitFor('#profile-views .data-grid');
const rowCountMatches = async () => {
const rows = await getDataGridRows('#profile-views table.data');
if (rows.length > 0) {
return rows;
}
return undefined;
};
return await waitForFunction(rowCountMatches);
}
export async function waitForNonEmptyHeapSnapshotData() {
const rows = await waitForHeapSnapshotData();
assert.isTrue(rows.length > 0);
}
export async function getDataGridRows(selector: string) {
// The grid in Memory Tab contains a tree
const grid = await waitFor(selector);
return await $$('.data-grid-data-grid-node', grid);
}
export async function setClassFilter(text: string) {
const classFilter = await waitFor(CLASS_FILTER_INPUT);
await classFilter.focus();
void pasteText(text);
}
export async function triggerLocalFindDialog(frontend: puppeteer.Page) {
switch (platform) {
case 'mac':
await frontend.keyboard.down('Meta');
break;
default:
await frontend.keyboard.down('Control');
}
await frontend.keyboard.press('f');
switch (platform) {
case 'mac':
await frontend.keyboard.up('Meta');
break;
default:
await frontend.keyboard.up('Control');
}
}
export async function setSearchFilter(text: string) {
const {frontend} = getBrowserAndPages();
const grid = await waitFor('#profile-views table.data');
await grid.focus();
await triggerLocalFindDialog(frontend);
const SEARCH_QUERY = '[aria-label="Find"]';
const inputElement = await waitFor(SEARCH_QUERY);
if (!inputElement) {
assert.fail('Unable to find search input field');
}
await inputElement.focus();
await inputElement.type(text);
}
export async function waitForSearchResultNumber(results: number) {
const findMatch = async () => {
const currentMatch = await waitFor('.search-results-matches');
const currentTextContent = currentMatch && await currentMatch.evaluate(el => el.textContent);
if (currentTextContent && currentTextContent.endsWith(` ${results}`)) {
return currentMatch;
}
return undefined;
};
return await waitForFunction(findMatch);
}
export async function findSearchResult(searchResult: string, pollIntrerval: number = 500) {
const {frontend} = getBrowserAndPages();
const match = await waitFor('#profile-views table.data');
const matches = await waitFor(' .search-results-matches');
const matchesText = await matches.evaluate(async element => {
return element.textContent;
});
if (matchesText === '1 of 1') {
await waitForElementWithTextContent(searchResult, match);
} else {
await waitForFunction(async () => {
const selectedBefore = await waitFor(SELECTED_RESULT);
await click('[aria-label="Show next result"]');
// Wait until the click has taken effect by checking that the selected
// result has changed. This is done to prevent the assertion afterwards
// from happening before the next result is fully loaded.
await waitForFunction(async () => {
const selectedAfter = await waitFor(SELECTED_RESULT);
return await frontend.evaluate((b, a) => {
return b !== a;
}, selectedBefore, selectedAfter);
});
const result = Promise.race([
waitForElementWithTextContent(searchResult, match),
new Promise(resolve => {
setTimeout(resolve, pollIntrerval, false);
}),
]);
return result;
});
}
}
const normalizRetainerName = (retainerName: string) => {
// Retainers starting with `Window /` might have host information in their
// name, including the port, so we need to strip that. We can't distinguish
// Window from Window / either, because on Mac it is often just Window.
if (retainerName.startsWith('Window /')) {
return 'Window';
}
// Retainers including double-colons :: are names from the C++ implementation
// exposed through V8's gn arg `cppgc_enable_object_names`; these should be
// considered implementation details, so we normalize them.
if (retainerName.includes('::')) {
if (retainerName.startsWith('Detached')) {
return 'Detached InternalNode';
}
return 'InternalNode';
}
return retainerName;
};
interface RetainerChainEntry {
propertyName: string;
retainerClassName: string;
}
export async function assertRetainerChainSatisfies(p: (retainerChain: Array<RetainerChainEntry>) => boolean) {
// Give some time for the expansion to finish.
const retainerGridElements = await getDataGridRows('.retaining-paths-view table.data');
const retainerChain = [];
for (let i = 0; i < retainerGridElements.length; ++i) {
const retainer = retainerGridElements[i];
const propertyName = await retainer.$eval('span.property-name', el => el.textContent);
const rawRetainerClassName = await retainer.$eval('span.value', el => el.textContent);
if (!propertyName) {
assert.fail('Could not get retainer name');
}
if (!rawRetainerClassName) {
assert.fail('Could not get retainer value');
}
const retainerClassName = normalizRetainerName(rawRetainerClassName);
retainerChain.push({propertyName, retainerClassName});
if (await retainer.evaluate(el => !el.classList.contains('expanded'))) {
// Only follow the shortest retainer chain to the end. This relies on
// the retainer view behavior that auto-expands the shortest retaining
// chain.
break;
}
}
return p(retainerChain);
}
export async function waitUntilRetainerChainSatisfies(p: (retainerChain: Array<RetainerChainEntry>) => boolean) {
await waitForFunction(assertRetainerChainSatisfies.bind(null, p));
}
export function appearsInOrder(targetArray: string[], inputArray: string[]) {
let i = 0;
let j = 0;
if (inputArray.length > targetArray.length) {
return false;
}
if (inputArray === targetArray) {
return true;
}
while (i < targetArray.length && j < inputArray.length) {
if (inputArray[j] === targetArray[i]) {
j++;
}
i++;
}
if (j === inputArray.length) {
return true;
}
return false;
}
export async function waitForRetainerChain(expectedRetainers: Array<string>) {
await waitForFunction(assertRetainerChainSatisfies.bind(null, retainerChain => {
const actual = retainerChain.map(e => e.retainerClassName);
return appearsInOrder(actual, expectedRetainers);
}));
}
export async function changeViewViaDropdown(newPerspective: string) {
const perspectiveDropdownSelector = 'select[aria-label="Perspective"]';
const dropdown = await waitFor(perspectiveDropdownSelector) as puppeteer.ElementHandle<HTMLSelectElement>;
const optionToSelect = await waitForElementWithTextContent(newPerspective, dropdown);
const optionValue = await optionToSelect.evaluate(opt => opt.getAttribute('value'));
if (!optionValue) {
throw new Error(`Could not find heap snapshot perspective option: ${newPerspective}`);
}
await dropdown.select(optionValue);
}
export async function changeAllocationSampleViewViaDropdown(newPerspective: string) {
const perspectiveDropdownSelector = 'select[aria-label="Profile view mode"]';
const dropdown = await waitFor(perspectiveDropdownSelector) as puppeteer.ElementHandle<HTMLSelectElement>;
const optionToSelect = await waitForElementWithTextContent(newPerspective, dropdown);
const optionValue = await optionToSelect.evaluate(opt => opt.getAttribute('value'));
if (!optionValue) {
throw new Error(`Could not find heap snapshot perspective option: ${newPerspective}`);
}
await dropdown.select(optionValue);
}
export async function focusTableRowWithName(text: string) {
const row = await waitFor(`//span[text()="${text}"]/ancestor::tr`, undefined, undefined, 'xpath');
await focusTableRow(row);
}
export async function focusTableRow(row: puppeteer.ElementHandle<Element>) {
// Click in a numeric cell, to avoid accidentally clicking a link.
const cell = await waitFor('.numeric-column', row);
await clickElement(cell);
}
export async function expandFocusedRow() {
const {frontend} = getBrowserAndPages();
await frontend.keyboard.press('ArrowRight');
await waitFor('.selected.data-grid-data-grid-node.expanded');
}
function parseByteString(str: string): number {
const number = parseFloat(str);
if (str.endsWith('kB')) {
return number * 1000;
}
if (str.endsWith('MB')) {
return number * 1000 * 1000;
}
if (str.endsWith('GB')) {
return number * 1000 * 1000 * 1000;
}
return number;
}
async function getSizesFromRow(row: puppeteer.ElementHandle<Element>) {
const numericData = await $$('.numeric-column>.profile-multiple-values>span', row);
assert.lengthOf(numericData, 4);
function readNumber(e: Element): string {
return e.textContent as string;
}
const shallowSize = parseByteString(await numericData[0].evaluate(readNumber));
const retainedSize = parseByteString(await numericData[2].evaluate(readNumber));
assert.isTrue(retainedSize >= shallowSize);
return {shallowSize, retainedSize};
}
export async function getSizesFromSelectedRow() {
const row = await waitFor('.selected.data-grid-data-grid-node');
return await getSizesFromRow(row);
}
export async function getCategoryRow(text: string, wait: boolean = true) {
const selector = `//td[text()="${text}"]/ancestor::tr`;
return await (wait ? waitFor(selector, undefined, undefined, 'xpath') : $(selector, undefined, 'xpath'));
}
export async function getSizesFromCategoryRow(text: string) {
const row = await getCategoryRow(text);
return await getSizesFromRow(row);
}
export async function getDistanceFromCategoryRow(text: string) {
const row = await getCategoryRow(text);
const numericColumns = await $$('.numeric-column', row);
return await numericColumns[0].evaluate(e => parseInt(e.textContent as string, 10));
}
export async function getCountFromCategoryRowWithName(text: string) {
const row = await getCategoryRow(text);
return await getCountFromCategoryRow(row);
}
export async function getCountFromCategoryRow(row: puppeteer.ElementHandle<Element>) {
const countSpan = await waitFor('.objects-count', row);
return await countSpan.evaluate(e => parseInt((e.textContent ?? '').substring(1), 10));
}
export async function getAddedCountFromComparisonRowWithName(text: string) {
const row = await getCategoryRow(text);
return await getAddedCountFromComparisonRow(row);
}
export async function getAddedCountFromComparisonRow(row: puppeteer.ElementHandle<Element>) {
const addedCountCell = await waitFor('.addedCount-column', row);
const countText = await addedCountCell.evaluate(e => e.textContent ?? '');
return parseByteString(countText);
}
export async function getRemovedCountFromComparisonRow(row: puppeteer.ElementHandle<Element>) {
const addedCountCell = await waitFor('.removedCount-column', row);
const countText = await addedCountCell.evaluate(e => e.textContent ?? '');
return parseByteString(countText);
}
export async function clickOnContextMenuForRetainer(retainerName: string, menuItem: string) {
const retainersPane = await waitFor('.retaining-paths-view');
const element = await waitFor(`//span[text()="${retainerName}"]`, retainersPane, undefined, 'xpath');
// Push the click right a bit further to avoid the disclosure triangle.
await clickElement(element, {clickOptions: {button: 'right', offset: {x: 35, y: 0}}});
const button = await waitForAria(menuItem);
await clickElement(button);
}
export async function restoreIgnoredRetainers() {
const element = await waitFor('devtools-button[aria-label="Restore ignored retainers"]');
await clickElement(element);
}
export async function setFilterDropdown(filter: string) {
const select = await waitFor('devtools-toolbar select[aria-label="Filter"]');
await select.select(filter);
}
export async function checkExposeInternals() {
const element = await waitForElementWithTextContent('Internals with implementation details');
await clickElement(element);
}