6

问题:我得到的结果合理吗?有什么东西可以对减少每秒的请求数产生如此大的影响吗?

编辑:我的一个朋友刚刚在 Linux 上对相同的应用程序进行了基准测试,平均 r/s 约为 7000。

编辑#2:我检查了 Node.exe 的 CPU 使用率,它只使用了 5-6% 的 cpu。如果真的在负载下运行在单线程上时,这个数字在四核机器上肯定应该是 12%,8 线程 CPU?

我编写了一个 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');
  }       
});

除了上述代码之外,唯一值得注意的剩余代码是一系列 Expressapp.getapp.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 Web 服务器上的静态文件执行了相同的测试:
每秒 6000 个请求

现在这个基准测试显示 Node.js 击败了 Apache!
http://zgadzaj.com/benchmarking-nodejs-basic-performance-tests-against-apache-php

我的相关硬件规格:
Intel i7 2630qm
6 GB RAM

4

4 回答 4

19

我可以通过在同一台机器上的 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
于 2012-02-11T22:57:16.847 回答
10

我发现如果您将其称为“localhost”,该节点的工作非常缓慢,但如果您将其称为“127.0.0.1”,则非常快。我认为 Windows 上的 dns 查找确实是减慢请求速度的原因。

于 2013-07-11T23:44:36.553 回答
2

有什么东西可以对减少每秒的请求数产生如此大的影响吗?

我假设您认为某些东西在您的测试中减慢了 Node 的速度。没有。但是,在最后链接到的测试中,有一些东西会减慢Apache的速度。

您说您正在使用静态文件测试 Apache,但您链接到的基准测试使用 PHP 文件进行 Apache 测试。Apache 直接为未解释的文件提供服务几乎与金属一样接近。但是添加 PHP,会增加大量开销。

因此,对于您的测试,它是 Apache 与 Node 和 Apache 获胜,而在您链接到它的测试中,Apache + PHP 与 Node 和 Node 获胜。

这两个结果都没有让我感到惊讶。

于 2012-02-11T19:37:09.937 回答
1

您是否意识到 Apache 使用多个进程或线程来处理请求,而只有一个 Node.js 进程?要么为单个工作进程或线程配置 Apache,要么运行与 CPU 内核一样多的 Node.js 进程,并在它们之间进行负载平衡。

于 2012-02-11T20:04:48.290 回答