1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | Object.defineProperty(exports, "__esModule", { value: true });
|
13 | exports.measurementName = exports.measure = void 0;
|
14 | const util_1 = require("./util");
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | async function measure(driver, measurement, server) {
|
21 | switch (measurement.mode) {
|
22 | case 'callback':
|
23 | if (server === undefined) {
|
24 | throw new Error('Internal error: no server for spec');
|
25 | }
|
26 | return (await server.nextResults()).millis;
|
27 | case 'expression':
|
28 | return queryForExpression(driver, measurement.expression);
|
29 | case 'performance':
|
30 | return queryForPerformanceEntry(driver, measurement);
|
31 | }
|
32 | util_1.throwUnreachable(measurement, `Internal error: unknown measurement type ` +
|
33 | JSON.stringify(measurement));
|
34 | }
|
35 | exports.measure = measure;
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | async function queryForPerformanceEntry(driver, measurement) {
|
43 | const escaped = escapeStringLiteral(measurement.entryName);
|
44 | const script = `return window.performance.getEntriesByName(\`${escaped}\`);`;
|
45 | const entries = await driver.executeScript(script);
|
46 | if (entries.length === 0) {
|
47 | return undefined;
|
48 | }
|
49 | if (entries.length > 1) {
|
50 | console.log('WARNING: Found multiple performance marks/measurements with name ' +
|
51 | `"${measurement.entryName}". This likely indicates an error. ` +
|
52 | 'Picking the first one.');
|
53 | }
|
54 | const entry = entries[0];
|
55 | switch (entry.entryType) {
|
56 | case 'measure':
|
57 | return entry.duration;
|
58 | case 'mark':
|
59 | case 'paint':
|
60 | return entry.startTime;
|
61 | default:
|
62 |
|
63 |
|
64 |
|
65 | throw new Error(`Performance entry type not supported: ${entry.entryType}`);
|
66 | }
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | async function queryForExpression(driver, expression) {
|
74 | const result = await driver.executeScript(`return (${expression});`);
|
75 | if (result !== undefined && result !== null) {
|
76 | if (typeof result !== 'number') {
|
77 | throw new Error(`'${expression}' was type ` +
|
78 | `${typeof result}, expected number.`);
|
79 | }
|
80 | if (result < 0) {
|
81 | throw new Error(`'${expression}' was negative: ${result}`);
|
82 | }
|
83 | return result;
|
84 | }
|
85 | }
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | function escapeStringLiteral(unescaped) {
|
91 | return unescaped.replace(/\\/g, '\\\\')
|
92 | .replace(/`/g, '\\`')
|
93 | .replace(/\$/g, '\\$');
|
94 | }
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | function measurementName(measurement) {
|
100 | if (measurement.name) {
|
101 | return measurement.name;
|
102 | }
|
103 | switch (measurement.mode) {
|
104 | case 'callback':
|
105 | return 'callback';
|
106 | case 'expression':
|
107 | return measurement.expression;
|
108 | case 'performance':
|
109 | return measurement.entryName === 'first-contentful-paint' ?
|
110 | 'fcp' :
|
111 | measurement.entryName;
|
112 | }
|
113 | util_1.throwUnreachable(measurement, `Internal error: unknown measurement type ` +
|
114 | JSON.stringify(measurement));
|
115 | }
|
116 | exports.measurementName = measurementName;
|
117 |
|
\ | No newline at end of file |