Modern QA2026k6 Performance Budget Script — tiles
Log inJoin
49 / 95 · 05 Performance & Chaos Engineering · Web Vitals Performance Gates in CI← prev⊞ allnext →☰ Read as one page

8.4k6 Performance Budget Script

Complement Lighthouse (frontend) with k6 (backend API) budgets:

// tests/performance/api-budget.js
import http from 'k6/http';
import { check } from 'k6';

export const options = {
  scenarios: {
    budget_check: {
      executor: 'shared-iterations',
      vus: 5,
      iterations: 50,
      maxDuration: '2m',
    },
  },
  thresholds: {
    // API performance budgets
    'http_req_duration{name:products_list}': ['p(95)<200'],
    'http_req_duration{name:product_detail}': ['p(95)<150'],
    'http_req_duration{name:search}': ['p(95)<300'],
    'http_req_duration{name:cart}': ['p(95)<250'],
    // Global budgets
    http_req_failed: ['rate<0.01'],
  },
};

const BASE_URL = __ENV.K6_TARGET_URL || 'https://staging.example.com';

export default function () {
  // Products list
  let res = http.get(`${BASE_URL}/api/products`, { tags: { name: 'products_list' } });
  check(res, { 'products 200': (r) => r.status === 200 });

  // Product detail
  res = http.get(`${BASE_URL}/api/products/1`, { tags: { name: 'product_detail' } });
  check(res, { 'product detail 200': (r) => r.status === 200 });

  // Search
  res = http.get(`${BASE_URL}/api/search?q=laptop`, { tags: { name: 'search' } });
  check(res, { 'search 200': (r) => r.status === 200 });

  // Cart operations
  res = http.post(`${BASE_URL}/api/cart`, JSON.stringify({ product_id: 1, qty: 1 }), {
    headers: { 'Content-Type': 'application/json' },
    tags: { name: 'cart' },
  });
  check(res, { 'cart 200': (r) => r.status === 200 });
}