| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- import http from 'k6/http';
- import { check, sleep } from 'k6';
- import { Rate, Trend } from 'k6/metrics';
- const BASE_URL = __ENV.BASE_URL;
- const SCENARIO = __ENV.SCENARIO || 'mixed';
- const MODE = __ENV.MODE || 'duration';
- const OUTLOOK_PAYLOAD = __ENV.OUTLOOK_PAYLOAD || '../payloads/outlook.json';
- const ACHIEVEMENT_PAYLOAD = __ENV.ACHIEVEMENT_PAYLOAD || '../payloads/achievement.json';
- const REQUEST_TIMEOUT = __ENV.REQUEST_TIMEOUT || '120s';
- const outlookPayload = open(OUTLOOK_PAYLOAD);
- const achievementPayload = open(ACHIEVEMENT_PAYLOAD);
- const businessSuccess = new Rate('business_success');
- const outlookDuration = new Trend('outlook_sync_duration');
- const achievementDuration = new Trend('achievement_sync_duration');
- function scenarioOptions() {
- if (MODE === 'burst') {
- return {
- scenarios: {
- burst_once: {
- executor: 'per-vu-iterations',
- vus: Number(__ENV.VUS || 20),
- iterations: Number(__ENV.ITERATIONS_PER_VU || 1),
- maxDuration: __ENV.MAX_DURATION || '2m',
- },
- },
- };
- }
- return {
- vus: Number(__ENV.VUS || 20),
- duration: __ENV.DURATION || '3m',
- };
- }
- export const options = {
- ...scenarioOptions(),
- thresholds: {
- http_req_failed: ['rate==0'],
- business_success: ['rate==1'],
- },
- };
- function requireBaseUrl() {
- if (!BASE_URL) {
- throw new Error('BASE_URL is required, for example: BASE_URL=https://test-host k6 run loadtest/k6/report-sync.js');
- }
- }
- function postSync(path, payload, apiName) {
- const response = http.post(`${BASE_URL}${path}`, payload, {
- headers: {
- 'Content-Type': 'application/json',
- ...( __ENV.TOKEN ? { Authorization: `Bearer ${__ENV.TOKEN}` } : {} ),
- },
- tags: { api: apiName },
- timeout: REQUEST_TIMEOUT,
- });
- const ok = check(response, {
- [`${apiName} status is 200`]: (res) => res.status === 200,
- [`${apiName} has downloadUrl`]: (res) => {
- try {
- return Boolean(res.json('data.downloadUrl'));
- } catch (error) {
- return false;
- }
- },
- });
- businessSuccess.add(ok, { api: apiName });
- if (apiName === 'outlook_sync') {
- outlookDuration.add(response.timings.duration);
- }
- if (apiName === 'achievement_sync') {
- achievementDuration.add(response.timings.duration);
- }
- }
- export default function () {
- requireBaseUrl();
- if (SCENARIO === 'outlook') {
- postSync('/api/exam-sprint/outlook-reports/sync', outlookPayload, 'outlook_sync');
- } else if (SCENARIO === 'achievement') {
- postSync('/api/exam-sprint/achievement-reports/sync', achievementPayload, 'achievement_sync');
- } else {
- if ((__VU + __ITER) % 2 === 0) {
- postSync('/api/exam-sprint/outlook-reports/sync', outlookPayload, 'outlook_sync');
- } else {
- postSync('/api/exam-sprint/achievement-reports/sync', achievementPayload, 'achievement_sync');
- }
- }
- sleep(Number(__ENV.SLEEP_SECONDS || 0.2));
- }
|