avatar
@bangbang93

fibjs vs nodejs

10/27/2014, 6:49:54 PM

网上见过的对比都是只有一个Hello, world!的输出。其实这毫无意义啊。于是打算自己敲一个对比

大致思路是收到http请求后去mongodb插一条数据,插完返回一段json,用来简单模拟一个创建的请求,以求更贴近业务

先说一下运行环境
CentOS 6.5 X86_64
双路Xeon X5470
16G内存

然后贴代码


先贴fibjs的

var db = require('db');
var http = require('http');

var mongodb = db.openMongoDB('mongodb://localhost/fibvsnode');
var fibCollection = mongodb.getCollection('fibjs');
var server = new http.Server(12345, function (r){
    fibCollection.insert({
        time: new Date()
    });
    r.response.write(JSON.stringify({
        success:true
    }));
});
server.run();

然后是Node.JS的

var http = require('http');
var mongodb = require('mongodb').MongoClient;
mongodb.connect('mongodb://localhost/fibvsnode', function (err, db){
    var collection = db.collection('node');
    http.createServer(function (req, res){
        collection.insert({
            time: new Date()
        }, function (err){
            //为了确保和fib一样是插完库才返回所以放到回调
            res.end(JSON.stringify({
                success: true
            }))
        });
    }).listen(12345, '0.0.0.0');
});

发起请求的是另外一台服务器

ab -n 100000 -c 500 http://xxxx:12345/

用来施压的服务器和被压的服务器不是同一台以减少影响


然后是fibjs的结果

[bangbang93@10-4-14-175 ~]$ ab -n 100000 -c 500 http://server.bangbang93.com:12345/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking server.bangbang93.com (be patient)
apr_socket_recv: Connection reset by peer (104)
Total of 4352 requests completed

在mongo里查询
db.fibjs.find().count()
结果是4654
死的太快我都没来得及看CPU占用


然后是node.js的结果

[bangbang93@10-4-14-175 ~]$ ab -n 100000 -c 500 http://server.bangbang93.com:12345/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking server.bangbang93.com (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests


Server Software:        
Server Hostname:        server.bangbang93.com
Server Port:            12345

Document Path:          /
Document Length:        16 bytes

Concurrency Level:      500
Time taken for tests:   79.381 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      9100182 bytes
HTML transferred:       1600032 bytes
Requests per second:    1259.75 [#/sec] (mean)
Time per request:       396.903 [ms] (mean)
Time per request:       0.794 [ms] (mean, across all concurrent requests)
Transfer rate:          111.95 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        9  156 463.2     31   15034
Processing:    11  239 248.1    272   15343
Waiting:       11  239 247.9    271   15343
Total:         31  395 527.9    303   15374

Percentage of the requests served within a certain time (ms)
  50%    303
  66%    309
  75%    315
  80%    321
  90%   1062
  95%   1307
  98%   1754
  99%   3073
 100%  15374 (longest request)

在mongo里
db.node.find().count()
返回100068

top里的node进程

14340 bangbang 20 0 1021m 68m 5620 S 74.4 0.4 0:43.78 node