
k8s 部署 websocet-bench 测试 socket.io
k8s 部署 websocet-bench 测试 socket.io简介编译websocket-bench 镜像根据情况编写generator编写yaml启动测试简介github项目地址websocket bench是一款能够用来测试websocket服务性能的工具,暂时支持Socket.IO,Engine.IO,Faye,Primus,WAMP等框架的测试。Socket.io作为一个实现webs
k8s 部署 websocet-bench 测试 socket.io
简介
github项目地址
websocket bench是一款能够用来测试websocket服务性能的工具,暂时支持Socket.IO,Engine.IO,Faye,Primus,WAMP等框架的测试。
Socket.io作为一个实现websocket协议的nodejs框架,因为实现了服务器和浏览器的常连接,因此测试较为困难,本位采用websocket bench对其进行性能测试。
socket.io 有兴趣的可以参考官网文档 和 官方github 有不少的例子学习。
编译websocket-bench 镜像
FROM node:slim
RUN set -ex \
&& npm install -g websocket-bench \
&& sed -i "s#true});#true,'transports': ['websocket', 'polling']});#" /usr/local/lib/node_modules/websocket-bench/lib/workers/socketioworker.js \
&& /bin/bash -c "mkdir -p /app/pubilc"
WORKDIR /app/pubilc
docker build -t websocket-bench:latest .
根据情况编写generator
对于基准消息或更高级的连接,您应该提供自己的 generator
格式参考官网github
小示范,接收来自服务端发送的消息 net message 并打印出来
module.exports = {
/**
* Before connection (optional, just for faye)
* @param {client} client connection
*/
beforeConnect : function(client) {
// Example:
// client.setHeader('Authorization', 'OAuth abcd-1234');
// client.disable('websocket');
},
/**
* On client connection (required)
* @param {client} client connection
* @param {done} callback function(err) {}
*/
onConnect : function(client, done) {
// Faye client
// client.subscribe('/channel', function(message) { });
// Socket.io client
client.on("new message", function(data) {
console.log(data);
});
// Primus client
// client.write('Sailing the seas of cheese');
// WAMP session
// client.subscribe('com.myapp.hello').then(function(args) { });
done();
},
/**
* Send a message (required)
* @param {client} client connection
* @param {done} callback function(err) {}
*/
sendMessage : function(client, done) {
// Example:
// client.emit('test', { hello: 'world' });
// client.publish('/test', { hello: 'world' });
// client.call('com.myapp.add2', [2, 3]).then(function (res) { });
done();
},
/**
* WAMP connection options
*/
options : {
// realm: 'chat'
}
};
编写yaml
这里不使用docker 部署,使用k8s部署,方便后面扩展了;docker 部署可以在 run 的时候 添加执行命令去执行:
docker run -it --rm --name test -v $PWD/generator.js:/app/pubilc/generator.js websocket-bench:latest /bin/bash -c "websocket-bench -a 2500 -c 200 -g generator.js http://192.168.50.134:3000"
k8s-yaml
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: websocket-node
labels:
app: websocket-node
spec:
selector:
matchLabels:
app: websocket-node
replicas: 5
template:
metadata:
labels:
app: websocket-node
spec:
affinity:
#podAntiAffinity 反亲合性
podAntiAffinity:
#绝对不要把带有键为 app、值为 websocket-node 的标签 的多个 Pods 调度到相同的节点
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- websocket-node
topologyKey: "kubernetes.io/hostname"
containers:
- image: docker.goeasy.io/websocket-bench:latest
imagePullPolicy: Always
name: node
command: ["/bin/bash"]
args: ["-c","websocket-bench -a 20000 -c 1000 -kv -g generator.js http://192.168.50.134:3000"]
volumeMounts:
- name: generator
mountPath: /app/pubilc/generator.js
subPath: generator.js
imagePullSecrets:
- name: dockercfg-goeasy
volumes:
- name: generator
configMap:
name: generator-js
上方yaml 说明,启动5个副本 利用k8s pod 的反亲和性,使其每个pod 分布到不同的集群节点上,每一个节点一份副本,不重复,只存在一份。
参考官方的亲和性与反亲和性
websocket-bench 参数介绍:
参数简写 | 参数全称 | 含义 |
---|---|---|
-a | –amount | 持久连接总数,默认为 100 |
-c | –concurency | 每秒并发连接数,默认为 20 |
-w | –worker | 工人数量 |
-g | –generator | 用于生成消息或特殊事件的js文件 |
-m | –message | 客户端的消息数。 默认为 0 |
-o | –output | 输出文件 |
-t | –type | websocket 服务器的类型(socket.io、engine.io、faye、primus、wamp)。 默认为 socket.io |
-p | –transport | ebsocket 的传输类型(engine.io、websockets、browserchannel、sockjs、socket.io)。 默认为 websockets(仅适用于 Primus) |
-k | –keep-alive | 保持活动连接 |
-v | –verbose | 详细日志记录 |
generator.js转换为configmap.yaml
kubectl create configmap generator-js --from-file=generator.js --dry-run=client -o yaml > generator-configmap.yaml
启动测试
要先执行generator-configmap.yaml, websocket-bench.yaml 挂载依赖它
kubectl apply -f generator-configmap.yaml
kubectl apply -f websocket-bench.yaml
#查看logs
kubectl logs -f --tail=200
logs 输出
Launch bench with 20000 total connection, 1000 concurent connection
0 message(s) send by client
1 worker(s)
WS server : primus
file server : /app/pubilc/generator.js
trying : 1000 ...
trying : 2000 ...
trying : 3000 ...
trying : 4000 ...
trying : 5000 ...
trying : 6000 ...
trying : 7000 ...
trying : 8000 ...
trying : 9000 ...
trying : 10000 ...
trying : 11000 ...
trying : 12000 ...
trying : 13000 ...
trying : 14000 ...
trying : 15000 ...
trying : 16000 ...
trying : 17000 ...
trying : 18000 ...
trying : 19000 ...
trying : 20000 ...
#### steps report ####
┌────────┬─────────────┬────────┬──────────────┐
│ Number │ Connections │ Errors │ Duration(ms) │
├────────┼─────────────┼────────┼──────────────┤
│ 1000 │ 1000 │ 0 │ 4432 │
├────────┼─────────────┼────────┼──────────────┤
│ 2000 │ 1000 │ 0 │ 4176 │
├────────┼─────────────┼────────┼──────────────┤
│ 3000 │ 1000 │ 0 │ 3173 │
├────────┼─────────────┼────────┼──────────────┤
│ 4000 │ 1000 │ 0 │ 2859 │
├────────┼─────────────┼────────┼──────────────┤
│ 5000 │ 1000 │ 0 │ 1858 │
├────────┼─────────────┼────────┼──────────────┤
│ 6000 │ 1000 │ 0 │ 2405 │
├────────┼─────────────┼────────┼──────────────┤
│ 7000 │ 1000 │ 0 │ 1404 │
├────────┼─────────────┼────────┼──────────────┤
│ 8000 │ 1000 │ 0 │ 1471 │
├────────┼─────────────┼────────┼──────────────┤
│ 9000 │ 1000 │ 0 │ 1432 │
├────────┼─────────────┼────────┼──────────────┤
│ 10000 │ 1000 │ 0 │ 1448 │
├────────┼─────────────┼────────┼──────────────┤
│ 11000 │ 1000 │ 0 │ 1485 │
├────────┼─────────────┼────────┼──────────────┤
│ 12000 │ 1000 │ 0 │ 1466 │
├────────┼─────────────┼────────┼──────────────┤
│ 13000 │ 1000 │ 0 │ 1520 │
├────────┼─────────────┼────────┼──────────────┤
│ 14000 │ 1000 │ 0 │ 1435 │
├────────┼─────────────┼────────┼──────────────┤
│ 15000 │ 1000 │ 0 │ 1440 │
├────────┼─────────────┼────────┼──────────────┤
│ 16000 │ 1000 │ 0 │ 1435 │
├────────┼─────────────┼────────┼──────────────┤
│ 17000 │ 1000 │ 0 │ 1503 │
├────────┼─────────────┼────────┼──────────────┤
│ 18000 │ 1000 │ 0 │ 1528 │
├────────┼─────────────┼────────┼──────────────┤
│ 19000 │ 1000 │ 0 │ 1458 │
├────────┼─────────────┼────────┼──────────────┤
│ 20000 │ 1000 │ 0 │ 1099 │
└────────┴─────────────┴────────┴──────────────┘
#### total report ####
┌────────┬─────────────┬────────┬──────────────┬──────────────┬──────────────┐
│ Number │ Connections │ Errors │ Message Send │ Message Fail │ Duration(ms) │
├────────┼─────────────┼────────┼──────────────┼──────────────┼──────────────┤
│ 20000 │ 20000 │ 0 │ 0 │ 0 │ 20133 │
└────────┴─────────────┴────────┴──────────────┴──────────────┴──────────────┘
在工作中使用过的技术,记录学习。
更多推荐
所有评论(0)