load-testingk6

How to execute single scenario out of multiple scenarios in a script in k6?


I have a k6 script with multiple scenarios configured, but I want to run only one at a time. How can I do this?

import http from "k6/http";

export const options = {
  scenarios: {
    shared_iter_scenario: {
      executor: "shared-iterations",
      vus: 10,
      iterations: 100,
      startTime: "0s",
    },
    per_vu_scenario: {
      executor: "per-vu-iterations",
      vus: 10,
      iterations: 10,
      startTime: "10s",
    },
  },
};

export default function () {
  http.get("https://test.k6.io/");
}

Solution

  • Inspired by this discussion, I have configured a way to execute only one scenario in k6 at a time.

    const scenarios = {
        shared_iter_scenario: { ... },
        per_vu_scenario: { ... }
    };
    
    const { SCENARIO } = __ENV;
    export const options = {
      scenarios: SCENARIO ? {
        [SCENARIO]: scenarios[SCENARIO]
      } : {},
    };
    

    After that, you can run your script by passing the environment variable

    k6 run --env SCENARIO=per_vu_scenario test.js 
    

    If you don't provide any scenario value then by it execute the default scenario. You can also run multiple specific scenarios or modify the behaviour as you like following this pattern.