|
| 1 | +import http from "k6/http"; |
| 2 | +import { check, sleep } from "k6"; |
| 3 | +import { Trend, Rate } from "k6/metrics"; |
| 4 | +import { randomItem } from "https://jslib.k6.io/k6-utils/1.2.0/index.js"; |
| 5 | + |
| 6 | +const baseUrl = `${__ENV.BASE_URL}/api/v1`; |
| 7 | +const apiKey = __ENV.API_KEY; |
| 8 | +const appId = __ENV.APP_ID; |
| 9 | +const params = { |
| 10 | + headers: { |
| 11 | + "Content-Type": "application/json", |
| 12 | + Authorization: `Bearer ${apiKey}`, |
| 13 | + }, |
| 14 | +}; |
| 15 | + |
| 16 | +const listEventsErrorRate = new Rate("List_Events_errors"); |
| 17 | +const createEventErrorRate = new Rate("Create_Event_error"); |
| 18 | +const ListEventsTrend = new Trend("List_Events"); |
| 19 | +const createEventsTrend = new Trend("Create_Events"); |
| 20 | + |
| 21 | +const names = ["John", "Jane", "Bert", "Ed"]; |
| 22 | +const emails = [ |
| 23 | + "John@gmail.com", |
| 24 | + "Jane@amazon.com", |
| 25 | + "Bert@yahoo.com", |
| 26 | + "Ed@hotmail.com", |
| 27 | +]; |
| 28 | + |
| 29 | +export const generateEventPayload = (appId) => ({ |
| 30 | + app_id: appId, |
| 31 | + data: { |
| 32 | + player_name: randomItem(names), |
| 33 | + email: randomItem(emails), |
| 34 | + }, |
| 35 | + event_type: `${randomItem(names)}.${randomItem(names)}`.toLowerCase(), |
| 36 | +}); |
| 37 | + |
| 38 | +export let options = { |
| 39 | + noConnectionReuse: true, |
| 40 | + stages: [ |
| 41 | + { duration: "60s", target: 20 }, // simulate ramp-up of traffic from 1 to 20 users over 60s |
| 42 | + { duration: "60s", target: 20 }, // stay at 20 users for 60s |
| 43 | + ], |
| 44 | + thresholds: { |
| 45 | + "List_Events": ["p(95) < 3000"], //95% of requests must complete below 3s |
| 46 | + "Create_Events": ["p(99) < 3000"], //99% of requests must complete below 3s |
| 47 | + "List_Events_errors": ["rate<0.1"], // error rate must be less than 10% |
| 48 | + "Create_Event_error": ["rate<0.1"], // error rate must be less than 10% |
| 49 | + http_req_duration: ["p(99)<6000"], // 99% of requests must complete below 6s |
| 50 | + }, |
| 51 | +}; |
| 52 | + |
| 53 | +export default function () { |
| 54 | + let eventBody = JSON.stringify(generateEventPayload(appId)); |
| 55 | + const listEventsUrl = `${baseUrl}/events?appId=${appId}`; |
| 56 | + const createEventUrl = `${baseUrl}/events`; |
| 57 | + |
| 58 | + const requests = { |
| 59 | + "List_Events": { |
| 60 | + method: "GET", |
| 61 | + url: listEventsUrl, |
| 62 | + params: params, |
| 63 | + }, |
| 64 | + "Create_Events": { |
| 65 | + method: "POST", |
| 66 | + url: createEventUrl, |
| 67 | + params: params, |
| 68 | + body: eventBody, |
| 69 | + }, |
| 70 | + }; |
| 71 | + |
| 72 | + const responses = http.batch(requests) |
| 73 | + const listResp = responses['List_Events']; |
| 74 | + const createResp = responses['Create_Events']; |
| 75 | + |
| 76 | + check(listResp, { |
| 77 | + 'list_events': (r) => r.status === 200, |
| 78 | + }) || listEventsErrorRate.add(1); |
| 79 | + |
| 80 | + ListEventsTrend.add(listResp.timings.duration) |
| 81 | + |
| 82 | + check(createResp, { |
| 83 | + 'event_created': (r) => r.status === 201, |
| 84 | + }) || createEventErrorRate.add(1) |
| 85 | + |
| 86 | + createEventsTrend.add(createResp.timings.duration) |
| 87 | + |
| 88 | + sleep(1); |
| 89 | +} |
0 commit comments