K6 Boost

 How did you create the K6 script in your project?

  • We have angular apps running inside our Java application
  • Open dev tools and start recording
  • Perform user flow and stop the recording
  • Download .har file
  • Convert .har file into k6 format
  • Update the script accordingly

To perform a load test using k6, you'll write a script in JavaScript that specifies the desired number of virtual users (VUs), the duration of the test, and the metrics you want to collect. Below is an example k6 script that includes VUs, test duration, throughput, and other relevant metrics:

javascript

import http from 'k6/http'; import { check, sleep } from 'k6'; import { Rate, Trend } from 'k6/metrics'; // Custom metrics const myTrend = new Trend('custom_trend'); const myRate = new Rate('custom_rate'); // Load test configuration export const options = { stages: [ { duration: '1m', target: 50 }, // ramp-up to 50 VUs over 1 minute { duration: '3m', target: 50 }, // stay at 50 VUs for 3 minutes { duration: '1m', target: 0 }, // ramp-down to 0 VUs over 1 minute ], thresholds: { http_req_duration: ['p(95)<500'], // 95% of requests should be below 500ms custom_rate: ['rate>0.95'], // custom rate should be above 95% }, }; export default function () { const url = 'https://your-api-endpoint.com'; const params = { headers: { 'Content-Type': 'application/json', }, }; // Perform a HTTP GET request let res = http.get(url, params); // Check if the response status is 200 check(res, { 'status is 200': (r) => r.status === 200, }); // Record custom metrics myTrend.add(res.timings.duration); myRate.add(res.status === 200); // Add some delay between requests sleep(1); }

Explanation:

  1. Importing Modules:

    • http: To perform HTTP requests.
    • check and sleep: For validating responses and adding delays.
    • Rate and Trend: Custom metrics to track request durations and success rates.
  2. Custom Metrics:

    • myTrend: Tracks the duration of HTTP requests.
    • myRate: Tracks the rate of successful requests (status 200).
  3. Load Test Configuration (options):

    • stages: Defines the different stages of the test, with the number of VUs and duration for each stage. This example ramps up to 50 VUs, holds for 3 minutes, and then ramps down to 0 VUs.
    • thresholds: Defines performance thresholds for the test. For example, 95% of requests should complete in under 500ms, and the custom rate of successful requests should be above 95%.
  4. Test Function (default function):

    • http.get: Sends a GET request to the specified URL.
    • check: Validates that the response status is 200.
    • Custom metrics are recorded using myTrend.add and myRate.add.
    • sleep(1): Adds a 1-second delay between requests to simulate real user behavior.

Running the Test

To run the above k6 script, save it to a file (e.g., load-test.js) and use the following command:

sh

k6 run load-test.js

This will start the load test according to the defined stages and collect the specified metrics.

Collecting and Viewing Metrics

By default, k6 prints the results to the console. You can also configure k6 to output metrics to various backends (e.g., InfluxDB, Datadog, etc.) for more detailed analysis and visualization.

Example Output

yaml

/\ |‾‾| /‾‾/ /‾/ /\ / \ | |/ / / / / \/ \ | ( / ‾‾\ / \ | |\ \ | (_) | / __________ \ |__| \__\ \___/ .io execution: local script: load-test.js output: - scenarios: (100.00%) 1 scenario, 50 max VUs, 5m30s max duration (incl. graceful stop): * default: Up to 50 looping VUs for 5m0s over 3 stages (gracefulRampDown: 30s, gracefulStop: 30s) running (5m00.1s), 00/50 VUs, 2392 complete and 0 interrupted iterations default [======================================] 00/50 VUs 5m0s status is 200 checks.........................: 100.00% 2392 0 http_req_blocked...............: avg=20.65µs min=1.46µs med=2.74µs max=5.57ms p(90)=3.4µs p(95)=5.3µs http_req_connecting............: avg=9.29µs min=0s med=0s max=5.4ms p(90)=0s p(95)=0s http_req_duration..............: avg=289.74ms min=195.54ms med=271.6ms max=1.71s p(90)=380.31ms p(95)=439.03ms http_req_failed................: 0.00% 0 2392 http_req_receiving.............: avg=102.77µs min=12.95µs med=21.62µs max=1.55ms p(90)=32.92µs p(95)=63.29µs http_req_sending...............: avg=59.61µs min=6.22µs med=12.8µs max=1.4ms p(90)=20.57µs p(95)=37.27µs http_req_tls_handshaking.......: avg=0s min=0s med=0s max=0s p(90)=0s p(95)=0s http_req_waiting...............: avg=289.57ms min=195.45ms med=271.48ms max=1.71s p(90)=380.08ms p(95)=438.83ms http_reqs......................: 2392 7.965187/s iteration_duration.............: avg=1.04s min=1.02s med=1.03s max=2.71s p(90)=1.05s p(95)=1.07s iterations.....................: 2392 7.965187/s vus............................: 1 min=1 max=50 vus_max........................: 50 min=50 max=50

This output provides detailed information about the test execution, including the number of iterations completed, request durations, and any failed requests. It also shows whether the defined thresholds were met




Comments