Websockets and HTTP requests on k6 scripts

Hi I’m doing performance testing and I’m able to open the web-socket and suscribe to the channel successfully.
But I’m facing the following issue to simulate user journey I need to perform some actions (HTTP requests) while the web socket is opened and then close the web-socket with a HTTP post request.

Is it possible to open the web socket and keep it alive in the background, so the k6 script can move on with the following HTTP requests that requiere that connection opened?

Thanks in advance you help will be much appreciatted.

Hi @uherzel,
Moving on with an open websocket is not possible as … k6 doesn’t have an event loop.

But you can make http requests … inside the websocket mini event loop. The catch is that if a part of the in websocket event loop is being executed no other part can execute, which might be a problem for you, but you have to check :smiley:
A small example I adapted:

var http = require("k6/http");
var ws = require("k6/ws");
var check = require("k6").check;

exports.default = function () {
    var url = "wss://echo.websocket.org"
    var params = {};

    var res = ws.connect(url, params, function (socket) {
        socket.on('open', function open() {
            var res = http.get("http://test.k6.io");
            console.log(res.status);
            socket.setInterval(function timeout() {
                socket.send("test highload");
                socket.ping();
            }, 10);
        });

        socket.on('ping', function () {
        });

        socket.on('close', function() {
            console.log("close");
        })

        socket.setTimeout(function () {
            socket.close();
        }, 2000);
    });

    check(res, { "status is 101": function(r) {return r && r.status === 101 }});
}

Will get you:

INFO[0000] 200
INFO[0002] close
    done [==========================================================] 1 / 1

    ✓ status is 101

    checks.....................: 100.00% ✓ 1   ✗ 0
    data_received..............: 33 kB   13 kB/s
    data_sent..................: 22 kB   8.7 kB/s
    http_req_blocked...........: avg=184.17ms   min=184.17ms   med=184.17ms   max=184.17ms   p(90)=184.17ms   p(95)=184.17ms
    http_req_connecting........: avg=111.55ms   min=111.55ms   med=111.55ms   max=111.55ms   p(90)=111.55ms   p(95)=111.55ms
    http_req_duration..........: avg=113.06ms   min=113.06ms   med=113.06ms   max=113.06ms   p(90)=113.06ms   p(95)=113.06ms
    http_req_receiving.........: avg=261.97µs   min=261.97µs   med=261.97µs   max=261.97µs   p(90)=261.97µs   p(95)=261.97µs
    http_req_sending...........: avg=148.13µs   min=148.13µs   med=148.13µs   max=148.13µs   p(90)=148.13µs   p(95)=148.13µs
    http_req_tls_handshaking...: avg=0s         min=0s         med=0s         max=0s         p(90)=0s         p(95)=0s
    http_req_waiting...........: avg=112.65ms   min=112.65ms   med=112.65ms   max=112.65ms   p(90)=112.65ms   p(95)=112.65ms
    http_reqs..................: 1       0.397284/s
    iteration_duration.........: avg=2.51s      min=2.51s      med=2.51s      max=2.51s      p(90)=2.51s      p(95)=2.51s
    iterations.................: 1       0.397284/s
    vus........................: 1       min=1 max=1
    vus_max....................: 1       min=1 max=1
    ws_connecting..............: avg=516ms      min=516ms      med=516ms      max=516ms      p(90)=516ms      p(95)=516ms
    ws_msgs_received...........: 158     62.770919/s
    ws_msgs_sent...............: 170     67.538331/s
    ws_ping....................: avg=115.384028 min=111.638298 med=112.170248 max=174.721201 p(90)=117.069677 p(95)=137.421875
    ws_session_duration........: avg=2.51s      min=2.51s      med=2.51s      max=2.51s      p(90)=2.51s      p(95)=2.51s
    ws_sessions................: 1       0.397284/s

You can even open another websocket inside the first one, but again you will block the outer one event loop until the inner one finishes, which might be what you want, but unlikely :smiley: