Created
January 11, 2019 19:52
-
-
Save timdp/6df94720b66815d501fac08b5b33ad03 to your computer and use it in GitHub Desktop.
This file contains hidden or 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') | |
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