我本来想写一个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楼.
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();
真服了你了,你居然用一个这么复杂运算的场景来评测基础架构的性能。
基础架构的速度是每秒万级的,经过你的算法一平均,大家都是每秒一两次,比个球啊。大家比的是 v8 的运行速度,有什么好比的? 况且你看你自己选的算法,连循环的次数都是不确定随机的,你能测试出什么来?谁倒霉谁慢呗。
楼主的测试没有参考价值,走极端了,理论上,加上巨复杂的业务逻辑之后,大家的性能就趋同了,因为大家都是用的V8。
楼主降低循环次数(10000级别),增加并发访问数量,当然还要保证循环次数一样,再测试下看看。
我的笔记本性能不高, 你可以提高到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 下的内存占用。然后绘制曲线,观察不同测试目标在不同场景的数据变化。