Below Script sends GET request once. It creates a single Virtual User(VU) and sends the get request.
import http from 'k6/http';
import { check } from 'k6';
export default function () {
// Target URL
let res = http.get('https://httpbin.test.k6.io/get');
// Basic check to ensure status is 200
check(res, {
'status is 200': (r) => r.status === 200,
});
// Print response body to console
//console.log(res.body);
}
k6 run test.js
To make the same script run for a duration. In this case the number of iterations varies based on how fast the request is completed based on keeping user constant 1(VU).
k6 run test.js --duration 5s
To make the same script run for a iteration. In this case the number of iterations would be 5 and VU is 1.
k6 run test.js --iterations 5
Below Script sends GET request and sleeps for 1 second. So if we run the test for 5 seconds then we run 4 request by same user
import http from 'k6/http';
import { check } from 'k6';
import { sleep } from 'k6';
export default function () {
// Target URL
let res = http.get('https://httpbin.test.k6.io/get');
sleep(1);
// Basic check to ensure status is 200
check(res, {
'status is 200': (r) => r.status === 200,
});
// Print response body to console
//console.log(res.body);
}
To make the same script run for a 10 iteration and 10 VUs.
k6 run test.js -i 10 -u 10
Instead of supplying in command argument we can do the same thing by defining in constant as below
import http from 'k6/http';
import { check } from 'k6';
export const options = {
vus: 10, // fixed number of VUs
iterations: 10 // each VU loops for 30 seconds
};
export default function () {
let res = http.get('https://httpbin.test.k6.io/get');
check(res, {
'status is 200': (r) => r.status === 200,
});
//console.log(res.body);
}