You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JMeter中编写Socket.IO测试?附服务端代码示例

Socket.IO服务端的JMeter WebSocket测试配置指南

参考服务端代码

const app = express();

app.use(express.static('public'));

app.use((req, res, next) => {
  res.setHeader('Access-Control-Allow-Origin', '*');
  next();
});

const server = http.createServer(app);
const io = new Server(server);
const port = 3000;

app.get('/help', (req, res) => {
  res.send('HELP PAGE')
})

io.on('connection', (socket) => {
  console.log(socket.id);
  console.log('User connected');

  socket.on('message', (msg) => {
    console.log('Message: ' + msg);
    io.emit('message', msg + socket.id);
  });

  socket.on('disconnect', () => {
    console.log('User disconnected');
  });
});

server.listen(port, () => {
  console.log(`Server Socket.IO listen on: ${port}`);
});

JMeter测试配置步骤(基于Peter的WebSocket插件)

1. 搭建基础测试结构

  • 新建测试计划
  • 添加线程组(调试阶段可设1线程、1循环,压测时按需调整参数)

2. 配置Socket.IO连接参数

  • 右键线程组 → 添加 → 配置元件 → WebSocket Connection Configuration
    • Server Name or IP:填写localhost
    • Port Number:填写3000
    • Path:填写/socket.io/?EIO=4&transport=websocket(Socket.IO的WebSocket连接必须带此协议参数,EIO版本号需匹配你的Socket.IO版本,v4.x系列用4即可)

3. 添加连接建立请求

  • 右键线程组 → 添加 → Sampler → WebSocket Open Connection
    • 无需额外配置,自动复用上述连接参数

4. 添加message事件发送请求

  • 右键线程组 → 添加 → Sampler → WebSocket Single Write Sampler
    • 选择对应Connection配置
    • Text/Message栏填写:42["message","test_demo_msg"]
      • 格式说明:Socket.IO事件消息遵循专属协议,42是事件类型标识码,数组第一个元素为事件名"message",第二个为发送的消息内容

5. 添加响应断言(验证服务端返回)

  • 右键WebSocket Single Write Sampler → 添加 → 断言 → Response Assertion
    • 勾选Response Field to Test中的Text Response
    • Patterns to Test栏添加:42\["message","test_demo_msg.*"\]
      • 说明:服务端返回内容为消息内容+socket.id,用.*匹配任意socket.id后缀

6. 添加结果查看元件

  • 右键线程组 → 添加 → 监听器 → 查看结果树
  • 运行测试后,可在该元件中查看请求、响应详情及断言结果

关键注意事项

  • Socket.IO不是标准WebSocket,必须使用其专属协议格式发送消息,不能直接传纯文本
  • 若Socket.IO版本不同,需调整EIO参数(比如v3.x版本用EIO=3)
  • 批量测试时,可通过线程组参数或CSV数据配置元件实现多消息内容的批量验证

内容的提问来源于stack exchange,提问作者The Trainer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 05:42:42