Confused about multiple scenarios

I don’t understand how multiple-scenarios.js or multiple-scenarios-complex.js work from the documentation at: https://k6.io/docs/using-k6/scenarios

Neither has a default export so i don’t understand how it could possibly work.

I don’t know why a default export is even needed if multiple scenarios are defined in the options – why wouldn’t k6 just run all of the scenarios listed?
Or is there no way to actually run multiple scenarios with a single “run” command?

Hi @mda, welcome to the forum :slight_smile:

In those examples, notice that all scenarios specify an exec option, which is the name of the function each scenario should execute. This is why the script doesn’t export a default function, since no scenarios would use it. If the scenario doesn’t specify an exec function, then the default function will be executed and must be exported.

So this is a way to distinguish different workflows in your tests. One scenario could test the API login, another could be a functional test, etc., which would be more cumbersome to handle if all scenarios executed the same function. More than one scenario could also execute the same function, so this gives you a lot of flexibility with how to structure your tests.

is there no way to actually run multiple scenarios with a single “run” command?

Sure, all scenarios defined in your script will be run with a single run command, that’s the whole purpose of the feature. :slight_smile:

Hope this clears up the confusion. Let us know how we could improve the documentation to hopefully avoid this confusion for others, as we know scenarios are a complex feature. Or feel free to create a PR with your suggestions, the “Suggest edits” link on each page will take you to the source document.

Thanks @imiric … one thing that confuses me is this output from k6:

$ k6 run --duration 10s /tmp/multiple_scenarios.js
          /\      |‾‾| /‾‾/   /‾‾/  
     /\  /  \     |  |/  /   /  /    
    /  \/    \    |     (   /   ‾‾\  
   /          \   |  |\  \ |  (‾)  | 
  / __________ \  |__| \__\ \_____/ .io

ERRO[0000] There were problems with the specified script configuration:
    - executor default: function 'default' not found in exports 

I found this information in the blog (but not the main documentation):

Beware that the scenarios API deprecates global usages of  `duration` ,  `vus`  and  `stages` , although they can still be used. This also means that you can't use them together with scenarios.

This doesn’t make sense to me – why can’t duration be used? I’d prefer not to have to hard-code the same value in each scenario in the js file. I would like to be able to do a command-line override on the command-line.

Also, how do I get a breakdown in the final report by scenario name (or by tag). Right now it aggregates over all scenarios, but having just a single value for average http_req_duration isn’t too informative.

The reason these global options are incompatible when you define custom scenarios in your script is because it would be difficult to map what those options mean across multiple scenarios. So take the multiple-scenarios.js example; each scenario specifies its own duration or maxDuration, with other related options like startTime. So how would your override of --duration 10s actually work? Would you like to run the first scenario for 10s and disregard the second one? Or would you like to run both for 10s, in which case how would startTime apply?

There would be similar and perhaps more complicated questions for the global vus and stages as well, which is why the global options aren’t compatible.

Behind the scenes what the global options do is actually create a default scenario using the most appropriate executor, but this only works if your script doesn’t define custom scenarios.

One thing you could do to override some options in your script with scenarios is to define them from environment variables, with some sane default. If you search the forum for “environment variables” you’ll find some examples of this.

Also, how do I get a breakdown in the final report by scenario name (or by tag).

The end-of-test summary currently doesn’t give a detailed breakdown per scenario, but if you output the metrics to a JSON file or an external database with the k6 run --out option, then you’ll see that metrics have a scenario tag you can use to filter and aggregate on as you need.

Alternatively, you can create groups or custom metrics per scenario, and these will show up in the end-of-test summary.