Multiple scenarios failing on API calls

Hi all, Can someone help? When I run scenarios separately everything looks good. If I run it as multiple scenarios(loadTest.js) I got APIs response status codes 403 or 500 instead of 200

// loadTest.js
import { scenario1 } from './scenario1.js';
import { scenario2 } from './scenario2.js';

export let options = {
  thresholds: {
    error_rate: ['rate < 0.1'],
  },
  scenarios: {
    scenario1: {
      executor: 'ramping-vus',
      startVUs: 0,
      stages: [
        { duration: '30s', target: 5 },
      ],
      gracefulRampDown: '10s', 
    },
    scenario2: { 
      executor: 'ramping-vus',
      startVUs: 0,
      stages: [
        { duration: '30s', target: 5 }, 
      ],
      gracefulRampDown: '10s', 
    },
  }
}

export default function() {
  scenario1() 
  scenario2()
}
// scenario1.js

import { sleep, group, check } from 'k6'
import { parseHTML } from "k6/html";
import { fail } from "k6";
import http from 'k6/http'

import {
  serverUrl, 
  errorRate, 
  scenario1user,
} from "./config.js";

import  { 
  getRandomNumberBetween, 
  getCsrfTokenFromResponse, 
  loginWeb, 
  getPatientIDAfterLogin, 
  getCookieFromResponse,
}  from "./tools.js";

export function scenario1() { 
  var response;
  var token ="";
  var token1 ="";

  var masterUrl = serverUrl;
  var userName = scenario1user;
  var password = "abc123";
  var patientID = "";

  var cookie = "";
  var status = true;

  group("scenario1", function () {	
    response = http.get(masterUrl+"/users/signin");
    token = getCsrfTokenFromResponse(response);
    response =	loginWeb(token, userName, password, masterUrl);
    var result = check(response, {'login': res => res.status === 200});			
    errorRate.add(result ? 0 : 1);	
    //+ other APIs
  });
  sleep(1)
}
// scenario2.js
import { sleep, group, check } from 'k6'
import { parseHTML } from "k6/html";
import { fail } from "k6";
import http from 'k6/http'

import {
  serverUrl, 
  errorRate, 
  scenario2user,
} from "./config.js";

import  { 
  getRandomNumberBetween, 
  getCsrfTokenFromResponse,
  loginWeb, 
  getPatientIDAfterLogin, 
  getCookieFromResponse,
}  from "./tools.js";


export function scenario2() { 
  var response;
  var token ="";
  var token1 ="";

  var masterUrl = serverUrl;
  var userName = scenario2user;
  var password = "abc123";
  var patientID = "";

  var cookie = "";
  var status = true;

  group("scenario2", function () {	
    response = http.get(masterUrl+"/users/signin");
    token = getCsrfTokenFromResponse(response);
    response = loginWeb(token, userName, password, masterUrl);
    var result = check(response, {'login': res => res.status === 200});			
    errorRate.add(result ? 0 : 1);
		
    //+ other APIs
  });
  sleep(1)
}
1 Like

I found the problem… scenarios share data…how I can avoid that? For example. If I log in as an admin user in one scenario, And if I try to login as a user in another scenario I will get admin user data as a response from the user API log in

resolved

added in loadtest.js:

  scenarios: {
    scenario1: {
      exec: 'scenario1A', // add this
      executor: 'ramping-vus',
      startVUs: 0,
      stages: [
        { duration: '30s', target: 5 },
      ],
      gracefulRampDown: '10s',  
    },
   scenario2: { 
     exec: 'scenario2A', // add this
      executor: 'ramping-vus',
      startVUs: 0,
      stages: [
        { duration: '30s', target: 5 }, 
      ],
      gracefulRampDown: '10s',
    },
  }

and add this

export function scenario1A() {
 scenario1();
}
export function scenario2A() {
 scenario2();
}

instead of

export default function() {
  scenario1() 
  scenario2()
}
1 Like

Hi @cinco107, Sorry for the late reply.

But yes as you’ve noticed just naming stuff scenario doesn’t make them one :). In reality, your code was executing the same default functions in two different executors instead of executing scenario1 in one of them and scenario2 in the other.

I hope you now understand this, but if you can provide any advice on what wasn’t clear or if anything else seems unclear to you, we can update the documentation accordingly.