私は、安らかなエンドポイントに複数 (5000) の連続したリクエストを送信する必要があるモジュールを備えた電子アプリを持っています。ブラウザー ウィンドウからこのエンドポイントへの連続する要求をテストすると、80 ミリ秒の応答時間が得られますが、今度はそれを Electron 側 (Node.js) に実装する必要があります。以下のコードで 100 要求のテストを実行すると、応答は時間は 500 ミリ秒以上であり、送信するほど長くなります。
これを Axios でもテストしましたが、応答時間は同じです (> 500ms)。
testHttp() {
const buffer = Buffer.alloc(5 * 1024, 1);
const agent = new https.Agent({
keepAlive: true
});
agent.maxSockets = 1000000;
const post_options = {
hostname: this.baseUrl.substr(8),
path: '/status/welcome',
method: 'POST',
port: 443,
agent: agent,
headers: {}
};
for (let i = 0; i < 100; i++) {
// Set up the request
let prepTime = 0;
const post_req = https.request(post_options, function (res) {
let rawResponse = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log(post_req.reusedSocket);
rawResponse += chunk;
});
res.on('end', () => {
try {
const parsedData = JSON.parse(rawResponse);
console.log('Time Elapsed for Status Welcome : ' + (new Date().getTime() - prepTime) + ' ms');
} catch (e) {
console.error(e.message);
}
});
});
// post the data
prepTime = new Date().getTime();
post_req.write(buffer);
post_req.end();
}
}