This k6 script is designed for load testing an application by simulating multiple virtual users (VUs) that perform a series of HTTP requests. It includes setup for authentication, payload generation, and defines different testing scenarios.
Script Breakdown
- Imports and Setup: The script imports the http module for making HTTP requests and the check function for validating responses. It also imports a service_name function from an external module. The setup function is executed once before the main test runs. It sends a POST request to a token service to obtain an access token, checking that the response status is 200 (OK) and returning the token for use in subsequent requests.
export function setup() { const url = 'https://example-token-service.com/token'; const formData = {}; const loginRes = http.post(url, formData); check(loginRes, { 'response code was 200': (res) => res.status === 200, }); return { token: loginRes.json().access_token }; }
- Payload Generation: The script defines a target size for the payload (700 KB) and generates a JSON payload array using the generatePayloadArray function. This function constructs an array of empty objects until the total size reaches the specified target.
const targetSizeKB = 700; const payload = generatePayloadArray(targetSizeKB);
- Scenarios Definition: Two scenarios are defined:
perVuIterations: 20 VUs, each performing 5000 iterations over a maximum duration of 300 seconds. constantRequestRate: A constant rate of 248 requests per second for 300 seconds, with a maximum of 20 VUs. const scenarios = { perVuIterations: { executor: 'per-vu-iterations', vus: 20, iterations: 5000, maxDuration: '300s', }, constantRequestRate: { executor: 'constant-arrival-rate', rate: 248, timeUnit: '1s', duration: '300s', preAllocatedVUs: 20, maxVUs: 20, }, };
- Options Configuration: The script configures options for the test, including skipping TLS verification and selecting scenarios based on an environment variable.
export const options = { insecureSkipTLSVerify: true, scenarios: SCENARIO ? { [SCENARIO]: scenarios[SCENARIO] } : {}, };
- Main Function: The default function is executed by each VU, sending a POST request to the specified application endpoint using the token and payload generated earlier.
export default function (data) { const url = 'https://your-application-endpoint.com/_POST'; service_name(url, data.token, payload); }
- Payload Generation Function: The generatePayloadArray function constructs a JSON object containing an array of payloads, ensuring the total size matches the target size.
function generatePayloadArray(targetSizeKB) { const payloadArray = []; let currentSize = 0; while (currentSize < targetSizeKB * 1024) { const payload = {}; const jsonString = JSON.stringify(payload); payloadArray.push(payload); currentSize += jsonString.length; } return JSON.stringify({ payload: payloadArray }); }
This script effectively sets up a load test that simulates user interactions with an application, allowing for performance evaluation under various conditions.
Complete Script as Follow:
import http from 'k6/http'; import { service_name } from './service.js'; import { check } from 'k6'; export function setup() { const url = 'https://example-token-service.com/token'; const formData = {}; const loginRes = http.post(url, formData); check(loginRes, { 'response code was 200': (res) => res.status === 200, }); return { token: loginRes.json().access_token }; } const targetSizeKB = 700; const payload = generatePayloadArray(targetSizeKB); const scenarios = { perVuIterations: { executor: 'per-vu-iterations', vus: 20, iterations: 5000, maxDuration: '300s', }, constantRequestRate: { executor: 'constant-arrival-rate', rate: 248, timeUnit: '1s', // 248 iterations per second, i.e. 248 RPS duration: '300s', preAllocatedVUs: 20, // initial pool of VUs maxVUs: 20, // max VUs if preAllocatedVUs are not enough }, }; const { SCENARIO } = __ENV; export const options = { insecureSkipTLSVerify: true, scenarios: SCENARIO ? { [SCENARIO]: scenarios[SCENARIO] } : {}, }; export default function (data) { const url = 'https://your-application-endpoint.com/_POST'; service_name(url, data.token, payload); } function generatePayloadArray(targetSizeKB) { const payloadArray = []; let currentSize = 0; while (currentSize < targetSizeKB * 1024) { const payload = {}; const jsonString = JSON.stringify(payload); payloadArray.push(payload); currentSize += jsonString.length; } return JSON.stringify({ payload: payloadArray }); }
- If you want to gradually increase the load, you can utilize the provided script, which implements a ramp-up strategy..
import http from 'k6/http'; import { check } from 'k6'; export let options = { stages: [ { duration: '1m', target: 80 }, { duration: '1m', target: 100 }, ], insecureSkipTLSVerify: true, }; export function setup() { const url = 'https://get-token-service.com/token'; const res = http.post(url, {}); return { token: res.json().access_token }; } export default function ({ token }) { const url = 'https://example-server.com/_POST/api/endpoint'; const headers = { 'Authorization': token, 'Content-Type': 'application/json', }; const body = JSON.stringify({ payload: [] }); const res = http.post(url, body, { headers }); check(res, { 'status was 202': (r) => r.status === 202 }); }
Top comments (0)