Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I run two scripts at the same time by a command? #1918

Closed
wmsken opened this issue Mar 19, 2021 · 7 comments
Closed

Can I run two scripts at the same time by a command? #1918

wmsken opened this issue Mar 19, 2021 · 7 comments
Labels

Comments

@wmsken
Copy link

wmsken commented Mar 19, 2021

Hi, I need to run script1.js and script2.js in parallel on windows, and I found the following command is failed, it just run the first script and then prompt syntax error for the second script:
k6 run script1.js --vus 1 --duration 10s & script2.js --vus 1 --duration 20s

How can I do this please?
Thank you!

@na--
Copy link
Member

na-- commented Mar 19, 2021

If you want to run multiple scripts simultaneously, you might be better off using the built-in k6 scenarios instead of separate scripts: https://k6.io/docs/using-k6/scenarios

Regarding running CLI apps in the background in Windows, this might be helpful: https://superuser.com/questions/198525/how-can-i-execute-a-windows-command-line-in-background

Here's also a k6 issue where we explore how we might support running multiple scripts from the k6 command line: #1342

Finally, these types of questions are better suited to the community forum, so please use that in the future: https://community.k6.io/

@na-- na-- closed this as completed Mar 19, 2021
@na-- na-- added the question label Mar 19, 2021
@wmsken
Copy link
Author

wmsken commented Mar 20, 2021

Thank you na for your reply, but i am confusing now about how to write my script with scenarios. Could you please help me?
Supposed that i have two separate scripts named script1.js and script2.js, and the default function is script1() and script2(), now i need to run these two scripts in parallel, do i need to write a new script named script3.js like this as below?

export let options = {
"scenarios": {
"script1": {
"executor": "constant-vus".
"vus": 2,
"duration": 1m,
"startTime": 0s,
"exec": "script1"
},
"script2": {
"executor": "constant-vus".
"vus": 1,
"duration": 1m,
"startTime": 0s,
"exec": "script2"
}
}
}

export default function script1() {
...
}

export default function script2() {
...
}
}

Thanks a lot!

@na--
Copy link
Member

na-- commented Mar 22, 2021

You can do it like this:

export { default as func01 } from './script1.js';
export { default as func02 } from './script2.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: 'func01',
    },
  }
}

Where in script1.js and script2.js you have the normal k6 scripts like:

export default function() {
    // ... some k6 test code
}

@wmsken
Copy link
Author

wmsken commented Mar 23, 2021

Thank you na, this really helps! i can do my test normally now.
Thanks again.

@emil45
Copy link

emil45 commented Apr 4, 2021

Hey @na--, if I'm using setup/teardown/handleSummary - where it goes in your example?
Thank you very much for your help.

@na--
Copy link
Member

na-- commented Apr 5, 2021

You can put setup, teardown and handleSummary in their own file, like this:

import { textSummary } from 'https://jslib.k6.io/k6-summary/0.0.1/index.js';

export function setup() {
    console.log('helpers.js setup()');
    return { foo: 'bar' };
}

export function teardown(data) {
    console.log(`helpers.js teardown(${JSON.stringify(data)})`);
}

export function handleSummary(data) {
    console.log('helpers.js handleSummary()');

    return {
        'stdout': textSummary(data, { indent: ' ', enableColors: true, }),
        'raw-summary-data.json': JSON.stringify(data, null, 4),
    };
}

And then you can just re-export them in every other file - script1.js, script2.js, main.js like this:

export { setup, teardown, handleSummary } from './helpers.js';

Also, please don't ask questions in closed GitHub issues... The place for such things is the k6 community forum: community.k6.io

@na--
Copy link
Member

na-- commented Apr 5, 2021

I saved the complete example as a GitHub gist: https://gist.github.com/na--/7105cd97ce76edd4d99c33a8ad38c889

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants