forked from kjelloe/webpagetest
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun-speedtest.js
118 lines (99 loc) · 4.95 KB
/
run-speedtest.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
#!/usr/bin/env node
var fs = require('fs')
if (!fs.existsSync(__dirname + '/config.json')) throw new Error('Configuration file config.json is missing!')
if (!fs.existsSync(__dirname + '/data.json')) throw new Error('Configuration file data.json is missing!')
var WebPageTest = require('webpagetest') // Documentation on: https://www.npmjs.com/package/webpagetest
var influx = require('influx')
var config = require(__dirname + '/config.json')
var data = require(__dirname + '/data.json')
var testurl = process.env.TESTURL || null
var testlabel = process.env.LABEL || null
var buildnumber = parseInt(process.env.BUILDNUMBER, 10) || 0
config.wpt.apikey = process.env.APIKEY || config.wpt.apikey
config.wpt.location = process.env.LOCATION || config.wpt.location
config.wpt.timeout = parseInt(process.env.TIMEOUT, 10) || config.wpt.timeout
config.wpt.runs = parseInt(process.env.RUNS || config.wpt.runs, 10)
config.wpt.testserver = process.env.TESTSERVER || config.wpt.testserver
config.wpt.connectivity = process.env.CONNECTIVITY || config.wpt.connectivity
config.wpt.video = process.env.VIDEO || config.wpt.video
config.consoletimer = parseInt(process.env.consoletimer, 10) || config.consoletimer
// Parameter checks
if (testurl == null) throw new Error('Missing ENV parameter TESTURL')
var influxClient = influx(config.influx)
influxClient.getDatabaseNames(function (err, arrayDatabaseNames) {
if (err) throw err
console.log('Found influx database: ' + arrayDatabaseNames)
})
var influxTags = {
'testurl': escapeInfluxDbWrite(testurl),
'location': escapeInfluxDbWrite(config.wpt.location),
'runs': config.wpt.runs,
'connectivity': config.wpt.connectivity
}
if (buildnumber) influxTags.buildnumber = buildnumber
if (testlabel) influxTags.label = testlabel
var timerId = startConsoleTimer(config.consoletimer)
var wpt = new WebPageTest(config.wpt.testserver, config.wpt.apikey)
console.log('Running pagespeedtest on url "' + testurl + '" using server "' + config.wpt.testserver + '" with location "' + config.wpt.location + '". Requiring results within: ' + config.wpt.timeout + ' seconds')
var runStart = new Date().getTime()
var wptOptions = {
'location': config.wpt.location,
'pollResults': 10,
'timeout': config.wpt.timeout,
'runs': config.wpt.runs,
'pageSpeed': true,
'connectivity': config.wpt.connectivity,
'video' : config.wpt.video
}
wpt.runTest(testurl, wptOptions, function (err, response) {
var testDurationMs = (new Date().getTime() - runStart)
console.log('Test run time: ' + (testDurationMs > 1 ? (testDurationMs / 1000) + ' seconds' : 'NA'))
if (err) throw new Error("Could not run webpagetest for url: '" + testurl + "', ERROR: " + JSON.stringify(err))
var testId = response.data.id
wpt.getTestStatus(testId, function (err, response) {
if (err || response.data.statusText !== 'Test Complete') throw new Error('WebPageTest was not completed. Status: ' + (err || response.data.statusText))
wpt.getTestResults(testId, function (err, testData) {
if (err) throw new Error('Could not get test results for testId: ' + testId)
console.log('Test highlights:')
console.log('- Url: ' + testData.data.url)
console.log('- Summary: ' + testData.data.summary)
console.log('- Location: ' + testData.data.location)
console.log('- Connectivity: ' + testData.data.connectivity)
console.log('Test timings:')
console.log('SpeedIndex', 'firstPaint', 'visualComplete', 'waterfall')
var firstView = testData.data.median.firstView
console.log(firstView.SpeedIndex, firstView.firstPaint, firstView.visualComplete, firstView.images.waterfall)
var repeatView = testData.data.median.repeatView
console.log(repeatView.SpeedIndex, repeatView.firstPaint, repeatView.visualComplete, repeatView.images.waterfall)
var influxValues = {}
data.forEach(function (elem) {
influxValues['firstView.' + elem] = firstView[elem] || 0
})
data.forEach(function (elem) {
influxValues['repeatView.' + elem] = repeatView[elem] || 0
})
// Post speed indexes to results db, tag with build number and url for reference
influxClient.writePoint('webpagetest', influxValues, influxTags, function (done) {
console.log('Influx db response: ')
console.log((done == null ? 'OK' : done))
stopConsoleTimer()
})
})
})
})
// Need to escape space and equals in https://influxdb.com/docs/v0.9/write_protocols/write_syntax.html
function escapeInfluxDbWrite (str) {
return str.replace(/[ =,]/g, '\\$&')
}
// Basic console time to give user a sense of feedback by waiting for test
function startConsoleTimer (waitIntervalSeconds) {
if (waitIntervalSeconds === undefined || waitIntervalSeconds === 0) return
var timerSeconds = 0
return setInterval(function () {
timerSeconds += waitIntervalSeconds
console.log(timerSeconds + ' seconds passed')
}, (waitIntervalSeconds * 1000))
}
function stopConsoleTimer () {
clearInterval(timerId) // Stopping timer after ending test
}