websocket如何保持连接压力测试统计最后断开了几个链接
2021-08-01 本文已影响0人
啊哈_0042
记录一次webSocket的压力测试
-
需求:
测试WebSocket可以同时保持多少个连接30分钟不断开,如有断开并统计断开次数
-
思路
- 最开始想到的就是直接用jmeter进行压力测试
- 发现用jmeter达到不到效果
a. 需要15秒内发一次消息才能保持连接
b. 如果使用jmeter一直发送消息,一旦连接断开,就会不停的报错也不知道断开的次数
3.于是想到的用java写一个jmeter的插件
需要的依赖
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_core</artifactId>
<version>5.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_java</artifactId>
<version>5.4.1</version>
</dependency>
<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.3.8</version>
</dependency>
websocket连接
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URI;
public class WebsocketTools extends WebSocketClient {
private static final Logger logger = LoggerFactory.getLogger(WebsocketTools.class);
public WebsocketTools(URI serverUri , Draft draft ) {
super( serverUri, draft );
}
@Override
public void onOpen(ServerHandshake serverHandshake) {
System.out.println( "开流--opened connection" );
}
@Override
public void onMessage(String s) {
System.out.println( "接收--received: " + s );
}
@Override
public void onClose(int i, String s, boolean b) {
System.out.println("i="+i);
System.out.println("s="+s);
System.out.println( "关流--Connection closed by " + ( b ? "remote peer" : "us" ) );
}
@Override
public void onError(Exception e) {
e.printStackTrace();
}
public static String testweb(String url,int time){
try {
WebsocketTools c = new WebsocketTools( new URI( url ), new Draft_6455());
c.connect();
while(!c.getReadyState().equals(READYSTATE.OPEN)){
Thread.sleep(10);
}
System.out.println("连接打开了!");
Boolean result = false;
int count = 0;
while(!result) {
try {
Thread.sleep(5 * 1000);
int max=100000,min=1;
int ran2 = (int) (Math.random()*(max-min)+min);
c.send(String.valueOf(ran2));
count ++ ;
if (count == time) {
result = true;
break ;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(c.getReadyState().equals(READYSTATE.OPEN)){
System.out.println("连接成功的");
return "连接成功的";
}else {
System.out.println("连接失败的");
return "连接失败的";
}
} catch (Exception e) {
e.printStackTrace();
return "连接失败的";
}
}
}
调用webcoket,编写jmeter插件类
WebSocket类继承了JavaSamplerClient类。getDefaultParameters()定义了JMeter界面参数。setupTest()是测试初始化。runTest()是测试执行,连接websocket。teardownTest()是测试清理。setupTest()和teardownTest()在运行时每个线程只会执行一次。
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
public class WebSocket implements JavaSamplerClient {
public Arguments getDefaultParameters() {
Arguments params = new Arguments();
params.addArgument("url", "wss://controller-webrtc.zmaxis.com:8082/socket.io/?EIO=3&transport=websocket");
params.addArgument("time", "120");
return params;
}
@Override
public void setupTest(JavaSamplerContext javaSamplerContext) {
}
//测试方法
@Override
public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
SampleResult sr = new SampleResult();
String url = javaSamplerContext.getParameter("url");
String testweb = WebsocketTools.testweb(url,120);
sr.setResponseData(testweb,"utf-8");
sr.setSuccessful(true);
return sr;
}
@Override
public void teardownTest(JavaSamplerContext javaSamplerContext) {
}
public static void main(String[] args) {
WebsocketTools.testweb("wss://10.111.236.9:8080/socket.io/?EIO=3&transport=websocket",5);
}
}
打包
Java代码写好后,需要打成jar包提供给JMeter调用。打包过程如下:
点击右上角Project Structure:
image.png
双击添加到左侧
image.png
点击OK确认后,从菜单栏找到Build Artifacts点击:
image.png
image.png
jmeter的使用
首先需要把jar包复制到lib\ext目录下,webrtctest.jar位置如下:
image.png
还依赖2个jar包,在Settings\Build\Build Tools\Maven找到Local repository本地仓库目录后打开
commons-compress-1.20.jar存放位置如下:
image.png
Java-WebSocket-1.5.0.jar存放位置如下:
image.png
然后打开JMeter,添加线程组,添加Java请求,选择刚才创建的类,初始参数也加载出来了:
image.png
现在就可以愉快的开始压测了,开启多少个线程就是有多少个连接