Skip to content

Commit 0fafd0a

Browse files
authored
Merge pull request #19 from davidsilva/feature/load-testing
Try running k6 load tests on the backend
2 parents 9676409 + f45f02a commit 0fafd0a

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

.github/workflows/ci.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,22 @@ jobs:
188188
- name: Update ECS service for frontend
189189
run: |
190190
aws ecs update-service --cluster ${{ secrets.ECS_CLUSTER_NAME }} --service ${{ secrets.FRONTEND_SERVICE_NAME }} --force-new-deployment
191+
192+
- name: Wait for ECS backend service update
193+
run: |
194+
aws ecs wait services-stable --cluster ${{ secrets.ECS_CLUSTER_NAME }} --services ${{ secrets.BACKEND_SERVICE_NAME }}
195+
196+
load-test:
197+
runs-on: ubuntu-latest
198+
needs: deploy
199+
200+
steps:
201+
- name: Checkout code
202+
uses: actions/checkout@v2
203+
204+
- name: Run load tests with k6
205+
uses: k6io/[email protected]
206+
with:
207+
filename: ./backend/load-test.js
208+
env:
209+
BASE_URL: https://api.dev.interviewprep.onyxdevtutorials.com

backend/load-test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import http from 'k6/http';
2+
import { check, sleep } from 'k6';
3+
4+
// Possible test types include smoke, stress, spike, soak and load.
5+
// Smoke: verify system can habdle a minimal load w/o issues.
6+
// Stress: determine breaking point by increading number of VUs until the API starts returning errors or response times degrade significantly.
7+
// Spike: test how the system handles sudden spikes in load. A sudden increase in the number of VUs to simulate a traffic spike, followed by a return to normal load.
8+
// Soak (endurance): test the system's stability and performance over an extended period. Running a constant load for several hours or days to identify memory leaks or performance degradation over time.
9+
// Load: test the system under expected peak load conditions. Running a constant load for a period of time to verify that the system can handle the expected number of users.
10+
export let options = {
11+
stages: [
12+
{ duration: '1m', target: 10 }, // Ramp-up to 10 users over 1 minute
13+
{ duration: '3m', target: 10 }, // Stay at 10 users for 3 minutes
14+
{ duration: '1m', target: 0 }, // Ramp-down to 0 users over 1 minute
15+
],
16+
};
17+
18+
const BASE_URL = __ENV.BASE_URL || 'https://localhost:3000';
19+
20+
export default function () {
21+
let res = http.get(`${BASE_URL}/api/v0/users`);
22+
check(res, {
23+
'status was 200': (r) => r.status == 200,
24+
'response time was < 500ms': (r) => r.timings.duration < 500,
25+
});
26+
sleep(1);
27+
}

backend/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"test:clearCache": "jest --clearCache",
1414
"test:users": "jest --config ./jest.config.ts --testPathPattern=users.integration.test.ts",
1515
"test:products": "jest --config ./jest.config.ts --testPathPattern=products.integration.test.ts",
16-
"test:unit": "jest --config ./jest.config.ts --testPathPattern=unit"
16+
"test:unit": "jest --config ./jest.config.ts --testPathPattern=unit",
17+
"load-test": "k6 run load-test.js"
1718
},
1819
"_moduleAliases": {},
1920
"keywords": [],

0 commit comments

Comments
 (0)