2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2018-07-27 13:36:35 +02:00
|
|
|
// Flags: --expose-gc
|
2012-05-03 10:16:25 -07:00
|
|
|
// just a simple http server and client.
|
|
|
|
|
2017-02-08 15:35:58 +01:00
|
|
|
const common = require('../common');
|
2024-08-13 18:28:21 +08:00
|
|
|
const { onGC } = require('../common/gc');
|
2016-07-19 02:03:42 +05:30
|
|
|
|
2022-12-29 10:38:04 +09:00
|
|
|
const cpus = require('os').availableParallelism();
|
2022-01-20 20:18:20 +01:00
|
|
|
|
2012-05-03 10:16:25 -07:00
|
|
|
function serverHandler(req, res) {
|
2017-07-10 20:55:21 -04:00
|
|
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
2012-05-03 10:16:25 -07:00
|
|
|
res.end('Hello World\n');
|
|
|
|
}
|
|
|
|
|
2016-01-13 21:42:45 +01:00
|
|
|
const http = require('http');
|
2022-08-07 20:44:25 +08:00
|
|
|
const numRequests = 36;
|
2022-01-20 20:18:20 +01:00
|
|
|
let createClients = true;
|
2016-01-13 21:42:45 +01:00
|
|
|
let done = 0;
|
|
|
|
let count = 0;
|
|
|
|
let countGC = 0;
|
2012-05-03 10:16:25 -07:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const server = http.createServer(serverHandler);
|
2018-08-17 14:25:03 +02:00
|
|
|
server.listen(0, common.mustCall(() => {
|
2022-01-20 20:18:20 +01:00
|
|
|
for (let i = 0; i < cpus; i++)
|
2022-08-07 20:44:25 +08:00
|
|
|
getAll(numRequests);
|
2018-08-17 14:25:03 +02:00
|
|
|
}));
|
2012-05-03 10:16:25 -07:00
|
|
|
|
2022-08-07 20:44:25 +08:00
|
|
|
function getAll(requestsRemaining) {
|
2022-01-20 20:18:20 +01:00
|
|
|
if (!createClients)
|
2014-02-24 10:20:30 -08:00
|
|
|
return;
|
2012-05-03 10:16:25 -07:00
|
|
|
|
2022-08-07 20:44:25 +08:00
|
|
|
if (requestsRemaining <= 0)
|
|
|
|
return;
|
|
|
|
|
2018-08-17 14:25:03 +02:00
|
|
|
const req = http.get({
|
|
|
|
hostname: 'localhost',
|
|
|
|
pathname: '/',
|
|
|
|
port: server.address().port
|
|
|
|
}, cb);
|
2014-02-24 10:20:30 -08:00
|
|
|
|
2018-08-17 14:25:03 +02:00
|
|
|
count++;
|
2018-08-21 13:38:59 -07:00
|
|
|
onGC(req, { ongc });
|
2014-02-24 10:20:30 -08:00
|
|
|
|
2022-08-07 20:44:25 +08:00
|
|
|
setImmediate(getAll, requestsRemaining - 1);
|
2012-05-03 10:16:25 -07:00
|
|
|
}
|
|
|
|
|
2018-08-17 14:25:03 +02:00
|
|
|
function cb(res) {
|
|
|
|
res.resume();
|
|
|
|
done += 1;
|
|
|
|
}
|
2014-02-24 10:20:30 -08:00
|
|
|
|
2018-07-27 13:36:35 +02:00
|
|
|
function ongc() {
|
2016-02-03 12:27:40 -08:00
|
|
|
countGC++;
|
2012-05-03 10:16:25 -07:00
|
|
|
}
|
|
|
|
|
2022-01-20 20:18:20 +01:00
|
|
|
setImmediate(status);
|
2012-05-03 10:16:25 -07:00
|
|
|
|
|
|
|
function status() {
|
2022-01-20 20:18:20 +01:00
|
|
|
if (done > 0) {
|
|
|
|
createClients = false;
|
|
|
|
global.gc();
|
|
|
|
console.log(`done/collected/total: ${done}/${countGC}/${count}`);
|
|
|
|
if (countGC === count) {
|
|
|
|
server.close();
|
|
|
|
} else {
|
|
|
|
setImmediate(status);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
setImmediate(status);
|
|
|
|
}
|
2012-05-03 10:16:25 -07:00
|
|
|
}
|