Last active
January 1, 2018 11:18
-
-
Save velizarn/728b8b0fce9e326035bf57fdd366a3c3 to your computer and use it in GitHub Desktop.
Run a load test on a given URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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