Skip to content

Instantly share code, notes, and snippets.

@lahaxearnaud
Created November 6, 2018 06:36
Show Gist options
  • Save lahaxearnaud/92cd407f5b1be865ba529f80b0a1bb9a to your computer and use it in GitHub Desktop.
Save lahaxearnaud/92cd407f5b1be865ba529f80b0a1bb9a to your computer and use it in GitHub Desktop.
const WebPageTest = require('webpagetest');
const http = require('http');
const SDC = require('statsd-client'),
sdc = new SDC({
prefix: '<NOM_DU_SITE_OU_DE_LA_PAGE>.webpagetest',
host: '<VOTRE_HOST_GRAPHITE>',
port: 8125
});
const wpt = new WebPageTest('www.webpagetest.org', '<VOTRE_API_KEY>');
wpt.runTest('<VOTRE_URL>', {location: 'ec2-eu-west-1:Chrome'}, (err, data) => {
if (err) {
console.error(err);
return;
}
let jsonUrl = data.data.jsonUrl;
let iterationNumber = 0;
let stop = setInterval(function () {
// 20 mins max
if (iterationNumber > 600) {
clearInterval(stop);
return;
}
iterationNumber ++;
http.get(jsonUrl, (resp) => {
let rawData = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
rawData += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
let response = JSON.parse(rawData);
if (response.statusCode === 200) {
clearInterval(stop);
['firstView', 'repeatView'].forEach((key) => {
sdc.gauge(key + '.first_paint', response.data.median[key].firstPaint);
sdc.gauge(key + '.fully_loaded', response.data.median[key].fullyLoaded);
sdc.gauge(key + '.bytes_out', response.data.median[key].bytesOut);
sdc.gauge(key + '.image_total', response.data.median[key].image_total);
sdc.gauge(key + '.load_time', response.data.median[key].loadTime);
sdc.gauge(key + '.speed_index', response.data.median[key].SpeedIndex);
sdc.gauge(key + '.cached', response.data.median[key].cached);
sdc.gauge(key + '.dom_interactive', response.data.median[key].domInteractive);
sdc.gauge(key + '.nb_domains', Object.keys(response.data.median[key].domains).length);
sdc.gauge(key + '.resources.html.size', response.data.median[key].breakdown.html.bytes);
sdc.gauge(key + '.resources.html.nb', response.data.median[key].breakdown.html.requests);
sdc.gauge(key + '.resources.css.size', response.data.median[key].breakdown.css.bytes);
sdc.gauge(key + '.resources.css.nb', response.data.median[key].breakdown.css.requests);
sdc.gauge(key + '.resources.js.size', response.data.median[key].breakdown.js.bytes);
sdc.gauge(key + '.resources.js.nb', response.data.median[key].breakdown.js.requests);
sdc.gauge(key + '.resources.image.size', response.data.median[key].breakdown.image.bytes);
sdc.gauge(key + '.resources.image.nb', response.data.median[key].breakdown.image.requests);
});
}
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
}, 2000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment