Skip to content

Instantly share code, notes, and snippets.

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 peterbe/f1fc3ae7811b7075e5da3f67531f62e6 to your computer and use it in GitHub Desktop.
Save peterbe/f1fc3ae7811b7075e5da3f67531f62e6 to your computer and use it in GitHub Desktop.
import got from 'got'
async function main() {
const N = 300
const t0 = Date.now()
const responses = await Promise.all(
Array.from(Array(N).keys()).map(async (i) => {
// const r = await got(`https://docs.github.com/en/search?query=${Math.random()}`, {
const r = await got(`https://docs.github.com/healthz-${Math.random()}`, {
// const r = await got(`https://docs.github.com/healthz-`, {
// const r = await got(`http://localhost:4000/healthz?query=${Math.random()}`, {
// const r = await got(`http://localhost:4000/en/search?xxx=${Math.random()}`, {
// const r = await got(`https://docs.github.com/healthz`, {
retry: { limit: 0 },
// headers: {
// food: 'beans',
// },
throwHttpErrors: false,
})
console.log(r.statusCode, r.headers['x-cache'], r.headers['content-type'])
// if (r.statusCode === 429) {
// console.log(r.headers)
// // console.log(r.body)
// }
// console.log(r.body)
// throw new Error('stop')
return r
})
)
const t1 = Date.now()
// const timingsByCode = {}
// for (const r of responses) {
// if (!timingsByCode[r.statusCode]) {
// timingsByCode[r.statusCode] = []
// }
// timingsByCode[r.statusCode].push(r.timings.phases.total)
// }
const codes = responses.map((r) => r.statusCode)
if (codes.length) {
console.log('\n')
const sum = codes.length
console.log('Total requests:', sum)
for (const code of new Set(codes)) {
console.log(
' ',
code,
codes.filter((c) => c === code).length,
`${((100 * codes.filter((c) => c === code).length) / sum).toFixed(1)}%`
// 'median response time:'
// `${median(timingsByCode[code]).toFixed(1)}ms`
)
}
}
console.log('Total time', t1 - t0, 'ms', 'avg', (t1 - t0) / N, 'ms')
}
main()
function median(numbers) {
return numbers.sort((a, b) => a - b)[Math.floor(numbers.length / 2)]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment