From cef86be93ba25373f02815c17ab224cdfb9c3454 Mon Sep 17 00:00:00 2001 From: demo-bot Date: Sun, 2 Aug 2026 11:13:07 +0000 Subject: [PATCH] feat(k6): add bespoke load test files --- k6/configmap.yaml | 53 +++++++++++++++++++++++++++ k6/load-test.js | 91 +++++++++++++++++++++++++++++++++++++++++++++++ k6/testrun.yaml | 24 +++++++++++++ 3 files changed, 168 insertions(+) create mode 100644 k6/configmap.yaml create mode 100644 k6/load-test.js create mode 100644 k6/testrun.yaml diff --git a/k6/configmap.yaml b/k6/configmap.yaml new file mode 100644 index 0000000..827fbdb --- /dev/null +++ b/k6/configmap.yaml @@ -0,0 +1,53 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: k6-test-gate-svc-1 + namespace: dev + labels: + app: gate-svc-1 + app.kubernetes.io/managed-by: backstage + app.kubernetes.io/component: load-testing +data: + K6_OUT: opentelemetry + K6_OTEL_GRPC_EXPORTER_INSECURE: 'true' + K6_OTEL_GRPC_EXPORTER_ENDPOINT: otel-collector.monitoring.svc.cluster.local:4317 + K6_OTEL_METRIC_PREFIX: k6_ + K6_OTEL_FLUSH_INTERVAL: '1000' + K6_OTEL_EXPORT_INTERVAL: '5000' + K6_OTEL_SERVICE_NAME: k6-gate-svc-1 + load-test.js: "import http from 'k6/http';\nimport { check, sleep, group } from\ + \ 'k6';\n\nconst vus = parseInt(__ENV.TEST_VUS || '10');\nconst duration = __ENV.TEST_DURATION\ + \ || '30s';\nconst targetUrl = __ENV.TARGET_URL || 'http://gate-svc-1.dev.svc.cluster.local:8080';\n\ + \nexport const options = {\n scenarios: {\n load_test: {\n executor:\ + \ 'ramping-vus',\n startVUs: 0,\n stages: [\n { duration: '10s',\ + \ target: vus },\n { duration: duration, target: vus },\n { duration:\ + \ '5s', target: 0 },\n ],\n },\n },\n thresholds: {\n http_req_duration:\ + \ ['p(95)<500'],\n http_req_failed: ['rate<0.01'],\n },\n};\n\nhttp.setResponseCallback(http.expectedStatuses({\ + \ min: 200, max: 399 }));\n\nexport default function () {\n group('Health Check',\ + \ () => {\n const res = http.get(`${targetUrl}/health`);\n check(res, {\n\ + \ 'status is 200': (r) => r.status === 200,\n 'response time < 500ms':\ + \ (r) => r.timings.duration < 500,\n 'body contains status UP': (r) => r.json().status\ + \ === 'UP',\n });\n });\n\n sleep(0.5);\n\n group('Items API', () => {\n\ + \ // List all items\n let res = http.get(`${targetUrl}/api/items`);\n \ + \ check(res, {\n 'status is 200': (r) => r.status === 200,\n 'response\ + \ time < 500ms': (r) => r.timings.duration < 500,\n 'body is an array': (r)\ + \ => Array.isArray(r.json()),\n });\n\n // Create a new item\n const\ + \ newItem = { name: 'Test Item', description: 'This is a test item' };\n res\ + \ = http.post(`${targetUrl}/api/items`, JSON.stringify(newItem), {\n headers:\ + \ { 'Content-Type': 'application/json' },\n });\n check(res, {\n 'status\ + \ is 201': (r) => r.status === 201,\n 'response time < 500ms': (r) => r.timings.duration\ + \ < 500,\n 'body contains id': (r) => r.json().id !== undefined,\n });\n\ + \n const itemId = res.json().id;\n\n // Retrieve the created item\n res\ + \ = http.get(`${targetUrl}/api/items/${itemId}`);\n check(res, {\n 'status\ + \ is 200': (r) => r.status === 200,\n 'response time < 500ms': (r) => r.timings.duration\ + \ < 500,\n 'body contains correct id': (r) => r.json().id === itemId,\n \ + \ });\n\n // Update the item\n const updatedItem = { name: 'Updated Item',\ + \ description: 'Updated description' };\n res = http.put(`${targetUrl}/api/items/${itemId}`,\ + \ JSON.stringify(updatedItem), {\n headers: { 'Content-Type': 'application/json'\ + \ },\n });\n check(res, {\n 'status is 200': (r) => r.status === 200,\n\ + \ 'response time < 500ms': (r) => r.timings.duration < 500,\n 'body\ + \ contains updated name': (r) => r.json().name === 'Updated Item',\n });\n\n\ + \ // Delete the item\n res = http.del(`${targetUrl}/api/items/${itemId}`);\n\ + \ check(res, {\n 'status is 200': (r) => r.status === 200,\n 'response\ + \ time < 500ms': (r) => r.timings.duration < 500,\n 'body contains deleted\ + \ id': (r) => r.json().deleted === itemId,\n });\n });\n\n sleep(0.5);\n}" diff --git a/k6/load-test.js b/k6/load-test.js new file mode 100644 index 0000000..ba9914c --- /dev/null +++ b/k6/load-test.js @@ -0,0 +1,91 @@ +import http from 'k6/http'; +import { check, sleep, group } from 'k6'; + +const vus = parseInt(__ENV.TEST_VUS || '10'); +const duration = __ENV.TEST_DURATION || '30s'; +const targetUrl = __ENV.TARGET_URL || 'http://gate-svc-1.dev.svc.cluster.local:8080'; + +export const options = { + scenarios: { + load_test: { + executor: 'ramping-vus', + startVUs: 0, + stages: [ + { duration: '10s', target: vus }, + { duration: duration, target: vus }, + { duration: '5s', target: 0 }, + ], + }, + }, + thresholds: { + http_req_duration: ['p(95)<500'], + http_req_failed: ['rate<0.01'], + }, +}; + +http.setResponseCallback(http.expectedStatuses({ min: 200, max: 399 })); + +export default function () { + group('Health Check', () => { + const res = http.get(`${targetUrl}/health`); + check(res, { + 'status is 200': (r) => r.status === 200, + 'response time < 500ms': (r) => r.timings.duration < 500, + 'body contains status UP': (r) => r.json().status === 'UP', + }); + }); + + sleep(0.5); + + group('Items API', () => { + // List all items + let res = http.get(`${targetUrl}/api/items`); + check(res, { + 'status is 200': (r) => r.status === 200, + 'response time < 500ms': (r) => r.timings.duration < 500, + 'body is an array': (r) => Array.isArray(r.json()), + }); + + // Create a new item + const newItem = { name: 'Test Item', description: 'This is a test item' }; + res = http.post(`${targetUrl}/api/items`, JSON.stringify(newItem), { + headers: { 'Content-Type': 'application/json' }, + }); + check(res, { + 'status is 201': (r) => r.status === 201, + 'response time < 500ms': (r) => r.timings.duration < 500, + 'body contains id': (r) => r.json().id !== undefined, + }); + + const itemId = res.json().id; + + // Retrieve the created item + res = http.get(`${targetUrl}/api/items/${itemId}`); + check(res, { + 'status is 200': (r) => r.status === 200, + 'response time < 500ms': (r) => r.timings.duration < 500, + 'body contains correct id': (r) => r.json().id === itemId, + }); + + // Update the item + const updatedItem = { name: 'Updated Item', description: 'Updated description' }; + res = http.put(`${targetUrl}/api/items/${itemId}`, JSON.stringify(updatedItem), { + headers: { 'Content-Type': 'application/json' }, + }); + check(res, { + 'status is 200': (r) => r.status === 200, + 'response time < 500ms': (r) => r.timings.duration < 500, + 'body contains updated name': (r) => r.json().name === 'Updated Item', + }); + + // Delete the item + res = http.del(`${targetUrl}/api/items/${itemId}`); + check(res, { + 'status is 200': (r) => r.status === 200, + 'response time < 500ms': (r) => r.timings.duration < 500, + 'body contains deleted id': (r) => r.json().deleted === itemId, + }); + }); + + sleep(0.5); +} \ No newline at end of file diff --git a/k6/testrun.yaml b/k6/testrun.yaml new file mode 100644 index 0000000..e4852e3 --- /dev/null +++ b/k6/testrun.yaml @@ -0,0 +1,24 @@ +apiVersion: k6.io/v1alpha1 +kind: TestRun +metadata: + name: k6-gate-svc-1 + namespace: dev + labels: + app: gate-svc-1 + backstage.io/component: gate-svc-1 + app.kubernetes.io/managed-by: backstage + app.kubernetes.io/component: load-testing +spec: + parallelism: 1 + script: + configMap: + name: k6-test-gate-svc-1 + file: load-test.js + runner: + image: grafana/k6:latest + envFrom: + - configMapRef: + name: k6-test-gate-svc-1 + env: + - name: K6_OTEL_SERVICE_NAME + value: k6-gate-svc-1