Websocket question

I’m getting “websocket: Too many open files” error.
I tried to count that error by using a custom metric ‘Counter’. K6 did not generate the counter in it’s report. Below is my code.
import ws from ‘k6/ws’;

import { check } from ‘k6’;

import { Rate } from ‘k6/metrics’;

import { Counter } from ‘k6/metrics’;

// A custom metric to track failure rates

var failureRate = new Rate(“check_failure_rate”);

var errorCounter = new Counter(“error_rate”);

// Options

export let options = {

//stages for spike test

stages: [



  { duration: '30s', target: 1000 }, // below normal load

  { duration: '1m', target: 1000 },

  { duration: '30s', target: 5000 }, // spike to 5000 users

  { duration: '3m', target: 5000 }, // stay at 5000 for 3 minutes

  { duration: '30s', target: 1000 }, // scale down. Recovery stage.

  { duration: '3m', target: 1000 },

  { duration: '30s', target: 0 },



],



  noVUConnectionReuse: false, //specifying whether k6 should reuse TCP connections between iterations of a VU

  rps: 50, //The maximum number of requests to make per second, in total across all VUs

  summaryTimeUnit: 'ms', //Define which time unit will be used for all time values in the end-of-test summary. Possible values are s (seconds), ms (milliseconds) and us (microseconds). If no value is specified, k6 will use mixed time units, choosing the most appropriate unit for each value.

  discardResponseBodies: true,

};

export default function () {

var url = 'wss://sauat.bajajfinserv.in/wsc/recommendedbanneroffersuat'; //API endpoint



//code for generation of sessionID

// var wsSessionID = Math.random().toString(36).substr(2, 11) + Math.random().toString(36).substr(2, 11);

var wsSessionID = "16258244763960kflpyDwnout4OZmoDEPh9cgD4jfp2"; //sessionID generated from Jmeter

var operation = 'recommendedbanneroffersuat'; //operation end point

var requestBody = "B53cH7c5SsQAF1Pb6LzajkMd0d34doNBivpnWqDQHSaWIdgUcUAz9qDplIRANVzs8TZHTsMJ9HDSNxoIjKx5AwFMqfVFGlgfjDhvxx8x2fY="; //encrypted request body



let params = {

  "headers":{

    "Authorization":"Basic clpVU3kwSFNKZlA5TUtrOl9jUGtLb3VwLXMtZGlSQlZHN1ZKZ040Vmh1QQ==",

    "token":"202107090831081625819468392sdYY0OOknUOR6A5TbkYVHv0r8ZAar9",

      // "wsSessionID":"${wsSessionID}",

      "wsSessionID": wsSessionID,

      "operation": operation,

      "source":"local",

      "requestCounter":"1"

    },

  "requestBody":requestBody

}

// console.log(JSON.stringify(params));



var response = ws.connect(url, params, function (socket) {

  socket.on('open', function open() {

    console.log('connected');

    socket.send(Date.now());



    // socket.setInterval(function timeout() {

    //   socket.ping();

    //   console.log('Pinging every 1sec (setInterval test)');

    // }, 1000);

  });



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

    console.log('PING!');

  });



  socket.on('pong', function () {

    console.log('PONG!');

  });



  socket.on('pong', function () {

    // Multiple event handlers on the same event

    console.log('OTHER PONG!');

  });



  socket.on('message', function (message) {

    // let success = check(message, { 

    //   ['status is 200']: (r) => r.statusCode == 200 });

    //   let checkRes = (message, { 

    //     ['status is ' + message.statusCode]: (r) => r.statusCode != 200 }); 

    console.log(`Received message: ${message}`);

    // failureRate.add(checkRes);

  });



  socket.on('close', function () {

    console.log('disconnected');

  });



  socket.on('error', function (e) {

    if (e.error() != 'websocket: close sent') {

      errorCounter.add(1);

      console.log('An unexpected error occured: ', e.error());

    }

  });

    

  //The timeout will close down the WebSocket connection after 10 seconds.

  socket.setTimeout(function () {

    console.log('10 seconds passed, closing the socket');

    socket.close();

  }, 10000);

});

}

// console.log("response -> " + response);

// // To display the response code

 let checkRes = check(response, { 

   ['status is 101']: (r) => r.status === 101 },

   { ["Close Code is 1000"]: (r) => r.status == 1000 } );

 failureRate.add(!checkRes);

Can you please let me know what is going wrong?

@neha13rb, I moved your question to a new topic. Please don’t ask unrelated questions in old threads…

And to answer your question, see this article in our documentation on how you can increase your OS limits for the number of currently open files: https://k6.io/docs/misc/fine-tuning-os/