| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c6f4a6f commit 4e8c58c
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,27 +5,27 @@ | |||
| 5 | 5 | ||
| 6 | 6 | const common = require('../common'); | |
| 7 | 7 | const onGC = require('../common/ongc'); | |
| 8 | - | ||
| 9 | 8 | const http = require('http'); | |
| 10 | - const todo = 500; | ||
| 9 | + const os = require('os'); | ||
| 10 | + | ||
| 11 | + const cpus = os.cpus().length; | ||
| 12 | + let createClients = true; | ||
| 11 | 13 | let done = 0; | |
| 12 | 14 | let count = 0; | |
| 13 | 15 | let countGC = 0; | |
| 14 | 16 | ||
| 15 | - console.log(`We should do ${todo} requests`); | ||
| 16 | - | ||
| 17 | 17 | function serverHandler(req, res) { | |
| 18 | 18 | res.connection.destroy(); | |
| 19 | 19 | } | |
| 20 | 20 | ||
| 21 | 21 | const server = http.createServer(serverHandler); | |
| 22 | 22 | server.listen(0, common.mustCall(() => { | |
| 23 | - for (let i = 0; i < 10; i++) | ||
| 24 | - getall(); | ||
| 23 | + for (let i = 0; i < cpus; i++) | ||
| 24 | + getAll(); | ||
| 25 | 25 | })); | |
| 26 | 26 | ||
| 27 | - function getall() { | ||
| 28 | - if (count >= todo) | ||
| 27 | + function getAll() { | ||
| 28 | + if (!createClients) | ||
| 29 | 29 | return; | |
| 30 | 30 | ||
| 31 | 31 | const req = http.get({ | |
@@ -37,7 +37,7 @@ function getall() { | |||
| 37 | 37 | count++; | |
| 38 | 38 | onGC(req, { ongc }); | |
| 39 | 39 | ||
| 40 | - setImmediate(getall); | ||
| 40 | + setImmediate(getAll); | ||
| 41 | 41 | } | |
| 42 | 42 | ||
| 43 | 43 | function cb(res) { | |
@@ -48,11 +48,18 @@ function ongc() { | |||
| 48 | 48 | countGC++; | |
| 49 | 49 | } | |
| 50 | 50 | ||
| 51 | - setInterval(status, 100).unref(); | ||
| 51 | + setImmediate(status); | ||
| 52 | 52 | ||
| 53 | 53 | function status() { | |
| 54 | - global.gc(); | ||
| 55 | - console.log('Done: %d/%d', done, todo); | ||
| 56 | - console.log('Collected: %d/%d', countGC, count); | ||
| 57 | - if (countGC === todo) server.close(); | ||
| 54 | + if (done > 0) { | ||
| 55 | + createClients = false; | ||
| 56 | + global.gc(); | ||
| 57 | + console.log(`done/collected/total: ${done}/${countGC}/${count}`); | ||
| 58 | + if (countGC === count) { | ||
| 59 | + server.close(); | ||
| 60 | + return; | ||
| 61 | + } | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + setImmediate(status); | ||
| 58 | 65 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,6 +5,8 @@ | |||
| 5 | 5 | ||
| 6 | 6 | const common = require('../common'); | |
| 7 | 7 | const onGC = require('../common/ongc'); | |
| 8 | + const http = require('http'); | ||
| 9 | + const os = require('os'); | ||
| 8 | 10 | ||
| 9 | 11 | function serverHandler(req, res) { | |
| 10 | 12 | setTimeout(function() { | |
@@ -14,19 +16,17 @@ function serverHandler(req, res) { | |||
| 14 | 16 | }, 100); | |
| 15 | 17 | } | |
| 16 | 18 | ||
| 17 | - const http = require('http'); | ||
| 18 | - const todo = 300; | ||
| 19 | + const cpus = os.cpus().length; | ||
| 20 | + let createClients = true; | ||
| 19 | 21 | let done = 0; | |
| 20 | 22 | let count = 0; | |
| 21 | 23 | let countGC = 0; | |
| 22 | 24 | ||
| 23 | - console.log(`We should do ${todo} requests`); | ||
| 24 | - | ||
| 25 | 25 | const server = http.createServer(serverHandler); | |
| 26 | - server.listen(0, common.mustCall(getall)); | ||
| 26 | + server.listen(0, common.mustCall(getAll)); | ||
| 27 | 27 | ||
| 28 | - function getall() { | ||
| 29 | - if (count >= todo) | ||
| 28 | + function getAll() { | ||
| 29 | + if (!createClients) | ||
| 30 | 30 | return; | |
| 31 | 31 | ||
| 32 | 32 | const req = http.get({ | |
@@ -35,18 +35,16 @@ function getall() { | |||
| 35 | 35 | port: server.address().port | |
| 36 | 36 | }, cb); | |
| 37 | 37 | ||
| 38 | - req.setTimeout(10, function() { | ||
| 39 | - console.log('timeout (expected)'); | ||
| 40 | - }); | ||
| 38 | + req.setTimeout(10, common.mustCall()); | ||
| 41 | 39 | ||
| 42 | 40 | count++; | |
| 43 | 41 | onGC(req, { ongc }); | |
| 44 | 42 | ||
| 45 | - setImmediate(getall); | ||
| 43 | + setImmediate(getAll); | ||
| 46 | 44 | } | |
| 47 | 45 | ||
| 48 | - for (let i = 0; i < 10; i++) | ||
| 49 | - getall(); | ||
| 46 | + for (let i = 0; i < cpus; i++) | ||
| 47 | + getAll(); | ||
| 50 | 48 | ||
| 51 | 49 | function cb(res) { | |
| 52 | 50 | res.resume(); | |
@@ -57,11 +55,18 @@ function ongc() { | |||
| 57 | 55 | countGC++; | |
| 58 | 56 | } | |
| 59 | 57 | ||
| 60 | - setInterval(status, 100).unref(); | ||
| 58 | + setImmediate(status); | ||
| 61 | 59 | ||
| 62 | 60 | function status() { | |
| 63 | - global.gc(); | ||
| 64 | - console.log('Done: %d/%d', done, todo); | ||
| 65 | - console.log('Collected: %d/%d', countGC, count); | ||
| 66 | - if (countGC === todo) server.close(); | ||
| 61 | + if (done > 0) { | ||
| 62 | + createClients = false; | ||
| 63 | + global.gc(); | ||
| 64 | + console.log(`done/collected/total: ${done}/${countGC}/${count}`); | ||
| 65 | + if (countGC === count) { | ||
| 66 | + server.close(); | ||
| 67 | + return; | ||
| 68 | + } | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + setImmediate(status); | ||
| 67 | 72 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,6 +5,9 @@ | |||
| 5 | 5 | ||
| 6 | 6 | require('../common'); | |
| 7 | 7 | const onGC = require('../common/ongc'); | |
| 8 | + const assert = require('assert'); | ||
| 9 | + const net = require('net'); | ||
| 10 | + const os = require('os'); | ||
| 8 | 11 | ||
| 9 | 12 | function serverHandler(sock) { | |
| 10 | 13 | sock.setTimeout(120000); | |
@@ -23,20 +26,17 @@ function serverHandler(sock) { | |||
| 23 | 26 | }, 100); | |
| 24 | 27 | } | |
| 25 | 28 | ||
| 26 | - const net = require('net'); | ||
| 27 | - const assert = require('assert'); | ||
| 28 | - const todo = 500; | ||
| 29 | + const cpus = os.cpus().length; | ||
| 30 | + let createClients = true; | ||
| 29 | 31 | let done = 0; | |
| 30 | 32 | let count = 0; | |
| 31 | 33 | let countGC = 0; | |
| 32 | 34 | ||
| 33 | - console.log(`We should do ${todo} requests`); | ||
| 34 | - | ||
| 35 | 35 | const server = net.createServer(serverHandler); | |
| 36 | - server.listen(0, getall); | ||
| 36 | + server.listen(0, getAll); | ||
| 37 | 37 | ||
| 38 | - function getall() { | ||
| 39 | - if (count >= todo) | ||
| 38 | + function getAll() { | ||
| 39 | + if (!createClients) | ||
| 40 | 40 | return; | |
| 41 | 41 | ||
| 42 | 42 | const req = net.connect(server.address().port); | |
@@ -49,21 +49,28 @@ function getall() { | |||
| 49 | 49 | count++; | |
| 50 | 50 | onGC(req, { ongc }); | |
| 51 | 51 | ||
| 52 | - setImmediate(getall); | ||
| 52 | + setImmediate(getAll); | ||
| 53 | 53 | } | |
| 54 | 54 | ||
| 55 | - for (let i = 0; i < 10; i++) | ||
| 56 | - getall(); | ||
| 55 | + for (let i = 0; i < cpus; i++) | ||
| 56 | + getAll(); | ||
| 57 | 57 | ||
| 58 | 58 | function ongc() { | |
| 59 | 59 | countGC++; | |
| 60 | 60 | } | |
| 61 | 61 | ||
| 62 | - setInterval(status, 100).unref(); | ||
| 62 | + setImmediate(status); | ||
| 63 | 63 | ||
| 64 | 64 | function status() { | |
| 65 | - global.gc(); | ||
| 66 | - console.log('Done: %d/%d', done, todo); | ||
| 67 | - console.log('Collected: %d/%d', countGC, count); | ||
| 68 | - if (countGC === todo) server.close(); | ||
| 65 | + if (done > 0) { | ||
| 66 | + createClients = false; | ||
| 67 | + global.gc(); | ||
| 68 | + console.log(`done/collected/total: ${done}/${countGC}/${count}`); | ||
| 69 | + if (countGC === count) { | ||
| 70 | + server.close(); | ||
| 71 | + return; | ||
| 72 | + } | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + setImmediate(status); | ||
| 69 | 76 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments