如何在JMeter中编写socket.io测试?

huangapple go评论68阅读模式
英文:

How to write socket.io tests in JMeter?

问题

以下是您要翻译的代码部分:

这是我用socket.io库编写的简单服务器

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。我应该如何为message方法编写测试?

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

有人可以发布他自己的测试吗?我已经在Postman中有工作的测试,但这些是针对socket.io的特殊请求类型 - 不幸的是,Postman不如JMeter那么可扩展,我不能使用它。

英文:

There is my simple server written with socket.io library:

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}`);
});

I need to write test in JMeter. I use plugin from Peter for testing WebSockets. How should I write test for method message?

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

Could someone post his own test? I have already working tests in Postman, but there are special type of request for socket.io - unfortunately Postman is not such scalable as JMeter and I can't use it.

答案1

得分: 1

使用类似Wireshark的嗅探工具捕获Postman发送的所有请求,并配置JMeter以发送相同的请求。

  1. 添加一个HTTP请求采样器以实例化初始的HTTP连接并获取sid

  2. 添加一个Boundary Extractor以从响应中提取sid

  3. 打开一个WebSocket连接

  4. 对这个连接执行需要的操作

英文:

Capture all the requests which are being sent by Postman using a sniffer tool like Wireshark and configure JMeter to send the same requests.

  1. Add a HTTP Request sampler to instantiate the initial HTTP connection and get sid

    如何在JMeter中编写socket.io测试?

  2. Add a Boundary Extractor to extract the sid from the response

    如何在JMeter中编写socket.io测试?

  3. Open a websocket connection

    如何在JMeter中编写socket.io测试?

  4. Do what you need with this connection

    如何在JMeter中编写socket.io测试?

huangapple
  • 本文由 发表于 2023年7月11日 14:06:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76659091.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定