Мы тестируем каждый релиз перед деплоем в продакшен. k6 позволяет писать сценарии на JavaScript и генерировать миллионы запросов с распределённых агентов:
// k6-scripts/send-notification.js
import http from 'k6/http';
import { check, sleep } from 'k6';
import { Rate, Trend } from 'k6/metrics';
const errorRate = new Rate('errors');
const sendDuration = new Trend('send_duration');
export const options = {
scenarios: {
constant_load: {
executor: 'constant-arrival-rate',
rate: 100000, // 100K RPS
timeUnit: '1s',
duration: '10m',
preAllocatedVUs: 500,
maxVUs: 2000,
},
spike: {
executor: 'ramping-arrival-rate',
startRate: 100000,
timeUnit: '1s',
stages: [
{ duration: '2m', target: 100000 }, // Baseline
{ duration: '30s', target: 500000 }, // Spike to 500K
{ duration: '3m', target: 500000 }, // Hold
{ duration: '30s', target: 100000 }, // Back to normal
{ duration: '2m', target: 100000 }, // Cool down
],
preAllocatedVUs: 2000,
maxVUs: 5000,
startTime: '12m', // После constant_load
},
},
thresholds: {
http_req_duration: ['p(95)<100', 'p(99)<500'],
errors: ['rate<0.01'], // Меньше 1% ошибок
},
};
const BASE_URL = __ENV.TARGET_URL || 'https://api-staging.notifypro.ru';
const API_KEY = __ENV.API_KEY;
export default function () {
const payload = JSON.stringify({
client_id: `client-${Math.floor(Math.random() * 200)}`,
channel: ['push', 'sms', 'email', 'telegram'][Math.floor(Math.random() * 4)],
recipient: `user-${Math.floor(Math.random() * 1000000)}`,
template_id: `tmpl-${Math.floor(Math.random() * 50)}`,
priority: Math.random() > 0.9 ? 'high' : 'normal',
idempotency_key: `${Date.now()}-${Math.random().toString(36).slice(2)}`,
data: {
title: 'Test Notification',
body: 'This is a load test notification',
},
});
const params = {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_KEY}`,
},
timeout: '5s',
};
const start = Date.now();
const res = http.post(`${BASE_URL}/api/v1/notifications`, payload, params);
sendDuration.add(Date.now() - start);
const success = check(res, {
'status is 202': (r) => r.status === 202,
'has notification id': (r) => JSON.parse(r.body).id !== undefined,
});
errorRate.add(!success);
}
Тесты запускаются с 10 распределённых агентов (каждый генерирует до 50K RPS). Результаты отправляются в InfluxDB и визуализируются в Grafana. Мы проверяем не только API-слой, но и сквозную доставку: считаем, сколько уведомлений дошло до mock-получателей.
Оставить комментарий