繁体   English   中英

如何在nodejs中将cpu使用率提高到100%

[英]how to make cpu usage to 100% in nodejs

我有一个nodejs服务器。我想测试该服务器中的节点繁忙模块。 我写了一个客户端代码,同时击中该服务器,以使CPU使用率达到100%,但仍然无法使该模块正常工作。 PFB服务器代码

var toobusy = require('toobusy'),
express = require('express');

var app = express();

// middleware which blocks requests when we're too busy app.use(function(req, res, next) {
 if (toobusy()) {
res.send(503, "I'm busy right now, sorry."); console.log("hi");
 } else {
 next();
console.log("hi");
 }
});

console.log("hi");
app.get('/', function(req, res) {
// processing the request requires some work!
var i = 0;
while (i < 1e5) i++;
res.send("I counted to " + i);
console.log("hi");
});

var server = app.listen(3005);

process.on('SIGINT', function() {
server.close();
// calling .shutdown allows your process to exit normally  toobusy.shutdown();
process.exit();
});

关于如何使用该模块的任何想法将非常有帮助。

您仅在应用程序启动时检查服务器是否为toobusy()。

参见下面的中间件用法:

app.use(function(req, res, next) {
    if (toobusy()) {
        res.send(503, "I'm busy right now, sorry.");
    } else {
        next();
    } 
});

这将检查每个传入的请求,并且仅在当前负载低于最大阈值时才调用next()。

您还可以通过以下方式设置最大延迟阈值:

toobusy.maxLag(10);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM