/
app.js
141 lines (122 loc) · 4.19 KB
/
app.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
const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');
const Influx = require('influx');
const env = require('./' + process.env.LIGHTHOUSE_SETTINGS);
const promiseSerial = (funcs, data, cb) =>
funcs.reduce((promise, func, i, arr) =>
promise.then(results => func(results[i - 1], data).then(result => { cb && cb((i + 1) / arr.length); return results.concat(result); })), Promise.resolve([]));
const schemaItems = [
{ measurement: 'first-contentful-paint', score: Influx.FieldType.INTEGER },
{ measurement: 'first-cpu-idle', score: Influx.FieldType.INTEGER },
{ measurement: 'first-meaningful-paint', score: Influx.FieldType.INTEGER },
{ measurement: 'speed-index', score: Influx.FieldType.INTEGER },
{ measurement: 'estimated-input-latency', score: Influx.FieldType.INTEGER },
{ measurement: 'time-to-first-byte', score: Influx.FieldType.INTEGER },
{ measurement: 'interactive', score: Influx.FieldType.INTEGER },
{ measurement: 'mainthread-work-breakdown', score: Influx.FieldType.INTEGER },
{ measurement: 'bootup-time', score: Influx.FieldType.INTEGER },
{ measurement: 'total-byte-weight', score: Influx.FieldType.INTEGER },
{ measurement: 'uses-responsive-images', score: Influx.FieldType.INTEGER },
{ measurement: 'dom-size', score: Influx.FieldType.INTEGER },
{ measurement: 'render-blocking-resources', score: Influx.FieldType.INTEGER }
];
const schema = schemaItems.map(schemaItem => {
return {
measurement: schemaItem.measurement,
fields: {
score: schemaItem.score,
value: Influx.FieldType.FLOAT
},
tags: [
'environment',
'uri',
'name'
]
};
});
schema.push({
measurement: 'score',
fields: {
score: Influx.FieldType.INTEGER,
value: Influx.FieldType.FLOAT
},
tags: [
'environment',
'uri',
'name'
]
});
const influx = new Influx.InfluxDB({
host: 'localhost',
database: 'lighthouse',
schema: schema
});
function launchChromeAndRunLighthouse(url, flags = {}, config = null) {
return chromeLauncher.launch(flags).then(chrome => {
flags.port = chrome.port;
return lighthouse(url, flags, config).then(results =>
chrome.kill().then(() => results.lhr));
});
}
const flags = {
chromeFlags: ['--headless', '--disable-gpu', '--no-sandbox', '--disable-software-rasterizer', '--disable-dev-shm-usage'],
onlyCategories: ['performance']
};
const audits = schemaItems.map(schemaItem => schemaItem.measurement);
function createTest(url, measurements) {
return (result, data) => {
return new Promise((resolve, reject) => {
console.log(`Starting test: ${url.name}`);
launchChromeAndRunLighthouse(url.url, flags).then(results => {
for(let audit of audits) {
const score = results.audits[audit].score;
const value = results.audits[audit].rawValue;
console.log('audit: ' + audit + ' value: '+value);
measurements.push({
measurement: audit,
tags: {
environment: env.environment,
uri: url.url,
name: url.name
},
fields: {
score: score,
value: value
}
});
}
resolve();
});
});
}
}
function progressCallback(progress) {
console.log(`Total progress: ${progress * 100}%`);
}
function doTests() {
const measurements = [];
const tests = env.urls.map(url => createTest(url, measurements));
promiseSerial(tests, {}, progressCallback)
.then((results) => {
console.log('Writing results');
influx.getDatabaseNames()
.then(names => {
if (!names.includes('lighthouse')) {
influx.createDatabase('lighthouse');
return influx.createRetentionPolicy('lighthouse', {
duration: '30d',
database: 'lighthouse',
replication: 1,
isDefault: true
})
}
})
.then(() => {
influx.writePoints(measurements)
.then(() => {
console.log('Tests are done');
});
});
});
}
doTests();