Node.js 电子网络请求太慢
我有一个electron应用程序,其中有一个模块,我需要向restful端点发送多个(5000)连续请求。从浏览器窗口测试对该端点的连续请求会产生80毫秒的响应时间,但现在我需要在Electron side(Node.js)中实现它,当我使用下面的代码运行一个包含100个请求的测试时,响应时间不少于500毫秒,并且随着我发送更多请求而增加。Node.js 电子网络请求太慢,node.js,typescript,electron,node-https,Node.js,Typescript,Electron,Node Https,我有一个electron应用程序,其中有一个模块,我需要向restful端点发送多个(5000)连续请求。从浏览器窗口测试对该端点的连续请求会产生80毫秒的响应时间,但现在我需要在Electron side(Node.js)中实现它,当我使用下面的代码运行一个包含100个请求的测试时,响应时间不少于500毫秒,并且随着我发送更多请求而增加。我也使用Axios进行了测试,响应时间相同(>500ms) testHttp(){ constbuffer=buffer.alloc(5*1024,1); c
我也使用Axios进行了测试,响应时间相同(>500ms)
testHttp(){
constbuffer=buffer.alloc(5*1024,1);
const agent=new https.agent({
keepAlive:是的
});
agent.maxSockets=1000000;
const post_选项={
主机名:this.baseUrl.substr(8),
路径:'/status/welcome',
方法:“POST”,
港口:443,
代理:代理,
标题:{}
};
for(设i=0;i<100;i++){
//设置请求
设预处理时间=0;
const post_req=https.request(post_选项、函数(res)){
让rawResponse='';
res.setEncoding('utf8');
res.on('data',函数(块){
控制台日志(post_请求重用套接字);
rawResponse+=chunk;
});
res.on('结束',()=>{
试一试{
const parsedData=JSON.parse(rawResponse);
log('Status Welcome:'+(new Date().getTime()-prepTime)+'ms');
}捕获(e){
控制台错误(e.message);
}
});
});
//发布数据
prepTime=新日期().getTime();
post_请求写入(缓冲区);
post_请求结束();
}
}
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();
}