Node.js 在 Windows 上的表现是否很差,对于基本 I/O 肯定不会比 apache 慢
问题:我得到的结果合理吗?有什么东西可以对减少每秒请求数产生如此大的影响吗?
编辑:我的一个朋友刚刚在 Linux 上对相同的应用程序进行了基准测试,平均 r/s 约为 7000。
编辑#2:我检查了Node.exe,它只使用 5-6% 的 cpu。当然,如果真正在负载下运行在单线程上,这个数字在四核机器、8 线程 CPU 上应该是 12%?
我编写了一个 Node.js 应用程序(运行 Node v0.6.10)并使用 apachebench 对其进行了基准测试:ab -c 256 -n 50000 http://localhost:3000/
。我收到的每秒请求率为每秒大约 650 个请求。代码太多,无法放在这里,但这是基本结构:
应用程序设置:
/**
* Module dependencies.
*/
var util = require('util'), //Useful for inspecting JSON objects
express = require('express'), //Express framework, similar to sinatra for ruby
connect = require('connect'), //An integral part of the express framework
app = module.exports = express.createServer(), //Create the server
io = require('socket.io').listen(app), //Make Socket.io listen on the server
parseCookie = require('connect').utils.parseCookie, //Parse cookies to retrieve session id
MemoryStore = require('connect').session.MemoryStore, //Session memory store
sessionStore = new MemoryStore(),
Session = require('connect').middleware.session.Session,
mongodb = require('mongodb'), //MongoDB Database
BSON = mongodb.BSONPure, //Used to serialize JSON into BSON [binary]
sanitize = require('validator').sanitize;
// Configuration
app.configure(function()
{
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({
store: sessionStore,
secret: '...',
key: 'express.sid'}));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
//app.use(express.errorHandler({dumpExceptions: true, showStack: true}));
});
app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
io.configure('development', function()
{
io.set('transports', ['websocket']);
//io.set('heartbeats', false);
//io.set('heartbeat timeout', 200);
//io.set('heartbeat interval', 220);
});
//MongoDB Database port and ip
var DATABASE_PORT = 27017;
var DATABASE_IP = "127.0.0.1"; //Localhost
/*
setInterval(function(){
console.log("BROWSING:\n" + util.inspect(browsing));
}, 1000);
*/
//Connected users
var validUsers = {};
var clients = {};
var browsing = {};
//Database handles
var users;
var projects;
//Connect to the database server
db = new mongodb.Db('nimble', new mongodb.Server(DATABASE_IP, DATABASE_PORT, {}, {}));
db.open(function (error, client)
{
if (error) {
console.error("Database is currently not running");
throw error;
}
users = new mongodb.Collection(client, 'users'); //Handle to users
projects = new mongodb.Collection(client, 'projects'); //Handle to projects
});
app.get('/', function(req, res)
{
//users.insert("test", {safe:true});
//users.findOne("test", function(result){})
if(req.session.auth)
{
if(req.session.account == "client")
{
//Redirect to the client dash
res.redirect('/dash');
}
else if (req.session.account == "developer")
{
res.redirect('/projects');
}
}
else
{
res.redirect('/login');
}
});
除了上面的代码之外,唯一值得注意的剩余代码是一系列 Express app.get
和 app.post
事件处理程序。
我在基本的 Express 设置 Web 服务器和基本的 Node.js http Web 服务器上执行了相同的测试。
Node.js 与 Express 服务器
var express = require('express');
var app = express.createServer();
app.get('/', function(req, res){
res.send();
});
app.listen(3000);
Node.js HTTP
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end();
}).listen(3000, "127.0.0.1");
结果是:
Express 上每秒 2000 个请求
Node.js 上每秒 2200 个请求
我对 Apache Web 服务器上托管的静态文件执行了相同的测试:
每秒 6000 个请求
现在这个基准测试显示 Node.js 轻而易举地击败了 Apache!
http://zgadzaj.com/benchmarking-nodejs-basic-performance -测试针对-apache-php
我的相关硬件规格:
英特尔 i7 2630qm
6 GB 内存
Question: Are the result that i'm getting reasonable? Is there anything which could have such an impact in reducing the number of requests per second?
Edit: A friend of mine has just benchmarked the same application on Linux and the average r/s was approx 7000.
Edit #2: I've checked the CPU usage of Node.exe, and it's only using 5-6% of the cpu. Surely this figure should be 12% on a quad core machine, 8 thread CPU when running on a single thread if truly under load?
I've written a Node.js application (running Node v0.6.10) and benchmarked it with apachebench: ab -c 256 -n 50000 http://localhost:3000/
. I'm getting a request per second rate of roughly 650 requests per second. There's too much code to put here, however this is the basic structure:
Application Settings:
/**
* Module dependencies.
*/
var util = require('util'), //Useful for inspecting JSON objects
express = require('express'), //Express framework, similar to sinatra for ruby
connect = require('connect'), //An integral part of the express framework
app = module.exports = express.createServer(), //Create the server
io = require('socket.io').listen(app), //Make Socket.io listen on the server
parseCookie = require('connect').utils.parseCookie, //Parse cookies to retrieve session id
MemoryStore = require('connect').session.MemoryStore, //Session memory store
sessionStore = new MemoryStore(),
Session = require('connect').middleware.session.Session,
mongodb = require('mongodb'), //MongoDB Database
BSON = mongodb.BSONPure, //Used to serialize JSON into BSON [binary]
sanitize = require('validator').sanitize;
// Configuration
app.configure(function()
{
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({
store: sessionStore,
secret: '...',
key: 'express.sid'}));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
//app.use(express.errorHandler({dumpExceptions: true, showStack: true}));
});
app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
io.configure('development', function()
{
io.set('transports', ['websocket']);
//io.set('heartbeats', false);
//io.set('heartbeat timeout', 200);
//io.set('heartbeat interval', 220);
});
//MongoDB Database port and ip
var DATABASE_PORT = 27017;
var DATABASE_IP = "127.0.0.1"; //Localhost
/*
setInterval(function(){
console.log("BROWSING:\n" + util.inspect(browsing));
}, 1000);
*/
//Connected users
var validUsers = {};
var clients = {};
var browsing = {};
//Database handles
var users;
var projects;
//Connect to the database server
db = new mongodb.Db('nimble', new mongodb.Server(DATABASE_IP, DATABASE_PORT, {}, {}));
db.open(function (error, client)
{
if (error) {
console.error("Database is currently not running");
throw error;
}
users = new mongodb.Collection(client, 'users'); //Handle to users
projects = new mongodb.Collection(client, 'projects'); //Handle to projects
});
app.get('/', function(req, res)
{
//users.insert("test", {safe:true});
//users.findOne("test", function(result){})
if(req.session.auth)
{
if(req.session.account == "client")
{
//Redirect to the client dash
res.redirect('/dash');
}
else if (req.session.account == "developer")
{
res.redirect('/projects');
}
}
else
{
res.redirect('/login');
}
});
Apart from the above code the only notable remaining code is a series of Express app.get
and app.post
event handlers.
I have performed the same test on a basic Express set up web server, and the basic node.js http web server.
Node.js with Express server
var express = require('express');
var app = express.createServer();
app.get('/', function(req, res){
res.send();
});
app.listen(3000);
Node.js HTTP
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end();
}).listen(3000, "127.0.0.1");
The results being:
2000 requests per second on Express
2200 requests per second on Node.js
I've performed the same test against a static file hosted on an Apache web server:
6000 requests per second
Now this benchmark shows Node.js beating Apache hands down!
http://zgadzaj.com/benchmarking-nodejs-basic-performance-tests-against-apache-php
My relevant hardware spec:
Intel i7 2630qm
6 GB RAM
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通过在同一台机器上安装的 Linux 上测试我自己的应用程序,我可以得出结论,它在 Windows 上实际上非常慢。我不确定这是我的 Windows 安装还是所有 Windows 安装。
相同的应用程序无需任何更改就能够在 Linux 上处理每秒 3500 个请求。速度快了 500% 以上...
如果您有类似的情况,请随时在此处发帖根据我自己的经验,我想知道这是否是Windows问题。
在同一台机器上进行基准测试,首先启动Linux,然后启动Windows。
I can conclude through testing my own application on a Linux install on the same machine that it was infact VERY slow on Windows. I'm unsure as to whether that's my Windows install or ALL Windows installs.
The same application with no change was able to deal with 3500 request / second on Linux. Which is over 500% faster...
Please feel free to post here if you've had a similar experience to myself, i'd like to know if this is a Windows problem.
Benchmarking on the same machine, first booted into Linux, and then Windows.
我发现,如果您将其称为“localhost”,则节点运行速度非常慢,但如果您将其称为“127.0.0.1”,则运行速度非常快。我认为 Windows 上的 dns 查找确实是减慢请求速度的原因。
I've found that node works really really slowly if you refer to it as "localhost", but very quickly if you refer to it by "127.0.0.1". I think the dns lookup on Windows is really what's slowing the requests down.
我假设您认为某些东西正在减慢测试中的 Node 速度。没有。然而,在最后链接到的测试中,有一些东西会减慢 Apache 的速度。
您说您正在使用静态文件测试 Apache,但您链接到的基准测试使用 PHP 文件进行 Apache 测试。 Apache 直接提供未解释的文件几乎是最接近金属的。但是添加 PHP,就会增加大量的开销。
因此,对于您的测试,这是 Apache 与 Node 和 Apache 获胜,而在您链接到的测试中,是 Apache + PHP 与 Node 和 Node 获胜。
这两个结果都不让我感到惊讶。
I assume you're thinking that something is slowing down Node in your tests. There isn't. However, in tests to you link to at the end, there's something slowing Apache down.
You say you're testing Apache with a static file, but the benchmarks you link to, use a PHP file for the Apache test. Apache serving an uninterpreted file directly is about as close to metal as you can get. But add PHP, and you're adding a ton of overhead.
So for your test, it's Apache vs. Node and Apache wins, whereas in the one you linked to it's Apache + PHP vs. Node and Node wins.
Neither result surprises me much.
您是否意识到 Apache 使用多个进程或线程来处理请求,而 Node.js 只有一个进程?为单个工作进程或线程配置 Apache,或者运行与 CPU 核心数量相同的 Node.js 进程,并在它们之间进行负载平衡。
Do you realize that Apache uses multiple processes or threads to serve the requests, whereas there's a single Node.js process? Either configure Apache for a single worker process or thread, or run as many Node.js processes as you have CPU cores, and loadbalance among them.