Skip to content

Instantly share code, notes, and snippets.

@velizarn
Last active January 1, 2018 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save velizarn/728b8b0fce9e326035bf57fdd366a3c3 to your computer and use it in GitHub Desktop.
Save velizarn/728b8b0fce9e326035bf57fdd366a3c3 to your computer and use it in GitHub Desktop.
Run a load test on a given URL
const http = require('http')
let getData = (rand = 1) => {
return new Promise((resolve, reject) => {
try {
let url = 'http://localhost:3000/db/products.count?v=' + rand
var start = new Date();
http.get(url, (res) => {
let responseTime = new Date() - start;
let body = '';
res.on('data', (chunk) => { body += chunk })
res.on('end', () => {
resolve({ data : body, elapsedTime : responseTime })
})
}).on('error', (e) => { return reject(e + '') })
} catch (e) {
return reject(e + '')
}
})
}
let times = []
setInterval(() => {
let rand = Math.floor(Math.random() * 1000)
getData(rand)
.then((response) => {
times.push(response.elapsedTime)
let sum = times.reduce(function(a, b) { return a + b; })
let avg = Number(sum / times.length)
console.log(response.data + ' | avg reponse time: ' + avg.toLocaleString("en-US", {
style : "decimal",
minimumFractionDigits : 3,
maximumFractionDigits : 3
}))
})
.catch((e) => {
console.error(e + '')
})
}, 30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment