gpt4 book ai didi

node.js - 即使在简单的 NODE JS 应用程序中,事件句柄也会创建但不会销毁

转载 作者:太空宇宙 更新时间:2023-11-03 22:22:36 24 4
gpt4 key购买 nike

我正在使用 Node 10.10.0 最新版本,下面是我尝试执行的一小段代码。

var fs = require('fs');
const express = require('express');
const request = require('request');
const app = express();


async function fun1(req, resp){
let response = await request.get('http://google.com');
if (response.err) { console.log('error');}
else { resp.send('fetched response'); };
}

app.get('/', (req, res) => {
fun1(req, res);
});

app.listen(3000, () => {
console.log('Example app listening on port 3000!');
});

我还使用诊所医生 check performance bottleneck for above code .

我正在运行如下命令;

clinic doctor --on-port 'autocannon -c100 localhost:3000' -- node index.js

执行上述命令时,我得到以下控制台输出。

Example app listening on port 3000!
Running 10s test @ http://localhost:3000
100 connections

Stat Avg Stdev Max
Latency (ms) 94.72 75.87 1077.87
Req/Sec 827 257.35 1248
Bytes/Sec 180 kB 55.7 kB 270 kB

9k requests in 11s, 1.96 MB read
9 errors (0 timeouts)
Analysing data
Generated HTML file is 3884.clinic-doctor.html
You can use this command to upload it:
clinic upload 3884.clinic-doctor

我还生成了一个不错的用户界面,它会自动打开,如下所示,

enter image description here

问题为什么我的应用程序在测试性能瓶颈时会继续创建事件句柄,如图中的第四个图表所示?我需要关闭任何东西吗?我是不是忘记了什么?

最佳答案

首先,非常感谢我正在寻找 Node.js 压力测试工具的这个示例。

您的示例设置不正确,request 不适用于 Promise。有additional packages可以与 Bluebird 或本地 promise 一起使用。

我使用async/await调整了您的示例:

const express = require('express');
const request = require('request-promise-native');
const app = express();


function fun1(req, resp) {
return request.get('http://google.com');
}

app.get('/', async (req, res) => {
try {
await fun1();
res.send('fetched response');
} catch (error) {
res.send('error')
}
});

app.listen(3000, () => {
console.log('Example app listening on port 3000!');
});

结果:

node-clinic

请记住,您还必须正常关闭任何挂起的服务器实例,如此 StackOverflow question 中所示。 .

关于node.js - 即使在简单的 NODE JS 应用程序中,事件句柄也会创建但不会销毁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52351445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com