We run Get http request with multiple user(vus) and multiple iteration(iterations) for particular duration. It is straightforward like below.
import http from 'k6/http';
import { sleep } from 'k6';
export let options ={
vus:'10',
duration: '20s',
iterations: '10'
}
export default function (){
const baseUri = 'http://Myhost:3200'
let response = http.get(`${baseUri}/api/v1/vm-inventory`)
let body = JSON.parse(response.body)
body.items.forEach(element => {
if (element.datastoreIds.length != 0 ){
console.log(JSON.stringify(element))
console.log(`Name of the vm is ${element.name}`)
}
});
sleep(1);
}
I also need to run post request by multiple user and multiple iterations. But all the example I am seeing are related to single post request. When we run multiple post request ,our data need to be changed for every iteration. How it is possible with k6? Below is my post request.
import http from 'k6/http';
import { sleep } from 'k6';
export let options ={
vus:'5',
duration: '120s',
iterations: '5'
}
let payload = `{
"VMId": "70c8b578-32ef-40d2-bcc5-81267423d2c4",
"name": "test_vm_1",
"mem": "4,
"cpu": "2",
"disk": "50gb"
}`
export default function () {
const url = 'http://myhost:3200/api/v1/vm-inventory'
var header = {
headers: {
'Content-Type': 'application/json',
},
};
let res = http.post(url, JSON.stringify(payload),header);
console.log(res.json().json.name);
sleep(5);
}
I need to change the Vm name in payload for every iteration in order the request to be unique. How to achieve this in k6? In Jmeter , they are reading it from csv with different data for each iteration and achieving it. But I could not find any sample for K6.
we need to create the payload inside the export default function so that we can modify it there before sending it. If the VMId just needs to be a unique UUID, have a look at jslib utils library, which contains a function for generating a uuidv4:
export function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
let r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
Putting it together:
import http from 'k6/http';
import { sleep } from 'k6';
export let options = {
vus: '5',
duration: '120s',
iterations: '5'
}
export default function () {
let payload = {
VMId: uuidv4(),
name: `test_vm_${Date.now()}`,
mem: 4,
cpu: 2,
disk: "50gb"
};
const url = 'http://myhost:3200/api/v1/vm-inventory'
var header = {
headers: {
'Content-Type': 'application/json',
},
};
let res = http.post(url, JSON.stringify(payload), header);
console.log(res.json().json.name);
sleep(5);
}
export function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
let r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
(note that I've changed the payload to a JS object so that it doesn't get stringified twice).
Also we can use the payload like below.
let payload = {
VMId: uuidv4(),
name: `test_vm_vu_${__VU}`,
mem: 4,
cpu: 2,
disk: "50gb"
};
__VU will be giving virtual user number so that we can create test_vm_vu_1,test_vm_vu_3, test_vm_vu_2 . (It will be run in parallel, so the number will not come in sequential manner)
I got this answer from K6 slack community group. I post it here for anybody facing the same situation.