Run two/several scenario files in parallel or serially using single file

I would like to run two/several files in parallel/serially using a single file

fileA.js

export let options = {
  scenarios: {
    funcA: {
      exec: "funcA",
      executor: "per-vu-iterations",
     env: { id: 'funcA' },
    },
  },
  thresholds: {
    failedTestCases: [{ threshold: "count===0", abortOnFail: false }],
  },
};

export function funcA() {
  runA();
}

fileB.js

export let options = {
  scenarios: {
    funcB: {
      exec: "funcB",
      startTime: '30s',
      executor: "per-vu-iterations",
     env: { id: 'funcB' },
    },
  },
  thresholds: {
    failedTestCases: [{ threshold: "count===0", abortOnFail: false }],
  },
};

export function funcB() {
 runB();
}

I would like to run both the files fileA.js and fileB.js from a single file (start.js). Is there way do that without using the export default function ?

Note - I don’t want to combine two files using scenarios and run them. I am looking for a way to use a single file that automatically run both the files without using the default function in each file. I have like 300 such files and so combining will not work.

For example

k6 run start.js → it should run fileA.js and fileB.js in parallel or serially

Hello!

Yes, this is achieved using Scenarios.

Your start.js would be the script that sets the scenario options (they would not be in each individual file you want to run).

Here’s an example:

import { script1main } from './script1.js';
import { script2main } from './script2.js';

export const options = {
  scenarios: {
    script1: {
      exec: 'script1',
      executor: 'constant-vus',
      vus: 5,
      duration: '10s'
    },
    script2: {
      exec: 'script2',
      executor: 'constant-vus',
      vus: 5,
      duration: '10s'
    }
  }
};

export function script1() {
  script1main();
};

export function script2() {
  script2main();
};

Your script files would then just define the function being imported as usual:

export function script1main() {
  console.log("script1main()");
}

This setup also caters for running external script files sequentially.

You don’t even need define wrappers around script1main and script2main, you should be able to do something like this:

export { default as script1main } from './script1.js';
export { default as script2main } from './script2.js';
2 Likes

Here’s a gist I made with an example that also re-uses setup(), teardown() and handleSummary(): code reuse in k6, you can `k6 run main.js` but also `k6 run script1.js` and `k6 run script2.js` · GitHub

1 Like

@ned @Tom Hello, thank you for your replies. My use case is slightly different as I am already doing this.

Suppose my fileA.js and fileB.js are already containing imports from other files and now I want to run them using single file. For example

fileA.js is something like this

export { default as func01 } from './script1.js';
export { default as func02 } from './script2.js';
export { setup, teardown, handleSummary } from './helpers.js';

export let options = {
    scenarios: {
        'first_scenario': {
            executor: 'constant-vus',
            vus: 2,
            duration: '10s',
            exec: 'func01',
        },
        'second_scenario': {
            executor: 'constant-vus',
            vus: 3,
            duration: '15s',
            exec: 'func02',
        },
    }
}

fileB.js is

export { default as func01 } from './script1.js';
export { default as func02 } from './script2.js';
export { setup, teardown, handleSummary } from './helpers.js';

export let options = {
    scenarios: {
        'first_scenario': {
            executor: 'constant-vus',
            vus: 2,
            duration: '10s',
            exec: 'func01',
        },
        'second_scenario': {
            executor: 'constant-vus',
            vus: 3,
            duration: '15s',
            exec: 'func02',
        },
    }
}

now I want to run them as a single file. Is there a way to do that ? In my case, the fileA and fileB are both containing 100 import statements each to run different files for functional testing.

I am not sure I completely understand your use case, but if you have 2 files with 100 import statements, what’s stopping you from having another one with 200? :sweat_smile:

Joking aside, instead of using ES6 import and export statements, you might want to use the ES5.1 require() function and exports object k6 has, since they are a bit a bit easier to work with programmatically. You can loop through the exported elements of a script and do stuff with them, re-exporting them only if necessary, etc. Just keep in mind that it’s not the same as Node.JS’s require function.

You don’t need to be in --compatibility-mode=base to use requre(), you can use it in your ES6 scripts as well.

hmm okay. We already have multiple files with bunch of import statements but I wanted to provide a single test file to run tests as it’s much convenient and easier to manage on our side. I will checkout using require function. thank you :+1: