import http from 'k6/http';
import { check, group, sleep, fail } from 'k6';
export let options = {
vus: 1000,
iterations: 1000,
// duration: '3m',
//stages: [{ target: 1000, duration: '5m' }],
//thresholds: {
//http_req_duration: ['p(95)<500', 'p(99)<1500'],
//'http_req_duration{name:PublicCrocs}': ['avg<400'],
//'http_req_duration{name:Create}': ['avg<600', 'max<1000'],
//},
};
// TM - global vars object for storing authToken (and whatever else you want)
const vars = {};
const PASSWORD = 'test123';
const BASE_URL = 'https://dev.target.org';
export default function main() {
//let uniqueNumber = __VU * 20 + __ITER; //setting up unique VUs
//This flow is intended that unique VUs each carry an email with same Password
//1) Register
//2) Login with email and Password
//3) Token is grabbed
//4) Joining the group with the token no need user credentials here,
// since the token from the logged in user itself is enough to login
//I am looping here so that I create individual email IDs as usernames
//and a standard pass to register
//for (var id = 1; id <= 20; id++) {
//const USERNAME = `ithayam_${id}@target.org`//[uniqueNumber]; //user_id_${id}@heartmath.com
registerAndLogin();
sleep(4);
joinGroup();
sleep(7);
}
function registerAndLogin() {
const USERNAME = `ithayam_${__VU}${__ITER}${Date.now()}@target.org`;
//const PASSWORD = 'test123';
//vars["BASE_URL"] = 'https://dev.target.org';
let data = {
"email": `${USERNAME}`,
"password": `${PASSWORD}`,
"confirm_password": `${PASSWORD}`,
"first_name": "Real",
"last_name": "Original",
"coherenceGoal": "300",
"isUnder16": "false",
"opt_in": "true",
"is_admin": "false",
"inspiration": "false",
"offers": "false",
"birthday": "2000-11-26",
"parental_email": "test@realoriginal.in"
};
//console.log(` checking for USERNAME ${USERNAME}`);
//console.log(` checking for PASSWORD ${PASSWORD}`);
//console.log(` checking for BASE_URL ${BASE_URL}`);
// register a new user
/*let handleResponse = function(resObj) {
console.log("HAndle response")
console.log(resObj)
}*/
let res;
res = http.post(`${BASE_URL}/api/v1/register`, JSON.stringify(data),
{
headers: {
"Content-Type": "application/json",
Accept: "*/*",
Host: "dev.target.org",
Connection: "keep-alive",
},
//responseCallback: handleResponse
}
);
//console.log(` Request-body: ${res.request.body}`);
//console.log(` checking response code/token after reg: ${res.status} ${res.body}`);
//check(res, { 'auth_token': (r) => r.status === 200 }); //201 ==> 200, there's no success message so I am using the response token variable to check the success
sleep(5);
//Login and grab token
let loginRes;
loginRes = http.post(`${BASE_URL}/api/v1/login`,
JSON.stringify({
"email": `${USERNAME}`,
"password": `${PASSWORD}`
}),
{
headers: {
Accept: "*/*",
"Content-Type": "application/json",
Host: "dev.target.org",
Connection: "keep-alive",
"User-Agent": "Mozilla/5.0",
},
}
);
//console.log(` Request-body: ${loginRes.request.body}`);
//console.log(` checking for auth_token after login ${loginRes.status} ${loginRes.body}`);
//let authToken = loginRes.json('data');
let authToken = loginRes.json('data').auth_token;
check(authToken, { 'auth_token': () => authToken !== '' });
// TM - no need to return authToken; just set it in a global object
//return authToken;
vars["authToken"] = authToken;
// console.log(` AT : ${JSON.stringify(vars["authToken"])}`);
}
function joinGroup() {
let joinRes = http.get(`${BASE_URL}/api/v1/auth/group/join/1991`,
//null, // empty response body? -- I commented out this
{
headers: {
Authorization: `Token ${vars["authToken"]}`,
},
}
);
//console.log(` Join Response-headers: ${JSON.stringify(joinRes.headers)}`);
// console.log(` checking after Join: ${joinRes.status} ${joinRes.body}`);
console.log(` JoinGroup-stats : ${joinRes.status}`);
}
have couple of questions regarding the above script
- To simulate a realistic approach of 1000 users registering at the same time, login in etc(the overall flow): how must I setup/ramp-up the VUs.
- The
sleep
commands that I have used, do they actually sleep because I dont see that they are causing actual sleep when running the program. The reason I have implemented longer sleep because I often get timeouts from the server.