javascript 如何将 Node.js 集群与我的简单 Express 应用程序一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10663809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I use Node.js clusters with my simple Express app?
提问by vjk2005
— I built a simple app that pulls in data (50 items) from a Redis DB and throws it up at localhost. I did an ApacheBench (c = 100, n = 50000) and I'm getting a semi-decent 150 requests/sec on a dual-core T2080 @ 1.73GHz (my 6 y.o laptop), but the proc usage is very disappointing as shown:
— 我构建了一个简单的应用程序,它从 Redis 数据库中提取数据(50 项)并将其发送到本地主机。我做了一个 ApacheBench(c = 100,n = 50000),我在双核 T2080 @ 1.73GHz(我的 6 岁笔记本电脑)上获得了 150 个不错的请求/秒,但是 proc 的使用非常令人失望,因为显示:
Only one core is used, which is as per design in Node, but I think I can nearly double my requests/sec to ~300, maybe even more, if I can use Node.js clusters. I fiddled around quite a bit but I haven't been able to figure out how to put the code given herefor use with my app which is listed below:
只使用了一个核心,这是按照 Node 中的设计,但我认为我的请求/秒几乎可以翻倍,达到 ~300,如果我可以使用 Node.js 集群,甚至可能更多。我摆弄了很多,但我一直无法弄清楚如何将此处给出的代码与下面列出的我的应用程序一起使用:
var
express = require( 'express' ),
app = express.createServer(),
redis = require( 'redis' ).createClient();
app.configure( function() {
app.set( 'view options', { layout: false } );
app.set( 'view engine', 'jade' );
app.set( 'views', __dirname + '/views' );
app.use( express.bodyParser() );
} );
function log( what ) { console.log( what ); }
app.get( '/', function( req, res ) {
redis.lrange( 'items', 0, 50, function( err, items ) {
if( err ) { log( err ); } else {
res.render( 'index', { items: items } );
}
});
});
app.listen( 8080 );
I also want to emphasize that the app is I/O intensive (not CPU-intensive, which would've made something like threads-a-gogoa better choice than clusters).
我还想强调的是,该应用程序是 I/O 密集型的(不是 CPU 密集型的,这会使线程成为比集群更好的选择)。
Would love some help in figuring this out.
希望得到一些帮助来解决这个问题。
回答by Didier Spezia
Actually, your workload is not really I/O bound: it is CPU bound due to the cost of jade-based dynamic page generation. I cannot guess the complexity of your jade template, but even with simple templates, generating HTML pages is expensive.
实际上,您的工作负载并非真正受 I/O 限制:由于基于 Jade 的动态页面生成的成本,它受 CPU 限制。我无法猜测您的 jade 模板的复杂性,但即使使用简单的模板,生成 HTML 页面的成本也很高。
For my tests I used this template:
对于我的测试,我使用了这个模板:
html(lang="en")
head
title Example
body
h1 Jade - node template engine
#container
ul#users
each user in items
li User:#{user}
I added 100 dummy strings to the items key in Redis.
我在 Redis 的 items 键中添加了 100 个虚拟字符串。
On my box, I get 475 req/s with node.js CPU at 100% (which means 50% CPU consumption on this dual core box). Let's replace:
在我的机器上,我在 node.js CPU 100% 时获得 475 req/s(这意味着在这个双核机器上 CPU 消耗为 50%)。让我们替换:
res.render( 'index', { items: items } );
by:
经过:
res.send( '<html lang="en"><head><title>Example</title></head><body><h1>Jade - node template engine</h1><div id="container"><ul id="users"><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li><li>User:NOTHING</li></ul></div></body></html>' );
Now, the result of the benchmark is close to 2700 req/s. So the bottleneck is clearly due to the formatting of the HTML page.
现在,基准测试的结果接近 2700 req/s。所以瓶颈显然是由于 HTML 页面的格式。
Using the cluster package in this situation is a good idea, and it is straightforward. The code can be modified as follows:
在这种情况下使用集群包是一个好主意,而且很简单。代码可以修改如下:
var cluster = require('cluster')
if ( cluster.isMaster ) {
for ( var i=0; i<2; ++i )
cluster.fork();
} else {
var
express = require( 'express' ),
app = express.createServer(),
redis = require( 'redis' ).createClient();
app.configure( function() {
app.set( 'view options', { layout: false } );
app.set( 'view engine', 'jade' );
app.set( 'views', __dirname + '/views' );
app.use( express.bodyParser() );
});
function log( what ) { console.log( what ); }
app.get( '/', function( req, res ) {
redis.lrange( 'items', 0, 50, function( err, items ) {
if( err ) { log( err ); } else {
res.render( 'index', { items: items } );
}
});
});
app.listen( 8080 );
}
Now the result of the benchmark is close to 750 req/s with 100 % CPU consumption (to be compared with the initial 475 req/s).
现在基准测试的结果接近 750 req/s,100% CPU 消耗(与最初的 475 req/s 相比)。