2. Babel Coder
RUNNING K6
import http from 'k6/http';
import { sleep, check } from 'k6';
export default function () {
const res = http.get('http://localhost:5000/api/v1/articles');
check(res, {
'is status 200': (r) => r.status === 200,
});
sleep(1);
}
import http from 'k6/http';
import { sleep, check } from 'k6';
import { Options } from 'k6/options'
export const options: Options = {
vus: 10,
duration: '30s'
}
export default function () {
const res = http.get('http://localhost:5000/api/v1/articles');
check(res, {
'is status 200': (r) => r.status === 200,
});
sleep(1);
}
k6 run --vus 10 --duration 30s script.js
k6 run script.js
3. Babel Coder
RAMPING UP / DOWN VUS
import http from 'k6/http';
import { sleep, check } from 'k6';
import { Options } from 'k6/options'
export const options: Options = {
stages: [
{ duration: '30s', target: 30 },
{ duration: '2m30s', target: 200 },
{ duration: '20s', target: 0 },
]
}
export default function () {
const res = http.get('http://localhost:5000/api/v1/articles');
check(res, {
'is status 200': (r) => r.status === 200,
});
sleep(1);
}
4. Babel Coder
METRICS
Metric Name Description
vus Current number of active virtual users
iterations The aggregate number of times the VUs executed the
JS script
checks The rate of successful checks.
http_reqs How many total HTTP requests k6 generated.
http_req_waiting Time spent waiting for response from remote host.
http_req_duration Total time for the request.
http_req_failed The rate of failed requests.
6. Babel Coder
ABORTING A TEST
import http from 'k6/http';
import { sleep, check } from 'k6';
import { Options } from 'k6/options'
export const options: Options = {
vus: 100,
duration: '1m',
thresholds: {
http_req_failed: [{
threshold: 'rate<0.01',
abortOnFail: true,
delayAbortEval: '10s'
}],
}
}
export default function () {
const res = http.get('http://localhost:5000/api/v1/articlexs');
check(res, {
'is status 200': (r) => r.status === 200,
});
sleep(1);
}
7. Babel Coder
SMOKE TESTING
• You want to run a smoke test as a sanity check every time you write a new
script or modify an existing script.
• Verify that your test script doesn't have errors.
• Verify that your system doesn't throw any errors when under minimal load.
export const options = {
vus: 1,
duration: '1m',
thresholds: {
http_req_duration: ['p(99)<1500'],
},
};
8. Babel Coder
LOAD TESTING
• Load Testing is a type of Performance Testing used to determine a system's
behavior under both normal and peak conditions.
• Load Testing is used to ensure that the application performs satisfactorily
when many users access it at the same time.
export const options = {
stages: [
{ duration: '5m', target: 100 },
{ duration: '10m', target: 100 },
{ duration: '5m', target: 0 },
],
thresholds: {
'http_req_duration': ['p(99)<1500'],
},
};
9. Babel Coder
STRESS TESTING
• Stress Testing is a type of load testing used to determine the limits of the
system. The purpose of this test is to verify the stability and reliability of the
system under extreme conditions.
export const options = {
stages: [
{ duration: '2m', target: 100 },
{ duration: '5m', target: 100 },
{ duration: '2m', target: 200 },
{ duration: '5m', target: 200 },
{ duration: '2m', target: 300 },
{ duration: '5m', target: 300 },
{ duration: '2m', target: 400 },
{ duration: '5m', target: 400 },
{ duration: '10m', target: 0 },
],
};
10. Babel Coder
SPIKE TESTING
• Spike testing is a type of stress testing that immediately overwhelms the
system with an extreme surge of load.
• How your system will perform under a sudden surge of traffic.
• If your system will recover once the traffic has subsided.
export const options = {
stages: [
{ duration: '10s', target: 100 },
{ duration: '1m', target: 100 },
{ duration: '10s', target: 1400 },
{ duration: '3m', target: 1400 },
{ duration: '10s', target: 100 },
{ duration: '3m', target: 100 },
{ duration: '10s', target: 0 },
],
};
11. Babel Coder
SOAK TESTING
• A soak test uncovers performance and reliability issues stemming from a
system being under pressure for an extended period.
• Verify that your system doesn't suffer from bugs or memory leaks, which result
in a crash or restart after several hours of operation.
export const options = {
stages: [
{ duration: '2m', target: 400 },
{ duration: '3h56m', target: 400 },
{ duration: '2m', target: 0 },
],
};