forked from mui/mui-x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperformance.js
169 lines (137 loc) · 4.79 KB
/
performance.js
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
/* eslint-disable no-await-in-loop */
const yargs = require('yargs');
const playwright = require('playwright');
const capitalize = require('lodash/capitalize');
const path = require('path');
const fse = require('fs-extra');
const PORT = 5001;
const workspaceRoot = path.join(__dirname, '../');
const snapshotDestPath = path.join(workspaceRoot, 'performance-snapshot.json');
let browser;
function getMedian(values) {
const length = values.length;
values.sort();
if (length % 2 === 0) {
return (values[length / 2] + values[length / 2 - 1]) / 2;
}
return values[parseInt(length / 2, 10)];
}
function getMean(values) {
const sum = values.reduce((acc, value) => acc + value, 0);
return sum / values.length;
}
function getStdDev(values, mean) {
const squareDiffs = values.map((value) => {
const diff = value - mean;
return diff * diff;
});
return Math.sqrt(getMean(squareDiffs));
}
function getMeasures(samples) {
const min = Math.min(...samples);
const max = Math.max(...samples);
const median = getMedian(samples);
const mean = getMean(samples);
const stdDev = getStdDev(samples, mean);
return { min, max, median, mean, stdDev, samples };
}
function createLabelNameEngine() {
const getControlOfLabel = (label, root) => {
const htmlFor = label.getAttribute('for');
return root.getElementById(htmlFor);
};
return {
query(root, selector) {
const label = Array.from(root.querySelectorAll('label')).find(
(el) => el.innerText.trim() === selector,
);
return getControlOfLabel(label, root);
},
queryAll(root, selector) {
const labels = Array.from(root.querySelectorAll('label')).filter(
(el) => el.innerText.trim() === selector,
);
return labels.map((label) => getControlOfLabel(label, root));
},
};
}
async function testFilter100kRows(page) {
const baseUrl = `http://localhost:${PORT}/performance/DataGrid/FilterRows100000`;
await page.goto(baseUrl);
await page.selectOption('label=Columns', 'currencyPair');
await page.selectOption('label=Operator', 'startsWith');
const t0 = await page.evaluate(() => performance.now());
await page.type('label=Value', 'usd');
// Ensure that the filter icon is not in the DOM.
await page.waitForSelector('.MuiDataGrid-filterIcon', { state: 'detached' });
// We use "attached", instead of the default "visible", because the icon is in the DOM but hidden.
await page.waitForSelector('.MuiDataGrid-filterIcon', { state: 'attached' });
const t1 = await page.evaluate(() => performance.now());
return t1 - t0 - 500; // Subtract the debounce time
}
async function testSort100kRows(page) {
const baseUrl = `http://localhost:${PORT}/performance/DataGrid/SortRows100000`;
await page.goto(baseUrl);
const t0 = await page.evaluate(() => performance.now());
await page.click('text="Currency Pair"');
const t1 = await page.evaluate(() => performance.now());
return t1 - t0;
}
async function testSelect100kRows(page) {
const baseUrl = `http://localhost:${PORT}/performance/DataGrid/SelectRows100000`;
await page.goto(baseUrl);
const t0 = await page.evaluate(() => performance.now());
await page.click('[aria-label="Select all rows"]');
const t1 = await page.evaluate(() => performance.now());
return t1 - t0;
}
async function testDeselect100kRows(page) {
const baseUrl = `http://localhost:${PORT}/performance/DataGrid/SelectRows100000`;
await page.goto(baseUrl);
await page.click('[aria-label="Select all rows"]');
const t0 = await page.evaluate(() => performance.now());
await page.click('[aria-label="Unselect all rows"]');
const t1 = await page.evaluate(() => performance.now());
return t1 - t0;
}
async function run() {
await playwright.selectors.register('label', createLabelNameEngine);
browser = await playwright.chromium.launch({
args: ['--font-render-hinting=none'],
headless: false,
});
const cases = [testFilter100kRows, testSort100kRows, testSelect100kRows, testDeselect100kRows];
const results = cases.map(async (testCase) => {
const samples = [];
for (let j = 0; j < 5; j += 1) {
const page = await browser.newPage();
const time = await testCase(page);
await page.close();
samples.push(time);
}
const name = capitalize(
testCase.name
.replace(/([A-Z]|[0-9]+)/g, ' $1')
.replace(/^test/, '')
.trim(),
);
return [name, getMeasures(samples)];
});
const allResults = await Promise.all(results);
const snapshot = allResults.reduce((acc, [name, values]) => {
acc[name] = values;
return acc;
}, {});
await browser.close();
await fse.writeJSON(snapshotDestPath, snapshot, { spaces: 2 });
}
yargs
.command({
command: '$0',
description: 'Runs the performance tests.',
handler: run,
})
.help()
.strict(true)
.version(false)
.parse();