Have anyone used k6 with socket.io library ?
I have nodejs server using socket.io, so when I using following code to test my websoket:
import ws from "k6/ws";
import { check } from "k6";
export default function() {
var url = "wss://localhost/socket.io";
var res = ws.connect(url, function(socket) {
socket.on('open', function() {
console.log('connected');
});
socket.on('close', function() {
console.log('disconnected');
});
});
check(res, { "status is 101": (r) => r && r.status === 101 });
}
It output GoError: websocket: bad handshake because my node server not using normal websocket, it using https://socket.io/
Then I try include https://www.npmjs.com/package/socket.io-client intoscript.js
import socketio from './socket.io.js';
...
socketio.io("wss://localhost/socket.io");
It output error TypeError: Cannot read property 'Buffer' of undefined at /vagrant/shared/socket.io.js:6:8072(15). It seem k6 javascript compile not have global variable.
Can anyone help me ! Thank you.
You cannot directly run Node modules in k6 because they often depend on parts of the NodeJS standard library. In this case, it seems like socket.io needs Buffer.
What you can sometimes do to use some of those modules in k6 is to pass them through browserify or use their pre-built versions meant for browsers. Take a look at the k6 documentation that describes this. I'm not sure if the socket.io client would work that way, but since it heavily depends on network access, there's a good chance that it won't, sorry.
I'm not very familiar with socket.io, but maybe the native k6 websockets would be sufficient? If not, you may have to wait until k6 has better compatibility with JS libraries or until we have native support for it.
@na-- Thank you, I will try it tommorow :(
Finally, I decided to use Artillery for testing with Socket.io 馃槀
Most helpful comment
You cannot directly run Node modules in k6 because they often depend on parts of the NodeJS standard library. In this case, it seems like socket.io needs
Buffer.What you can sometimes do to use some of those modules in k6 is to pass them through browserify or use their pre-built versions meant for browsers. Take a look at the k6 documentation that describes this. I'm not sure if the socket.io client would work that way, but since it heavily depends on network access, there's a good chance that it won't, sorry.
I'm not very familiar with socket.io, but maybe the native k6 websockets would be sufficient? If not, you may have to wait until k6 has better compatibility with JS libraries or until we have native support for it.