avatar
@bangbang93

fibjs vs nodejs(3)

10/27/2014, 9:41:54 PM

响马回复了

@孢子响马:同时你这个测试中大家的 mongodb 都是一个连接,因此其实在请求 mongodb 时是串行的,所以你这个其实是在测试 mongodb 的 qps

于是改了一下代码,创建了一个500连接的连接池

fibjs

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

var connectPool = [];
for (var i =0;i<500;i++){
    var mongodb = db.openMongoDB('mongodb://localhost/fibvsnode');
    var fibCollection = mongodb.getCollection('fibjs');
    connectPool.push(fibCollection);
}
var connectI = -1;
var getConnect = function (){
    connectI++;
    return (connectPool[connectI % connectPool.length]);
};
var server = new http.Server(12345, function (r){
    getConnect().insert({
        time: new Date()
    });
    r.response.write(JSON.stringify({
        success:true
    }));
});
console.log('server ready');
server.run();

var http = require('http');
var mongodb = require('mongodb').MongoClient;
var connectI = 0;
var connectPool = [];
//nodejs的mongodb自带连接池,所以只需要创建100个连接即可得到500个可用连接
//http://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html
for (var i=0;i<100;i++){
    mongodb.connect('mongodb://localhost/fibvsnode', function (err, db){
        if (err){
            throw err;
        }
        var connection = db.collection('node');
        connectPool.push(connection);
        connectI ++;
        if (connectI == 100){
            connectI = -1;
            console.log('server ready');
        }
    })
}
var getConnection = function (){
    connectI ++;
    return (connectPool[connectI % connectPool.length]);
};
http.createServer(function (req, res){
    getConnection().insert({
        time: new Date()
    }, function (err){
        //为了确保和fib一样是插完库才返回所以放到回调
        res.end(JSON.stringify({
            success: true
        }))
    });
}).listen(12345, '0.0.0.0');

然后跑测试

fibjs

[bangbang93@10-4-14-175 ~]$ ab -n 100000 -c 500 -k 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:   33.297 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    100000
Total transferred:      12700000 bytes
HTML transferred:       1600000 bytes
Requests per second:    3003.23 [#/sec] (mean)
Time per request:       166.487 [ms] (mean)
Time per request:       0.333 [ms] (mean, across all concurrent requests)
Transfer rate:          372.47 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    9 203.3      0   15048
Processing:    10  151 401.5     47   16110
Waiting:       10  151 401.5     47   16110
Total:         10  160 456.3     47   16110

Percentage of the requests served within a certain time (ms)
  50%     47
  66%     50
  75%     57
  80%    282
  90%    294
  95%    767
  98%    801
  99%   1762
 100%  16110 (longest request)

nodejs

[bangbang93@10-4-14-175 ~]$ ab -n 100000 -c 500 -k 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:   89.279 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    0
Total transferred:      9100182 bytes
HTML transferred:       1600032 bytes
Requests per second:    1120.08 [#/sec] (mean)
Time per request:       446.396 [ms] (mean)
Time per request:       0.893 [ms] (mean, across all concurrent requests)
Transfer rate:          99.54 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        9  186 562.7     33   31025
Processing:    13  255 277.5    276    7815
Waiting:       13  255 277.5    276    7815
Total:         33  441 631.2    311   31870

Percentage of the requests served within a certain time (ms)
  50%    311
  66%    321
  75%    332
  80%    351
  90%   1088
  95%   1324
  98%   1828
  99%   3310
 100%  31870 (longest request)

node败下阵了


编辑文章查看草稿的时候发现我二了
nodejs自带连接池我为什么要手动维护
于是代码改成了

var http = require('http');
var mongodb = require('mongodb').MongoClient;
mongodb.connect('mongodb://localhost/fibvsnode?maxPoolSize=500', function (err, db) {
    if (err) {
        throw err;
    }
    var connection = db.collection('node');
    http.createServer(function (req, res) {
        connection.insert({
            time: new Date()
        }, function (err) {
            //为了确保和fib一样是插完库才返回所以放到回调
            res.end(JSON.stringify({
                success: true
            }))
        });
    }).listen(12345, '0.0.0.0');
});
[bangbang93@10-4-14-175 ~]$ ab -n 100000 -c 500 -k 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:   92.204 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    0
Total transferred:      9100000 bytes
HTML transferred:       1600000 bytes
Requests per second:    1084.55 [#/sec] (mean)
Time per request:       461.020 [ms] (mean)
Time per request:       0.922 [ms] (mean, across all concurrent requests)
Transfer rate:          96.38 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        9  193 592.7     33   15054
Processing:    13  265 301.1    277   15911
Waiting:       13  264 300.8    276   15911
Total:         31  457 671.0    311   15959

Percentage of the requests served within a certain time (ms)
  50%    311
  66%    320
  75%    333
  80%    352
  90%   1100
  95%   1327
  98%   1883
  99%   3315
 100%  15959 (longest request)

不过没什么变化
不过在这台机器里node报
js-bson: Failed to load c++ bson extension, using pure JS version
的问题我还没解决,应该也是影响性能的。改天换台服务器再测