Skip to content

Instantly share code, notes, and snippets.

@timdp
Created January 11, 2019 19:52
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 timdp/6df94720b66815d501fac08b5b33ad03 to your computer and use it in GitHub Desktop.
Save timdp/6df94720b66815d501fac08b5b33ad03 to your computer and use it in GitHub Desktop.
const http = require('http')
const PORT = 8888
const REQUEST_LATENCY = 100
const REQUEST_COUNT = 1000
const requestOptions = {
hostname: '127.0.0.1',
port: PORT,
path: '/',
agent: new http.Agent({ keepAlive: true })
}
const reused = new Set()
const id = Symbol('id')
let idCount = 0
const makeRequests = cb => {
let completedCount = 0
for (let i = 0; i < REQUEST_COUNT; ++i) {
http.request(requestOptions, res => {
if (res.socket[id]) {
reused.add(res.socket[id])
} else {
res.socket[id] = ++idCount
}
res.once('end', () => {
// This supposedly causes a memory leak?
res.setTimeout(0)
if (++completedCount === REQUEST_COUNT) {
cb()
}
}).resume()
}).end()
}
}
const server = http.createServer((_req, res) => {
setTimeout(() => {
res.end()
}, REQUEST_LATENCY)
})
server.listen(PORT, () => {
console.log('listening')
makeRequests(() => {
console.log('batch 1 complete')
makeRequests(() => {
console.log('batch 2 complete')
console.log('reused sockets:', reused.size)
server.close()
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment