精华 nodejs fibjs, 不如让我发个测试吧, 不要再发Hello world!
发布于 18天前 作者 tulayang 527 次浏览 来自 分享

我本来想写一个socket通信服务的测试, 不过fibjs的文档实在不够详细, 就退而求其次, 写了一个模拟数据服务.

现在, 服务器代码做了修改, 数据服务被放到一个外部进程中. 这样服务器就没有"读取压力了". 我是很匪夷所思, 同是js, fibjs如何被"拖累"的.

不过现在看新的测试, 1楼仍然是修改后的测试代码. (Nodejs CPU 性能更佳了)

Nodejs服务器和Fibjs服务器都提供同样的服务, 模拟数据服务:

在服务器加载前, 存储1000000条数据, 每条数据有一个随机数作为标记.

当有请求时, 服务器制作一个随机数, 去1000000条数据找出这条和随机数匹配的第一个. 如果没有, 使用第一条数据.

  • Nodejs: 127.0.0.1:8100
  • Fibjs: 127.0.0.1:8200

同时运行服务

请求模拟:

制作一个不停向服务器请求的客户端, 当响应后立刻再重新发出请求, 连接是非长久连接, agent池被禁用. 另外, 同时发出10条请求. 每个请求会自己独立记录从发出请求到接收响应的时间.

也就是说, 会同时有10条独立的客户端, 不停向服务器发出请求, 服务器响应后, 每个请求又立刻重新发出.

测试结果:

运行3分钟后, 截取的当前命令行输出.

Nodejs的响应比较平稳, 3000ms ~ 5000ms之间, Fibjs的响应不平均, 偶尔会有700ms, 但是7000ms, 8000ms也长有.

Linux TOP 命令下的内存, CPU参数对比

PID  USER PR NIVIRT    RES    SHR  S  %CPU  %MEM  TIME+    COMMAND
4388 king 20 0 1672292 223680 5708 S  4.0   11.3  0:03.21  fibjs 
4665 king 20 0 838488  193276 5568 S  1.3   9.8   0:03.34  node 

客户端连接Nodejs服务器输出:

Id 1 Last time: 3564ms
Id 5 Last time: 3619ms
Id 4 Last time: 3715ms
Id 2 Last time: 3313ms
Id 1 Last time: 3185ms
Id 3 Last time: 3792ms
Id 4 Last time: 3484ms
Id 5 Last time: 3765ms
Id 2 Last time: 3122ms
Id 1 Last time: 3126ms
Id 4 Last time: 3591ms
Id 3 Last time: 3758ms
Id 5 Last time: 3656ms
Id 2 Last time: 2700ms
Id 1 Last time: 3049ms
Id 4 Last time: 3243ms
Id 3 Last time: 3254ms
Id 5 Last time: 3258ms
Id 2 Last time: 3512ms
Id 1 Last time: 3343ms
Id 3 Last time: 3080ms
Id 4 Last time: 3329ms
Id 5 Last time: 3466ms
Id 2 Last time: 3362ms
Id 1 Last time: 3348ms
Id 3 Last time: 3014ms
Id 4 Last time: 3141ms
Id 5 Last time: 3397ms
Id 2 Last time: 2979ms
Id 1 Last time: 3113ms
Id 3 Last time: 3146ms
Id 4 Last time: 3303ms
Id 5 Last time: 3407ms
Id 2 Last time: 2977ms
Id 1 Last time: 3405ms
Id 3 Last time: 3257ms
Id 4 Last time: 3201ms
Id 5 Last time: 3310ms
Id 2 Last time: 3394ms
Id 1 Last time: 2995ms
Id 3 Last time: 2935ms
Id 4 Last time: 3677ms
Id 5 Last time: 3292ms
Id 2 Last time: 3136ms
Id 1 Last time: 3723ms
Id 3 Last time: 3894ms
Id 4 Last time: 2955ms
Id 5 Last time: 2789ms
Id 2 Last time: 3158ms
Id 3 Last time: 2764ms
Id 1 Last time: 3803ms
Id 4 Last time: 3104ms
Id 5 Last time: 3613ms
Id 2 Last time: 3601ms
Id 3 Last time: 2860ms
Id 1 Last time: 3242ms
Id 4 Last time: 3206ms
Id 5 Last time: 3182ms
Id 2 Last time: 3755ms
Id 3 Last time: 3580ms
Id 1 Last time: 2934ms
Id 4 Last time: 3671ms
Id 5 Last time: 3564ms
Id 3 Last time: 2879ms
Id 2 Last time: 3765ms
Id 1 Last time: 3303ms
Id 4 Last time: 3335ms
Id 5 Last time: 3182ms
Id 3 Last time: 3500ms
Id 2 Last time: 3505ms
Id 1 Last time: 3464ms
Id 5 Last time: 3002ms
Id 4 Last time: 3680ms
Id 3 Last time: 3633ms
Id 2 Last time: 3897ms
Id 1 Last time: 4061ms
Id 5 Last time: 3489ms
Id 4 Last time: 3586ms
Id 2 Last time: 2873ms
Id 3 Last time: 3905ms

