k6
01 / 02

k6: Traffic Patterns, Custom Metrics & CI Integration

k6: Traffic Patterns, Custom Metrics & CI Integration

Ramping Traffic with Stages

export const options = {
  // Ramp up, hold steady, then ramp down -- more realistically
  // models how real-world traffic builds up and subsides
  stages: [
    { duration: '30s', target: 20 },  // ramp up to 20 VUs
    { duration: '1m', target: 20 },   // hold at 20 VUs
    { duration: '10s', target: 0 },   // ramp down
  ],
}

Load, Stress & Spike Testing

  • Load testing -- verifies correct behavior under expected, realistic traffic levels.

  • Stress testing -- gradually increases load beyond normal to find the system's breaking point.

  • Spike testing -- applies a sudden burst of traffic, checking whether auto-scaling/connection pools handle an abrupt jump (a flash sale, a viral link) that a gradual ramp-up wouldn't reveal.

Custom Metrics

import { Counter } from 'k6/metrics'

// Tracks a business-relevant measurement beyond k6's built-in HTTP
// metrics -- visibility into what specifically matters for this system
const loginFailures = new Counter('login_failures')

export default function () {
  const res = http.post('https://test.example.com/login', payload)
  if (res.status !== 200) {
    loginFailures.add(1)
  }
}

Grouping Requests

import { group } from 'k6'

export default function () {
  group('checkout flow', () => {
    http.get('https://test.example.com/cart')
    http.post('https://test.example.com/checkout', payload)
  })
}

CI Integration & Testing Against Staging

A k6 script with configured thresholds runs automatically in CI (e.g. against a staging environment) after each deploy -- a breached threshold's non-zero exit code blocks the deploy before a regression reaches production. Testing against staging (not production directly) avoids degrading real user experience, though a staging environment's smaller scale can mean results don't perfectly predict production behavior.

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free