Quick start with K6
I would like to share my experience with load testing. Someday I need to test API method for many queries, because some time response was good, sometime bad. And need to choose between what everybody uses (JMeter) and what is better (K6 https://k6.io/blog/load-testing-restful-apis-with-k6/).
Why I prefer K6 instead of JMeter.
Installation.
On window I use scoop (https://scoop.sh/), because it easier to track updates.
scoop install k6
# check if it is installed fine
scoop --version
领英推荐
First script.
The example that I show not reproduceable, but K6 has good documentation with many examples and AI (that was surprised for me when I return to their site, on that moment there was no Grot)
// book.js
import http from 'k6/http';
import { sleep, check } from 'k6';
let date_current = new Date();
export const options = {
// vus: 1,
// duration: '10s',
stages: [
{ duration: '2m', target: 8 }, // simulate ramp-up of traffic from 1 to 100 users over 5 minutes.
{ duration: '5m', target: 10 }, // stay at 100 users for 10 minutes
{ duration: '2m', target: 0 }, // ramp-down to 0 users
],
};
export default function () {
// given
const url = 'put your URL';
const token = 'put your token';
const params = {
headers: {
'jd-token': token,
'Content-Type': 'application/xml',
'Accept-Language': 'ru-RU',
},
};
let depDate = "28.03.2023";
const payload = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><do_order_req><backward>false</backward><ordersRequest>" +
"<request><car><number>08</number><type>Сидячий</type></car><depDate>" + depDate + "</depDate><requirements>"; // Some dummy xml
// when
const res = http.post(url, payload, params);
// then
check(res, {
'Booked with 200': (r) => r.status == 200,
'Body is not empty': (r) => r.json('body') != null,
'status is 200': (r) => r.json('status') == 200,
'status is 404': (r) => r.json('status') == 404,
'status is 504': (r) => r.json('status') == 504
});
// Logging
console.info(date_current.toString());
console.log(res.status);
console.log('Inner status is ' + res.json('status') + " type: " + typeof (res.json('status')));
console.log('Order id is ' + res.json('body.orderId'));
sleep(1);
}
Run K6
Open terminal, go to your scripts folder and run command (I forget what commands I use for outputting my logs to file, but Grot suggest this):
k6 run --vus 10 --duration 5m --console-output logs.json book.js
To continue with K6 you can read also this article https://www.lambdatest.com/blog/k6-testing-tutorial/