客户端连接Fibjs服务器输出:

Id 1 Last time: 3836ms
Id 2 Last time: 3262ms
Id 4 Last time: 3279ms
Id 5 Last time: 3553ms
Id 3 Last time: 3428ms
Id 1 Last time: 3586ms
Id 4 Last time: 3243ms
Id 2 Last time: 3407ms
Id 5 Last time: 3237ms
Id 3 Last time: 3266ms
Id 1 Last time: 3726ms
Id 2 Last time: 3279ms
Id 4 Last time: 3426ms
Id 3 Last time: 3211ms
Id 5 Last time: 3872ms
Id 1 Last time: 2940ms
Id 3 Last time: 3112ms
Id 2 Last time: 3431ms
Id 4 Last time: 3503ms
Id 1 Last time: 3428ms
Id 5 Last time: 3486ms
Id 3 Last time: 3132ms
Id 2 Last time: 3245ms
Id 1 Last time: 2913ms
Id 4 Last time: 3764ms
Id 5 Last time: 3471ms
Id 3 Last time: 2843ms
Id 2 Last time: 3398ms
Id 4 Last time: 2810ms
Id 1 Last time: 3560ms
Id 5 Last time: 3649ms
Id 3 Last time: 3761ms
Id 2 Last time: 3082ms
Id 1 Last time: 3212ms
Id 4 Last time: 3759ms
Id 5 Last time: 2968ms
Id 2 Last time: 3055ms
Id 3 Last time: 3507ms
Id 1 Last time: 3065ms
Id 4 Last time: 3352ms
Id 5 Last time: 3367ms
Id 3 Last time: 3272ms
Id 2 Last time: 3836ms
Id 1 Last time: 3169ms
Id 4 Last time: 3160ms
Id 5 Last time: 3686ms
Id 2 Last time: 3185ms
Id 1 Last time: 3254ms
Id 3 Last time: 3575ms
Id 4 Last time: 3371ms
Id 5 Last time: 3625ms
Id 2 Last time: 3048ms
Id 1 Last time: 3711ms
Id 3 Last time: 3527ms
Id 4 Last time: 3464ms
Id 5 Last time: 3576ms
Id 3 Last time: 2816ms
Id 2 Last time: 3628ms
Id 1 Last time: 3582ms
Id 4 Last time: 3853ms
Id 5 Last time: 2936ms
Id 3 Last time: 2963ms
Id 2 Last time: 2924ms
Id 1 Last time: 3470ms
Id 5 Last time: 3381ms
Id 4 Last time: 3865ms
Id 2 Last time: 2870ms
Id 3 Last time: 2890ms
Id 1 Last time: 4070ms
Id 4 Last time: 3338ms
Id 5 Last time: 3625ms
Id 3 Last time: 3068ms
Id 2 Last time: 3509ms
Id 1 Last time: 3503ms
Id 4 Last time: 3836ms

为了防止混乱, 测试代码放在1楼.

19 回复

I Nodejs 服务器源码

var child_process = require('child_process');
var http = require('http');
var server = http.createServer();
server.on('request', function (req, res) {
    child_process.exec('node /home/king/server/read.js', function () {
        res.writeHead(200);
        res.end('OK');
    });
});
server.listen(8100);

II Fibjs 服务器源码

var process = require('process');
var http = require('http');
var svr = new http.Server(8200, function(r){
    process.system('node /home/king/server/read.js');
    r.response.write('OK');
});
svr.run();

III 连接Nodejs服务器客户端源码, 采用Nodejs request

var http = require('http');

function request(id, port, lastRes, time) {
    if (lastRes) {
        console.log('Id %d Last time: %dms', id, time);
    } else {
        console.log('Id %d Start test', id);
    }
    var start = Date.now();
    var req = http.request({
        hostname : '127.0.0.1',
        port     : port,
        path     : '/',
        method   : 'get',
        agent    : false
    }, function(res) {
        var data = '';
        res.setEncoding('utf8');
        res.on('data', function(d) {
            data += d;
        });
        res.on('end', function() {
            res.body = data;
            var end = Date.now();
            request(id, port, res, end - start);
        });
    });
    req.end();
}

function multiRequest(n, port) {
    var i = 0;
    while (i < n) {
        request(i + 1, port, null, null);
        i++;
    }
}   

multiRequest(5, 8100);

IV 连接Fibjs服务器客户端源码, 采用Nodejs request

var http = require('http');

