Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

K6 trigger ASP.NET Core server function

I have an ASP.NET Core Web API project that is using SignalR, I have a hub there which I am connecting to it using k6 (I want to do some load testings) I manage to connect to my hub but I can not figure out how to call a function from my server, my code is

import ws from 'k6/ws';
import { check } from 'k6';

export default function () {
    var token = "Bearer userAccessToken";
    const url = 'wss://localhost:5001/session';
    const params = { headers: { "Authorization": token } };

    const res = ws.connect(url, params, function (socket) {
        socket.on('open', () => {
            console.log("opened");
            socket.send(JSON.stringify({ UserId: "aUserId", GameId: "AGameId" }))
        });
        socket.on('close', () => console.log('disconnected'));
    });

    check(res, { 'status is 101': (r) => r && r.status === 101 });
}

My function is called joinGameSession and it takes two variables the user id and the gameId

public async Task<bool> JoinGameSession(JoinGameRequest request)
{
    return true;
}

I have managed to trigger functions using Microsoft's SignalR client.

const signalR = require("@microsoft/signalr");
require('dotenv').config();

var token = process.env.token ?? "";

var questionIndex = 0;
let connection = new signalR.HubConnectionBuilder()
    .withUrl("http://localhost:5000/session", { headers: { "Authorization": token } })
    .withAutomaticReconnect()
    .build();

connection.start().then(() => {
    connection.invoke("JoinGameSession", { UserId: "a", GameId: "x" });
}).catch(e => {
    console.log(e);
})

but I can not do it with k6, is there any other tools to achieve my goal?

Thank you.

like image 258
MoTahir Avatar asked Jun 01 '26 20:06

MoTahir


1 Answers

I had a similar issue and found an old thread which managed to solve it.

  ws.connect(wsUrl, function (socket) {
    socket.on('open', () => {
      socket.send('{"protocol":"json","version":1}\x1e') // add this
    });
    socket.on('message', () => console.log('message')); 
    socket.on('close', () => console.log('closed')); 
  });

This should allow you to connect to your SignalR hub performing the appropriate handshake. In SignalR, the client and server communicate using a custom protocol. The message being sent in this case is a handshake message that informs the server about the protocol and version the client supports.

like image 59
Codeheir Avatar answered Jun 04 '26 10:06

Codeheir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!