问题:我得到的结果合理吗?有什么东西可以对减少每秒请求数产生如此大的影响吗?
编辑:我的一个 friend 刚刚在 Linux 上对同一个应用程序进行了基准测试,平均 r/s 约为 7000。
编辑 #2: 我检查了 Node.exe 的 CPU 使用率,它只使用了 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 服务器上执行了相同的测试。
带有 Express 服务器的 Node.js
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 网络服务器上的静态文件执行了相同的测试:
每秒 6000 个请求
现在这个基准测试表明 Node.js 轻而易举地击败了 Apache!
http://zgadzaj.com/benchmarking-nodejs-basic-performance-tests-against-apache-php
我的相关硬件规范:
英特尔 i7 2630qm
6 GB 内存
最佳答案
我可以通过在同一台机器上的 Linux 安装上测试我自己的应用程序得出结论,它实际上在 Windows 上非常慢。我不确定这是我的 Windows 安装还是所有 Windows 安装。
相同的应用程序没有任何变化能够在 Linux 上处理 3500 个请求/秒。速度提高了 500% 以上...
如果您有与我类似的经历,请随时在这里发帖,我想知道这是否是 Windows 的问题。
在同一台机器上进行基准测试,首先启动到 Linux,然后是 Windows。
Linux GET R/s 3534 3494 3568 3580 3528
CLUSTER GET R/s 11987 11565 11950 12042 12056
GET DB INSERT R/s 2251 2201 2167 2207 2132
GET DB SELECT R/s 2436 2512 2407 2457 2496
Windows GET R/s 725 730 721 760 723
CLUSTER GET R/s 3072 3129 3421 2912 3203
GET DB INSERT R/s 611 623 605 632 610
GET DB SELECT R/s 672 691 701 698 682
关于apache - Node.js 在 Windows 上表现不佳吗,它的基本 I/O 肯定不会比 apache 慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9243221/