-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautomation.js
More file actions
94 lines (71 loc) · 2.72 KB
/
automation.js
File metadata and controls
94 lines (71 loc) · 2.72 KB
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
require('dotenv').config();
const puppeteer = require('puppeteer');
const getSplitClient = require('./server/split.js');
const splitClient = getSplitClient();
// You can optionally pass the loop variables on the command line, e.g. `npm run automation 0 200`
let [i, SAMPLE_SIZE] = process.argv.slice(2,4).map(n => parseInt(n, 10));
// Validate input
if ( !( i <= SAMPLE_SIZE ) ) {
console.log(`Found invalid command line arguments '${i}' and '${SAMPLE_SIZE}'. Using default values instead.`);
i = 0;
SAMPLE_SIZE = 200;
}
// https://fdalvi.github.io/blog/2018-02-05-puppeteer-network-throttle/
const NETWORK_CONDITIONS = {
Regular3G: {
download: 750 * 1024 / 8,
upload: 250 * 1024 / 8,
latency: 100
},
Good3G: {
offline: false,
download: 1.5 * 1024 * 1024 / 8,
upload: 750 * 1024 / 8,
latency: 40
},
Regular4G: {
download: 4 * 1024 * 1024 / 8,
upload: 3 * 1024 * 1024 / 8,
latency: 20
},
WiFi: {
download: 30 * 1024 * 1024 / 8,
upload: 15 * 1024 * 1024 / 8,
latency: 2
}
};
(async () => {
console.log('Navigation script');
// Launch browser, use headful mode to allow Cumulative Layout Shift (CLS) measurment
const browser = await puppeteer.launch({headless: false, defaultViewport: null});
for (; i < SAMPLE_SIZE; i++) {
console.log(`Running ${i} of ${SAMPLE_SIZE}`);
// Open a new tab in a new session
const context = await browser.createIncognitoBrowserContext();
const page = await context.newPage();
// Emulate network conditions and disable cache to simulate new users
await page.setCacheEnabled(false);
// Evaluate Split flag to determine what the emulated network conditions should be for this user
const networkSpeed = splitClient.getTreatment(i, process.env.FEATURE_FLAG_NETWORK_SPEED);
await page.emulateNetworkConditions(
(networkSpeed in NETWORK_CONDITIONS)
? NETWORK_CONDITIONS[networkSpeed]
: NETWORK_CONDITIONS.Regular3G
);
// Navigate to URL
await page.goto(`http://localhost:3000/?id=${i}`);
// Pause to allow time for the image to load
await new Promise(resolve => setTimeout(resolve, 1000));
// Click on an element to start measuring First Input Delay (FID) and Interaction to Next Paint (INP) time
await page.click('#split_logo');
// Pause to allow time for the FID and INP measurement
await new Promise(resolve => setTimeout(resolve, 1000));
// Close the tab so that the CLS and INP measurements are sent
await page.close();
}
// Pause to allow the browser time to send the last CLS measurement
await new Promise(resolve => setTimeout(resolve, 1000));
// Close browser
await browser.close();
console.log('Automation script finished');
})();