JMeterWebSocketSampler-1.0.2-SNAPSHOT.jar下载
公司使用websocket比较奇怪,需要带认证信息进行长连接,通过websocket插件是请求失败,如下图,后面通过代码实现随再打包jar包完成websocket测试
本地实现代码如下:
java
package com.fangdd.socket;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
import java.net.URI;
import java.net.URISyntaxException;
import java.io.IOException;
public class SocketIOExample {
public static void main(String[] args) throws URISyntaxException, IOException {
IO.Options options = new IO.Options();
options.query="tid=wwwww1231&uid=12352547&ticket=mxLqAgCaq7R";
Socket socket = IO.socket(URI.create("http://localhost:9092"), options);
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("connect");
// 如果需要发消息参考此代码 连接成功后发送具有body的自定义事件,公司暂不需要注释此代码
// JSONObject body = new JSONObject();
// try {
// body.put("message", "Hello, Server!");
// } catch (JSONException e) {
// e.printStackTrace();
// }
//
// // 发送事件到服务器--需要与开发确认事件(customEvent)
// socket.emit("customEvent", body);
}
});
socket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("connect_error: " + args[0]);
}
});
socket.on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("disconnect due to: " + args[0]);
}
});
socket.connect();
// 监听服务器返回的自定义事件----需要与开发确认监听事件(messageEvent)
socket.on("messageEvent", new Emitter.Listener() {
@Override
public void call(Object... args) {
// 处理服务器返回的数据,如果是json需要数据处理
//JSONObject response = (JSONObject) args[0];
System.out.println("Server response: " + args[0].toString());
}
});
}
}
显示如下:
开发代码打包到lib/ext目录,通过Jmeter对本地方法进行测试,具体实现代码如下:
底层实现代码:
java
import java.net.URI;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
public class NomalTools {
public static void IOsocket(String url,String tid) {
IO.Options options = new IO.Options();
options.query="tid="+tid+"&uid=12352547&ticket=mxLqA";
Socket socket = IO.socket(URI.create(url), options);
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("connect");
}
});
socket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("connect_error: " + args[0]);
}
});
socket.on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("disconnect due to: " + args[0]);
}
});
socket.connect();
// 监听服务器返回的自定义事件
socket.on("messageEvent", new Emitter.Listener() {
@Override
public void call(Object... args) {
// 处理服务器返回的数据
// JSONObject response = (JSONObject) args[0];
System.out.println("Server response: " + args[0].toString());
}
});
//10秒后自动关闭连接
// try {
// Thread.sleep(10000);
// System.out.println("10秒自动关闭连接");
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// socket.close();
}
}
Jmeter封装代码:
java
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import Common.NomalTools;
public class socketTest extends AbstractJavaSamplerClient{
private NomalTools nomalTools;
private String url;
private String tid;
private static long start = 0;
private static long end = 0;
public Arguments getDefaultParameters() {
Arguments params = new Arguments();
params.addArgument("url", "");
params.addArgument("tid", "");
return params;
}
//每个线程测试前执行一次,做一些初始化工作;
public void setupTest(JavaSamplerContext arg0) {
nomalTools=new NomalTools();
url = arg0.getParameter("url");
tid = arg0.getParameter("tid");
start = System.currentTimeMillis();
}
public SampleResult runTest(JavaSamplerContext arg0) {
SampleResult sr = new SampleResult();
sr.setSamplerData("请求参数num1:"+url+"\n请求参数num2:"+tid);
try {
sr.sampleStart();// jmeter 开始统计响应时间标记
// 通过下面的操作就可以将被测方法的响应输出到Jmeter的察看结果树中的响应数据里面了。
nomalTools.IOsocket(url, tid);
// System.out.println(resultData);
sr.setSuccessful(true);
} catch (Throwable e) {
sr.setSuccessful(false);
e.printStackTrace();
} finally {
sr.sampleEnd();// jmeter 结束统计响应时间标记
}
return sr;
}
//测试结束时调用;
public void teardownTest(JavaSamplerContext arg0) {
end = System.currentTimeMillis();
// 总体耗时
System.err.println("cost time:" + (end - start) + "毫秒");
}
public static void main(String[] args) {
Arguments params = new Arguments();
params.addArgument("url", "http://localhost:9092");//设置参数,并赋予默认值1
params.addArgument("tid", "qwe123");//设置参数,并赋予默认值2
JavaSamplerContext arg0 = new JavaSamplerContext(params);
socketTest test = new socketTest();
test.setupTest(arg0);
test.runTest(arg0);
test.teardownTest(arg0);
}
}
集成到Jmeter的效果:
参考文献:
【JMeter】Jmeter进行webSocket接口测试_千重萌的博客-CSDN博客
https://github.com/socketio/socket.io-client-java/blob/main/src/test/java/io/socket/Fiddle.java