| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4a858b2 commit 3e14212
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,45 +6,44 @@ | |||
| 6 | 6 | const common = require('../common'); | |
| 7 | 7 | const onGC = require('../common/ongc'); | |
| 8 | 8 | ||
| 9 | + const cpus = require('os').cpus().length; | ||
| 10 | + | ||
| 9 | 11 | function serverHandler(req, res) { | |
| 10 | 12 | req.resume(); | |
| 11 | 13 | res.writeHead(200, { 'Content-Type': 'text/plain' }); | |
| 12 | 14 | res.end('Hello World\n'); | |
| 13 | 15 | } | |
| 14 | 16 | ||
| 15 | 17 | const http = require('http'); | |
| 16 | - const todo = 500; | ||
| 18 | + let createClients = true; | ||
| 17 | 19 | let done = 0; | |
| 18 | 20 | let count = 0; | |
| 19 | 21 | let countGC = 0; | |
| 20 | 22 | ||
| 21 | - console.log(`We should do ${todo} requests`); | ||
| 22 | - | ||
| 23 | 23 | const server = http.createServer(serverHandler); | |
| 24 | 24 | server.listen(0, common.mustCall(() => { | |
| 25 | - for (let i = 0; i < 10; i++) | ||
| 26 | - getall(); | ||
| 25 | + for (let i = 0; i < cpus; i++) | ||
| 26 | + getAll(); | ||
| 27 | 27 | })); | |
| 28 | 28 | ||
| 29 | - function getall() { | ||
| 30 | - if (count >= todo) | ||
| 31 | - return; | ||
| 32 | - | ||
| 33 | - const req = http.get({ | ||
| 34 | - hostname: 'localhost', | ||
| 35 | - pathname: '/', | ||
| 36 | - port: server.address().port | ||
| 37 | - }, cb).on('error', onerror); | ||
| 29 | + function getAll() { | ||
| 30 | + if (createClients) { | ||
| 31 | + const req = http.get({ | ||
| 32 | + hostname: 'localhost', | ||
| 33 | + pathname: '/', | ||
| 34 | + port: server.address().port | ||
| 35 | + }, cb).on('error', onerror); | ||
| 38 | 36 | ||
| 39 | - count++; | ||
| 40 | - onGC(req, { ongc }); | ||
| 37 | + count++; | ||
| 38 | + onGC(req, { ongc }); | ||
| 41 | 39 | ||
| 42 | - setImmediate(getall); | ||
| 40 | + setImmediate(getAll); | ||
| 41 | + } | ||
| 43 | 42 | } | |
| 44 | 43 | ||
| 45 | 44 | function cb(res) { | |
| 46 | 45 | res.resume(); | |
| 47 | - done += 1; | ||
| 46 | + done++; | ||
| 48 | 47 | } | |
| 49 | 48 | ||
| 50 | 49 | function onerror(err) { | |
@@ -55,11 +54,19 @@ function ongc() { | |||
| 55 | 54 | countGC++; | |
| 56 | 55 | } | |
| 57 | 56 | ||
| 58 | - setInterval(status, 100).unref(); | ||
| 57 | + setImmediate(status); | ||
| 59 | 58 | ||
| 60 | 59 | function status() { | |
| 61 | - global.gc(); | ||
| 62 | - console.log('Done: %d/%d', done, todo); | ||
| 63 | - console.log('Collected: %d/%d', countGC, count); | ||
| 64 | - if (countGC === todo) server.close(); | ||
| 60 | + if (done > 0) { | ||
| 61 | + createClients = false; | ||
| 62 | + global.gc(); | ||
| 63 | + console.log(`done/collected/total: ${done}/${countGC}/${count}`); | ||
| 64 | + if (countGC === count) { | ||
| 65 | + server.close(); | ||
| 66 | + } else { | ||
| 67 | + setImmediate(status); | ||
| 68 | + } | ||
| 69 | + } else { | ||
| 70 | + setImmediate(status); | ||
| 71 | + } | ||
| 65 | 72 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments