K6 each VU with own test data

29 Views Asked by At

I have common scenario like each VU uses its own data, in my case each VU needs to have own token to make the same HTTP request.

Tokens.json

{
    "dev": [
      { "token": "test33333"},
      { "token": "test2444"},
      { "token": "test3333332211"},
      { "token": "test4"},
      { "token": "test5"},
      { "token": "test6"}
    ]
  }

K6 test:

export const options: Options = {
  stages: [
    { duration: '10s', target: 5 }, // traffic ramp-up from 1 to 2 users over 1 minute
    { duration: '1m', target: 5 }, // stay at 5 users for 18 minutes
    { duration: '1m', target: 0 }, // ramp-down to 0 users
  ],
};

const url: string = constants.api_node_requests + "?limit=1000&offset=0";
console.log(`URL: ${url}`)

export default () => {
  console.log(`${new Date()}: URL: ${url}`);
  let response = http.get(url, {
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'Bearer ' + `${tokens[0].token}`,
      'Accept': '*/*',
      'Cache-Control': 'no-cache',
      'Connection': 'keep-alive'
    },
  }
  );

Is there a built-in scenario parameter to implement it with a ramp load? I would like each VU grab next available token from json and make a http call

Thanks in advance for any hints

*** RESOLVED ***

const data = new SharedArray('Bearer tokens', function() {
  const obj = JSON.parse(open('./tokens.json'))
  return [obj]
})

const tokens = data[0].dev.tokens

export const options: Options = {
  stages: [
    { duration: '1m', target: 2 }, // traffic ramp-up from 1 to 2 users over 1 minute
    { duration: '1m', target: Math.round(tokens.length / 4) }, // stay at 25% of max number of users
    { duration: '1m', target: tokens.length }, // stay at 25% of max number of users
    { duration: '1m', target: 0 }, // ramp-down to 0 users
  ],
};

const url: string = constants.api_node_requests + "?limit=1000&offset=0";
export default () => {
  var id_vu = exec.vu.idInTest

  console.log(`Token is ${tokens[id_vu - 1]} for ${id_vu}`)
  console.log(`${new Date()}: URL: ${url}`);
  let response = http.get(url, {
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'Bearer ' + `${tokens[id_vu - 1]}`,
      'Accept': '*/*',
      'Cache-Control': 'no-cache',
      'Connection': 'keep-alive'
    },
  }
  );  
0

There are 0 best solutions below