问题
在使用websocket-bench压测一个长连接服务,执行脚本g.js的内容是:
module.exports = {
//建立连接后所要做的事情
onConnect: function(client, done){
//登录
client.write({"action":"login","cid":1,"session":"123456"});
done();
},
//向服务器民送消息时运行的代码
sendMessage: function(client, done) {
//说话
client.write({"action":say","content":"hhhhhhhh"});
done();
}
};
执行压测
websocket-bench -a 10 -c 1 -w 8 -m 8 -t primus -p websockets ws://127.0.0.1 -g g.js -o result
结果发现,在长连接建立之后发送了登录信息之后,马上发送了说话的信息,而说话的功能是依赖用户登录态的,这个时候登录动作还没有处理完成,导致sendMessage发送的消息不能像预期一样处理,请问有没有解决的方法。(曾想过在onConnect中实现sleep,但是javascript这种单线程模式会导致整个压测工具执行变慢,得不到好的压测结果)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…