Modern QA2026Rate Limit Testing — tiles
Log inJoin
63 / 95 · 05 Performance & Chaos Engineering · Load Testing LLM Endpoints← prev⊞ allnext →☰ Read as one page

10.4Rate Limit Testing

Understanding your provider's rate limit behavior is critical for capacity planning:

// k6-rate-limit-test.js
import http from 'k6/http';
import { Counter, Trend } from 'k6/metrics';

const rateLimits = new Counter('rate_limit_responses');
const responseStatus = new Counter('response_status');
const retryAfter = new Trend('retry_after_seconds');

export const options = {
  scenarios: {
    burst: {
      executor: 'constant-arrival-rate',
      rate: 100,       // deliberately exceed expected rate limit
      timeUnit: '1s',
      duration: '2m',
      preAllocatedVUs: 100,
      maxVUs: 200,
    },
  },
};

export default function () {
  const res = http.post('https://api.example.com/v1/chat/completions',
    JSON.stringify({
      model: __ENV.LLM_SMALL_MODEL,  // your provider's cheapest small-tier model
      messages: [{ role: "user", content: "Say hi" }],
      max_tokens: 5,
    }),
    { headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${__ENV.LLM_API_KEY}`,
    }}
  );

  responseStatus.add(1, { status: String(res.status) });

  if (res.status === 429) {
    rateLimits.add(1);
    const retryHeader = res.headers['Retry-After'];
    if (retryHeader) {
      retryAfter.add(parseFloat(retryHeader));
    }
  }
}