Parottasalna

AI, Backend Engineering & Architecture Guides

Learning Notes #73 – Awesome Tricks on K6

K6 is an awesome, simple load testing tool. There are many CLI options available to K6. In this blog i jot down notes on some of the nice options available for testing.

1. You can use the k6 run --vus=10 --duration=30s script.js command to specify the number of virtual users (VUs) and the duration of the test. This can be useful for running tests with different levels of concurrency and for controlling the length of the test.

k6 run --vus=10 --duration=30s script.js

    script.js

    import http from 'k6/http';
    import { check, sleep } from 'k6';
     
    export let options = {
      vus: 10, // Number of virtual users
      duration: '10s', // Test duration
    };
     
    export default function () {
      let res = http.get('https://api.restful-api.dev/objects');
      check(res, {
        'is status 200': (r) => r.status === 200,
      });
      sleep(1); // Simulate user wait time
    }

    Explanation

    • Runs the test for 30 seconds with 10 virtual users (VUs).
    • Simulates concurrent users making requests within the specified time frame.

    2. The k6 run --no-throttle flag can be used to disable the automatic rate limiting that K6 applies to test requests. This can be useful if you want to test the maximum possible load that your system can handle.

    k6 run --no-throttle script.js

    Explanation

    • The --no-throttle flag disables automatic rate limiting.
    • This allows the test to generate as many requests as possible, simulating peak load conditions.

    By default, k6 applies automatic rate limiting based on system resource constraints, configured VUs, and iteration pacing.

    It ensures requests do not exceed defined limits, such as rps, and prevents overloading. The --no-throttle flag disables these internal checks, allowing execution as fast as possible, constrained only by hardware and network capacity.

    3. The k6 run --iterations=1000 command can be used to specify the number of iterations that K6 should perform during a test. This can be useful if you want to run a fixed number of requests rather than running the test for a fixed duration.

    4. The k6 run --no-connection-reuse flag can be used to disable connection reuse during a test. This can be useful for simulating the behavior of real users who are opening new connections for each request. The --no-connection-reuse flag forces k6 to open a new connection for every request.

    5. The k6 run --rps=1000 command can be used to specify the number of requests per second that K6 should send during the test. This can be useful for running tests with a fixed request rate.

    6. You can use the --compatibility flag to specify the version of the HTTP/1.1 protocol that K6 should use when making requests. This can be useful if you need to test a system that only supports a specific version of the protocol.

    7. The k6 stats command can be used to collect and analyze performance metrics during a test. This can be useful for identifying performance bottlenecks and for understanding how your system is behaving under load.

    http_reqs......................: 500   100/s
    http_req_duration..............: avg=250ms min=100ms max=500ms p(95)=400ms

    8. K6 has a built-in graphical user interface (GUI) that can be used to visualize the results of a test. You can access the GUI by running the k6 run --ui command and then visiting the URL displayed in the output.

    Discover more from Parottasalna

    Subscribe now to keep reading and get access to the full archive.

    Continue reading