JMeter 是目前最为流行的开源性能测试工具,JMeter 本身提供的基于插件的机制允许第三方实现标准 JMeter 所不支持的协议,而 WebSocket 的一个比较好的实现是 WebSocketSampler 。利用此插件,能完成基于 WebSocket 协议的基本性能测试。
下载地址:https://jmeter.apache.org/download_jmeter.cgi
Windows直接下载二进制文件即可
将下载的文件复制/移动到本地目录下。我这里是直接复制到
D
盘,D:\apache-jmeter-5.4.1
下载地址:https://bitbucket.org/pjtr/jmeter-websocket-samplers/downloads/
将下载的文件复制/移动到
D:\apache-jmeter-5.4.1\lib\ext
目录下
进入可执行目录
D:\apache-jmeter-5.4.1\bin
查找批处理文件jmeter.bat
先添加线程组Tinywan Thread Group
,再点击添加sampler,就可以看到websocket信息
以上截图表示插件安装成功。
这里按照之前编写教程填写相关参数即可
var ws = new WebSocket("ws://127.0.0.1:8783");
开源技术小栈测试计划.jmx
选择协议ws/wss
,输入ip
、端口
、路径
、请求参数
(没有参数可不填)
JMeter
let $_content = {
"event": "join",
"mode": 2,
"group_id": 100,
"from_user_id": "10086",
"from_username": "阿克苏",
"to_user_id": "10000",
"content": "加入会话",
};
console.log(JSON.stringify($_content))
JSON.stringify()
方法将一个 JavaScript 对象或值转换为 JSON 字符串。
解析以上转换为JSON字符串
{"event":"join","mode":2,"group_id":100,"from_user_id":"10086","from_username":"阿克苏","to_user_id":"10000","content":"加入会话"}
需要发送的请求内容
let content = {
"event": "speak",
"mode": 2,
"group_id": 100,
"from_user_id": "10086",
"from_username": "阿克苏",
"to_user_id": "10000",
"content": "WebSocket教程:消息持久化的实现与应用",
}
console.log(JSON.stringify(content))
JSON.stringify()
方法将一个 JavaScript 对象或值转换为 JSON 字符串。
解析以上转换为JSON字符串
{"event":"speak","mode":2,"group_id":100,"from_user_id":"10086","from_username":"阿克苏","to_user_id":"10000","content":"WebSocket教程:消息持久化的实现与应用"}
创建循环控制器
添加 WebSocket Sampler
请求内容
{"event":"ping","content":"ping heartbeat"}
上图就是执行结果的查看树,可以在右边的窗口中看到取样器结果、请求、响应数据,其中,请求是客户端向服务器发送的请求,响应数据是服务器接收请求后返回的结果,可以选择不同的结果查看方式,有json、html、xpath等等。
请求数据
响应数据
请求数据
响应数据
下载地址:https://github.com/Tinywan/webman-admin/blob/main/db/开源技术小栈测试计划.jmx
开源技术小栈测试计划.jmx
<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="5.0" jmeter="5.4.1">
<hashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="开源技术小栈测试计划" enabled="true">
<stringProp name="TestPlan.comments"></stringProp>
<boolProp name="TestPlan.functional_mode">false</boolProp>
<boolProp name="TestPlan.serialize_threadgroups">true</boolProp>
<elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
<collectionProp name="Arguments.arguments">
<elementProp name="username" elementType="Argument">
<stringProp name="Argument.name">username</stringProp>
<stringProp name="Argument.value">Tinywan</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
<elementProp name="password" elementType="Argument">
<stringProp name="Argument.name">password</stringProp>
<stringProp name="Argument.value">xxxx</stringProp>
<stringProp name="Argument.metadata">=</stringProp>
</elementProp>
</collectionProp>
</elementProp>
<stringProp name="TestPlan.user_define_classpath"></stringProp>
</TestPlan>
<hashTree>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Im Websocket 压测" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="循环控制器" enabled="true">
<boolProp name="LoopController.continue_forever">false</boolProp>
<stringProp name="LoopController.loops">500</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">1</stringProp>
<stringProp name="ThreadGroup.ramp_time">1</stringProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>
<stringProp name="ThreadGroup.duration"></stringProp>
<stringProp name="ThreadGroup.delay"></stringProp>
<boolProp name="ThreadGroup.same_user_on_next_iteration">true</boolProp>
</ThreadGroup>
<hashTree>
<CriticalSectionController guiclass="CriticalSectionControllerGui" testclass="CriticalSectionController" testname="临界部分控制器" enabled="false">
<stringProp name="CriticalSectionController.lockName">global_lock</stringProp>
</CriticalSectionController>
<hashTree/>
<eu.luminis.jmeter.wssampler.OpenWebSocketSampler guiclass="eu.luminis.jmeter.wssampler.OpenWebSocketSamplerGui" testclass="eu.luminis.jmeter.wssampler.OpenWebSocketSampler" testname="1.0 连接" enabled="true">
<boolProp name="TLS">false</boolProp>
<stringProp name="server">127.0.0.1</stringProp>
<stringProp name="port">8783</stringProp>
<stringProp name="path">/?sign=ca7a2df4c9850239ded1974f5abe8fc7&ws_timestamp=1636079196</stringProp>
<stringProp name="connectTimeout">20000</stringProp>
<stringProp name="readTimeout">6000</stringProp>
</eu.luminis.jmeter.wssampler.OpenWebSocketSampler>
<hashTree/>
<eu.luminis.jmeter.wssampler.RequestResponseWebSocketSampler guiclass="eu.luminis.jmeter.wssampler.RequestResponseWebSocketSamplerGui" testclass="eu.luminis.jmeter.wssampler.RequestResponseWebSocketSampler" testname="2.0 加入群聊" enabled="true">
<boolProp name="createNewConnection">false</boolProp>
<boolProp name="TLS">false</boolProp>
<stringProp name="server"></stringProp>
<stringProp name="port">80</stringProp>
<stringProp name="path"></stringProp>
<stringProp name="connectTimeout">20000</stringProp>
<boolProp name="binaryPayload">false</boolProp>
<stringProp name="requestData">{"event":"join","mode":2,"group_id":100,"from_user_id":"10086","from_username":"阿克苏","to_user_id":"10000","content":"加入会话"}</stringProp>
<stringProp name="readTimeout">6000</stringProp>
<boolProp name="loadDataFromFile">false</boolProp>
<stringProp name="dataFile"></stringProp>
</eu.luminis.jmeter.wssampler.RequestResponseWebSocketSampler>
<hashTree/>
<eu.luminis.jmeter.wssampler.RequestResponseWebSocketSampler guiclass="eu.luminis.jmeter.wssampler.RequestResponseWebSocketSamplerGui" testclass="eu.luminis.jmeter.wssampler.RequestResponseWebSocketSampler" testname="3.0 说话" enabled="true">
<boolProp name="createNewConnection">false</boolProp>
<boolProp name="TLS">false</boolProp>
<stringProp name="server"></stringProp>
<stringProp name="port">80</stringProp>
<stringProp name="path"></stringProp>
<stringProp name="connectTimeout">20000</stringProp>
<boolProp name="binaryPayload">false</boolProp>
<stringProp name="requestData">{"event":"speak","mode":2,"group_id":100,"from_user_id":"10086","from_username":"阿克苏","to_user_id":"10000","content":"WebSocket教程:消息持久化的实现与应用"}</stringProp>
<stringProp name="readTimeout">6000</stringProp>
<boolProp name="loadDataFromFile">false</boolProp>
<stringProp name="dataFile"></stringProp>
</eu.luminis.jmeter.wssampler.RequestResponseWebSocketSampler>
<hashTree/>
<ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="察看结果树" enabled="true">
<boolProp name="ResultCollector.error_logging">false</boolProp>
<objProp>
<name>saveConfig</name>
<value class="SampleSaveConfiguration">
<time>true</time>
<latency>true</latency>
<timestamp>true</timestamp>
<success>true</success>
<label>true</label>
<code>true</code>
<message>true</message>
<threadName>true</threadName>
<dataType>true</dataType>
<encoding>false</encoding>
<assertions>true</assertions>
<subresults>true</subresults>
<responseData>false</responseData>
<samplerData>false</samplerData>
<xml>false</xml>
<fieldNames>true</fieldNames>
<responseHeaders>false</responseHeaders>
<requestHeaders>false</requestHeaders>
<responseDataOnError>false</responseDataOnError>
<saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
<assertionsResultsToSave>0</assertionsResultsToSave>
<bytes>true</bytes>
<sentBytes>true</sentBytes>
<url>true</url>
<threadCounts>true</threadCounts>
<idleTime>true</idleTime>
<connectTime>true</connectTime>
</value>
</objProp>
<stringProp name="filename"></stringProp>
</ResultCollector>
<hashTree/>
<ResultCollector guiclass="SummaryReport" testclass="ResultCollector" testname="汇总报告" enabled="true">
<boolProp name="ResultCollector.error_logging">false</boolProp>
<objProp>
<name>saveConfig</name>
<value class="SampleSaveConfiguration">
<time>true</time>
<latency>true</latency>
<timestamp>true</timestamp>
<success>true</success>
<label>true</label>
<code>true</code>
<message>true</message>
<threadName>true</threadName>
<dataType>true</dataType>
<encoding>false</encoding>
<assertions>true</assertions>
<subresults>true</subresults>
<responseData>false</responseData>
<samplerData>false</samplerData>
<xml>false</xml>
<fieldNames>true</fieldNames>
<responseHeaders>false</responseHeaders>
<requestHeaders>false</requestHeaders>
<responseDataOnError>false</responseDataOnError>
<saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
<assertionsResultsToSave>0</assertionsResultsToSave>
<bytes>true</bytes>
<sentBytes>true</sentBytes>
<url>true</url>
<threadCounts>true</threadCounts>
<idleTime>true</idleTime>
<connectTime>true</connectTime>
</value>
</objProp>
<stringProp name="filename"></stringProp>
</ResultCollector>
<hashTree/>
<ResultCollector guiclass="StatVisualizer" testclass="ResultCollector" testname="聚合报告" enabled="true">
<boolProp name="ResultCollector.error_logging">false</boolProp>
<objProp>
<name>saveConfig</name>
<value class="SampleSaveConfiguration">
<time>true</time>
<latency>true</latency>
<timestamp>true</timestamp>
<success>true</success>
<label>true</label>
<code>true</code>
<message>true</message>
<threadName>true</threadName>
<dataType>true</dataType>
<encoding>false</encoding>
<assertions>true</assertions>
<subresults>true</subresults>
<responseData>false</responseData>
<samplerData>false</samplerData>
<xml>false</xml>
<fieldNames>true</fieldNames>
<responseHeaders>false</responseHeaders>
<requestHeaders>false</requestHeaders>
<responseDataOnError>false</responseDataOnError>
<saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
<assertionsResultsToSave>0</assertionsResultsToSave>
<bytes>true</bytes>
<sentBytes>true</sentBytes>
<url>true</url>
<threadCounts>true</threadCounts>
<idleTime>true</idleTime>
<connectTime>true</connectTime>
</value>
</objProp>
<stringProp name="filename"></stringProp>
</ResultCollector>
<hashTree/>
<LoopController guiclass="LoopControlPanel" testclass="LoopController" testname="循环控制器" enabled="true">
<boolProp name="LoopController.continue_forever">true</boolProp>
<stringProp name="LoopController.loops">500</stringProp>
</LoopController>
<hashTree>
<ConstantTimer guiclass="ConstantTimerGui" testclass="ConstantTimer" testname="固定定时器" enabled="true">
<stringProp name="ConstantTimer.delay">10000</stringProp>
</ConstantTimer>
<hashTree/>
<eu.luminis.jmeter.wssampler.RequestResponseWebSocketSampler guiclass="eu.luminis.jmeter.wssampler.RequestResponseWebSocketSamplerGui" testclass="eu.luminis.jmeter.wssampler.RequestResponseWebSocketSampler" testname="ws-心跳检测" enabled="true">
<boolProp name="createNewConnection">false</boolProp>
<boolProp name="TLS">false</boolProp>
<stringProp name="server"></stringProp>
<stringProp name="port">80</stringProp>
<stringProp name="path"></stringProp>
<stringProp name="connectTimeout">20000</stringProp>
<boolProp name="binaryPayload">false</boolProp>
<stringProp name="requestData">{"event":"ping","content":"ping heartbeat"}</stringProp>
<stringProp name="readTimeout">6000</stringProp>
<boolProp name="loadDataFromFile">false</boolProp>
<stringProp name="dataFile"></stringProp>
</eu.luminis.jmeter.wssampler.RequestResponseWebSocketSampler>
<hashTree/>
</hashTree>
</hashTree>
</hashTree>
</hashTree>
</jmeterTestPlan>
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有