Design a spike profile

In this milestone, you write the spike.js script that defines your spike profile: a short baseline, a sharp burst, a recovery window, and a ramp down to zero. The shape is controlled by a stages array that raises and lowers the number of virtual users (VUs) over time.

Size your baseline and spike VU counts from your Establish a performance baseline with k6 results: pick a baseline close to your system’s normal concurrent load, then a spike target several times higher to simulate a surge. This path uses 5 baseline VUs and a 100-VU spike (a 20x jump) against QuickPizza; replace the URL and targets when you test your own approved non-production stack.

To design a spike profile, complete the following steps:

  1. Create spike.js and add imports:

    JavaScript
    import http from 'k6/http';
    import { check, sleep } from 'k6';
    import { randomIntBetween } from 'https://jslib.k6.io/k6-utils/1.4.0/index.js';
  2. Define stages and thresholds:

    JavaScript
    export const options = {
      stages: [
        // Baseline: establish steady-state metrics at 5 VUs
        { duration: '1m', target: 5 },
        { duration: '1m', target: 5 },
        // Spike: ramp to 100 VUs (20x) in 10 seconds
        { duration: '10s', target: 100 },
        { duration: '1m', target: 100 },
        // Recovery: drop back to baseline and observe for 3 minutes
        { duration: '10s', target: 5 },
        { duration: '3m', target: 5 },
        { duration: '30s', target: 0 },
      ],
      thresholds: {
        http_req_duration: ['p(95)<1500'],
        http_req_failed: ['rate<0.10'],
      },
    };

    Ten-second ramps create the abrupt spike shape. Thresholds are looser than in a baseline test so the run can finish under burst load. Use http_req_failed for pass/fail on HTTP errors; keep checks in the default function for summary visibility.

  3. Add the default function with the same checks as your baseline test:

    JavaScript
    export default function () {
      const res = http.get('https://quickpizza.grafana.com');
    
      check(res, {
        'status is 200': (r) => r.status === 200,
        'response body is not empty': (r) => r.body.length > 0,
      });
    
      // Randomize the sleep time to simulate real user traffic
      sleep(randomIntBetween(1, 3));
    }
  4. Save spike.js. Combine the options block and default function in one file before you run the test in the next milestone.

In the next milestone, you run the spike test and interpret the results.

More to explore (optional)

At this point in your path, you can explore the following topics:

Spike testing

Ramping VUs executor

About test scenarios

Use k6 Script Authoring mode


page 3 of 10