Skip to content

Instantly share code, notes, and snippets.

@dave-malone
Created August 21, 2018 15:15
Show Gist options
  • Save dave-malone/e035684cd93d2fe550db0a61d4228132 to your computer and use it in GitHub Desktop.
Save dave-malone/e035684cd93d2fe550db0a61d4228132 to your computer and use it in GitHub Desktop.
A simple node program that uses the fast-speedtest-api npm package to test internet bandwidth and publishes it to CloudWatch
const FastSpeedtest = require("fast-speedtest-api")
const AWS = require('aws-sdk')
AWS.config.update({region: 'us-east-1'})
const cloudwatch = new AWS.CloudWatch()
let speedtest = new FastSpeedtest({
token: "INSERT-YOUR-TOKEN-HERE",
verbose: false,
timeout: 10000,
https: true,
urlCount: 5,
bufferSize: 8,
unit: FastSpeedtest.UNITS.Mbps
})
speedtest.getSpeed().then((s) => {
console.log(`Speed: ${s} Mbps`)
let cloudWatchPayload = {
MetricData: [
{
MetricName: 'bandwidth_mbps',
StorageResolution: 1,
Timestamp: new Date(),
Unit: "Megabytes/Second",
Value: s
},
],
Namespace: 'FastSpeedtest'
}
cloudwatch.putMetricData(cloudWatchPayload, (err, response) => {
if(err){
console.log(`Failed to publish speed test metrics to CloudWatch: ${err}`, err.stack)
}else{
console.log(`CloudWatch publish response: ${JSON.stringify(response)}`)
}
})
}).catch(e => {
console.error(`An error occurred when attempting to run the speed test: ${e.message}`)
})
@dave-malone
Copy link
Author

Uses fast.com via fast-speedtest-api

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment