Share a unique value between functions

Hi Team,

I need to pass a unique value generated within one function that should be used by another function in the latter part of the execution. If I initiate the variable on the init section of the code and run a single user test works fine but if I run multiple VUs then the same unique value is used by all the users.

How to handle it.

Hi @Gerard you can not share a variable between functions. You can use setup() function for this. For example look at the below code;

const totalVu = 10;

export const options = {
  scenarios: {
    Demo: {
      exec: "Demo",
      executor: "per-vu-iterations",
      vus: totalVu,
      iterations: 10,

    },

  },

};
export function setup() {
  let test1= http.post(
    Load.api + endpoints.SalesOrderLoadSeed.CreateSLAMCompletedSalesOrders,
    JSON.stringify({
      tenant: "test",
    }),
    {
      headers: {
        apikey: "test",
      }
    }
  );

  let test2= Load.auth(Roles.Support);
  return [
    test1.json(),
    test2.json()
  ];
}

You can return the values .After that you can access below code;

export function Demo(data) {
 let token = data[0].access_token        //data[0] calls  test1.json() response body
 let x=`${data[1][(totalVu * (exec.vu.iterationInInstance)) + (exec.vu.idInTest - 1)]}`  //data[1] calls  test2.json() response body                               

}

let x=${data[1][(totalVu * (exec.vu.iterationInInstance)) + (exec.vu.idInTest - 1)]} this method starts 0 and continue until totalVu * iterations =100 like 0,1,2,3…99 .And takes unique index if you using parallel requests
Hopes help you.Regards

1 Like