function request(id, port, lastRes, time) {
    if (lastRes) {
        console.log('Id %d Last time: %dms', id, time);
    } else {
        console.log('Id %d Start test', id);
    }
    var start = Date.now();
    var req = http.request({
        hostname : '127.0.0.1',
        port     : port,
        path     : '/',
        method   : 'get',
        agent    : false
    }, function(res) {
        var data = '';
        res.setEncoding('utf8');
        res.on('data', function(d) {
            data += d;
        });
        res.on('end', function() {
            res.body = data;
            var end = Date.now();
            request(id, port, res, end - start);
        });
    });
    req.end();
}

function multiRequest(n, port) {
    var i = 0;
    while (i < n) {
        request(i + 1, port, null, null);
        i++;
    }
}   

multiRequest(5, 8200);

V /home/king/server/read.js进程服务

var i = 0, len = 1000000, sets = [];
while (i < len) {
    sets.push({
        _id: '_' + i, 
        name: 'name_' + i,
        keyword: Math.random()
    });
    i++;
}

function read() {
    var random = Math.random();
    var i = 0;
    var set = null, keyword;
    while (i < len) {
        if (false/*random >= sets[i].keyword*/) {
            set = sets[i];
            break;
        }
        i++;
    }
    if (set === null) {
        set = sets[0];
    }
    return set;
}

read();

计算了下平均时间,node是3381ms, fibjs是3932 貌似fibjs更慢????

真服了你了,你居然用一个这么复杂运算的场景来评测基础架构的性能。

基础架构的速度是每秒万级的,经过你的算法一平均,大家都是每秒一两次,比个球啊。大家比的是 v8 的运行速度,有什么好比的? 况且你看你自己选的算法,连循环的次数都是不确定随机的,你能测试出什么来?谁倒霉谁慢呗。 Screen Shot 2015-01-13 at 2.35.26 PM.png

上一份基础架构性能测试对比,0-5000 连接下的 qps。不同设备测试结果会不同,越好的设备差异越大。这是在我的笔记本上的结果。 Screen Shot 2015-01-13 at 2.42.35 PM.png

@xicilion

是吗? 运算都是js层面计算的, 总不至于fibjs就被拖累到, 唯一的不足就是没有交给另一个外部进程.

@xicilion

压力测试工具, 只能比较平均值, 实际在体验中, 每个用户只关心从自己请求到响应的速度. 如果可以, 做一个HTTP TCP服务通信的实时输出.

楼主的测试没有参考价值,走极端了,理论上,加上巨复杂的业务逻辑之后,大家的性能就趋同了,因为大家都是用的V8。

楼主降低循环次数(10000级别),增加并发访问数量,当然还要保证循环次数一样,再测试下看看。

支持楼上。启动结果,好奇。。。

@tulayang 好的 js 程序员是可以写出比烂的 c++ 程序员性能更好的代码的。不过咱能不说这些废话吗?

那我们就来些进程通信, 这还不简单

@xicilion

事实上, 把服务放到服务器进程外, 对比也一目了然

@tulayang 鼓励多做进一步测试,也帮 fibjs 多发现一些问题呵呵。

@tulayang 好吧,才发现你改了测试。

在 cpu 占用只有 4% 和 1.3% 的情形下,你确定你是在测试性能吗?你还不如干脆不请求,大家比静默负载呢。

@xicilion

我的笔记本性能不高, 你可以提高到1000请求, 自己测试.

这个测试是比较: [ 请求进来, 转入服务器业务进程处理, … 服务器业务进程处理完毕, 发回响应 ] 每个客户端在这个过程的体验和服务器CPU, 内存的消耗率.

node /home/king/server/read.js 这个服务, 两个服务器都是使用的相同服务.

唉,怎么测试还要我教你。

首先消除测试部分以外的干扰,也就是只测试要测试的单元本身,尽可能把与其不相关的部分摘除。否则你的测试结果就是脏的。以这个例子,我们测试的就是 hello world,把压力全部放在 web sever 本身。

其次,根据测试单元设计测试模式和数据。比如我们需要测试 web server 的请求响应性能,不同负载下的 cpu 占用和内存占用。

再次,规划设计方法。根据上面的描述,我们需要测试 0-5000 连接下的 qps;qps 0-10000 下的 CPU 占用;qps 1-10000 下的内存占用。然后绘制曲线,观察不同测试目标在不同场景的数据变化。

@xicilion

拜托, 服务器本身就没有东西, 只负责转移.

所有的服务都是node /home/king/server/read.js, 这是在单独的进程里运行的, 跟你服务器没有关系. 服务完成, 通知服务器, 服务器收到, 发回响应.

何来的干扰?

Hello word根本不涉及服务器的TCP通信转发, 也不涉及进程间的通信, 有何测试效力.

@tulayang hello world 的 web server 啊兄弟!!!!

@xicilion

hello world只能说明:

Fibjs的r.response.write()提交给操作系统比Nodejs的res.write()提交给操作系统的过程要快.

回到顶部