-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Creating a global variable #2267
Comments
In k6, the iterations (the Similarly, the In short, you can't use You can use the fact that import { sleep } from 'k6';
import exec from 'k6/execution';
import http from 'k6/http';
import { randomString } from 'https://jslib.k6.io/k6-utils/1.1.0/index.js';
const totalVUs = 10;
export let options = {
scenarios: {
// The executor here doesn't matter, as long as you make sure that its
// maximum duration + gracefulStop value is less than the startTime of
// the second scenario, and that they have the same number of VUs
create_users: {
executor: 'constant-arrival-rate',
preAllocatedVUs: totalVUs,
rate: 10,
duration: '13s',
gracefulStop: '2s',
exec: 'createUser',
},
cleanup: {
executor: 'per-vu-iterations',
vus: totalVUs,
iterations: 1, // we only need one iteration to clean everything up
startTime: '15s', // equal to duration + gracefulStop of create_users
exec: 'cleanupUsers',
},
},
};
let customerList = [];
export function createUser() {
let customerName = `user-${exec.scenario.iterationInTest}-${randomString(10)}`;
console.log(`VU {${exec.vu.idInTest}} created customer ${customerName}...`);
// TODO: actually create it :)
customerList.push(customerName);
};
export function cleanupUsers() {
// customerList here will be local for this VU, populated with just the
// users that the same VU created in the previous scenario.
customerList.forEach(customerName => {
console.log(`VU {${exec.vu.idInTest}} deleted customer ${customerName}!`);
// TODO: actually delete it...
});
}; I hope this helps, and I've opened a new issue to explain these concepts better in our docs, since it's obvious https://k6.io/docs/using-k6/test-life-cycle/ is not quite enough: grafana/k6-docs#512. And an issue to have this workaround as an official example: grafana/k6-docs#513 |
Assuming that I want to test our SignUp endpoint and I'm going to Sign up more than 10 users in 10 iterations at once.
Every HTTP request for signup will return a unique ID for the created user which we called
customer_id
.I want when whole the test is done, delete all the new users which are created by our test, in the
teardown()
.I already read the documentation and search a lot, but cannot find any way to reach this.
This is what I mean (using init section):
Also, I have tested using setup(), neither works.
The text was updated successfully, but these errors were encountered: