Skip to content

Instantly share code, notes, and snippets.

@na--

na--/helpers.js Secret

Created April 5, 2021 11:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save na--/7105cd97ce76edd4d99c33a8ad38c889 to your computer and use it in GitHub Desktop.
Save na--/7105cd97ce76edd4d99c33a8ad38c889 to your computer and use it in GitHub Desktop.
code reuse in k6, you can `k6 run main.js` but also `k6 run script1.js` and `k6 run script2.js`
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),
};
}
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',
},
}
}
import { sleep } from "k6";
export { setup, teardown, handleSummary } from './helpers.js';
export default function () {
console.log('script1 main');
sleep(1);
}
import { sleep } from "k6";
export { setup, teardown, handleSummary } from './helpers.js';
export default function () {
console.log('script2 main');
sleep(1);
}
@emil45
Copy link

emil45 commented Apr 6, 2021

That's actually what I'm doing right now (running bash script, and then printing all the JSON's, then to CSV) ... thank you very much!
(I just thought maybe I can save some code ... but eventually, the handleSummary got me)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment