Skip to content

Commit

Permalink
test: add k6 API testing (smoke+stress)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-chinour committed Nov 7, 2024
1 parent a969265 commit 7321c07
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/load_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Run k6 API testsuite
on:
workflow_dispatch:

jobs:
run-test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup K6
uses: grafana/setup-k6-action@v1

- name: Run local k6 / API Smoke testing
uses: grafana/run-k6-action@v1
env:
K6_CLOUD_TOKEN: ${{ secrets.K6_CLOUD_TOKEN }}
K6_CLOUD_PROJECT_ID: ${{ secrets.K6_CLOUD_PROJECT_ID }}
AUTH_TOKEN: ${{ secrets.ADMIN_AUTH_TOKEN }}
with:
path: k6/api-smoke.js

- name: Run local k6 / API Stress testing
uses: grafana/run-k6-action@v1
env:
K6_CLOUD_TOKEN: ${{ secrets.K6_CLOUD_TOKEN }}
K6_CLOUD_PROJECT_ID: ${{ secrets.K6_CLOUD_PROJECT_ID }}
AUTH_TOKEN: ${{ secrets.ADMIN_AUTH_TOKEN }}
with:
path: k6/api-stress.js
1 change: 1 addition & 0 deletions k6/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
29 changes: 29 additions & 0 deletions k6/api-smoke.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import http from 'k6/http';
import { check, sleep } from 'k6';

const paths = [
'/articles',
'/features',
'/projects',
'/articles/@id',
'/articles/@id/comments',
'/articles/@id/recommendations',
];

export default function () {
for (let path of paths) {
const res = http.get(
`https://api.udfn.fr${path.replace('@id', 'NMVntUf1yiwhMkrOftLNB')}`,
{
headers: {
Authorization: `Bearer ${__ENV.AUTH_TOKEN}`
}
}
);
check(res, {
'is status 200': (r) => r.status === 200,
});

sleep(0.1);
}
}
42 changes: 42 additions & 0 deletions k6/api-stress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import http from 'k6/http';
import { check } from 'k6';
import { randomItem } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';

const paths = [
'/articles',
'/features',
'/projects',
'/articles/@id',
'/articles/@id/comments',
'/articles/@id/recommendations',
];

const articles = [
'NMVntUf1yiwhMkrOftLNB',
'4aAkSjsn311n0jwAgNnIvH',
'7rLwY85ICqoRIXKyHeeSGY',
'1UXgm0ZL7D1rysOnysYlrM',
'1yN0AOunIiaLn8nXZc7k6z',
'4EXN4ffKnY92zPpVS1xrVn',
'6J0hWoFCuiuWQtjp3K1mXn',
];

export const options = {
vus: 50,
duration: '1m',
};

export default function () {
const path = randomItem(paths).replace('@id', randomItem(articles));
const res = http.get(
`https://api.udfn.fr${path}`,
{
headers: {
Authorization: `Bearer ${__ENV.AUTH_TOKEN}`
}
}
);
check(res, {
'is status 200': (r) => r.status === 200,
});
}
25 changes: 25 additions & 0 deletions k6/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions k6/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"@types/k6": "^0.54.1",
"k6": "^0.0.0"
}
}

0 comments on commit 7321c07

Please sign in to comment.