Skip to content

Commit d19b004

Browse files
authored
Add k6 performance tests (#3193)
1 parent 4c87da5 commit d19b004

22 files changed

Lines changed: 27746 additions & 229 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ frontends/.yarn
117117

118118
.yarn/*
119119
!.yarn/releases
120+
load_testing/.yarn/*
121+
!load_testing/.yarn/releases
120122

121123
# Typescript
122124
tsconfig.tsbuildinfo

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ repos:
104104
language: node
105105
types_or: [javascript, jsx, ts, tsx]
106106
args: []
107-
exclude: "(node_modules/|.yarn/)"
107+
exclude: "(node_modules/|.yarn/|load_testing/)"
108108
require_serial: false
109109
additional_dependencies:
110110
- eslint@8

docker-compose.services.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ services:
7676
ports:
7777
- "8089:8089"
7878
volumes:
79-
- ./load_testing:/mnt/locust
79+
- ./load_testing_locust:/mnt/locust
8080
command: -f /mnt/locust/locustfile.py --master -H http://nginx:8063 --class-picker
8181
links:
8282
- nginx
@@ -86,7 +86,7 @@ services:
8686
locust-worker:
8787
image: locustio/locust
8888
volumes:
89-
- ./load_testing:/mnt/locust
89+
- ./load_testing_locust:/mnt/locust
9090
command: -f /mnt/locust/locustfile.py --worker --master-host locust
9191
links:
9292
- nginx

load_testing/.stylelintrc.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
extends:
2+
- stylelint-config-standard
3+
rules:
4+
selector-class-pattern:
5+
- (^ck.*)|((^Mui[A-Z-])|^([a-z][a-z0-9]*)((-|__?)[a-z0-9]+)*$) # CKEditor, MUI or kebab-case
6+
- message: "Expected class selector to be kebab-case"
7+
overrides:
8+
- files:
9+
- "**/*.tsx"
10+
- "**/*.ts"
11+
customSyntax: postcss-styled-syntax

load_testing/README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
## Load Testing
1+
## k6 load testing
22

3-
#### Locust
3+
### Usage (Docker)
44

5-
The load testing system uses [Locust](https://docs.locust.io/en/stable/index.html) to run the load tests against the APIs. The entrypoint for this is `locustfile.py`. Locust automatically picks up the testing configuration based on subclassing things, you can read the docs for further details.
5+
```shell
6+
./scripts/k6.sh -e BACKEND_BASE_URL=#### -e FRONTEND_BASE_URL=####
7+
```
68

7-
#### Running
9+
### Usage (local k6)
810

9-
- Add `load-testing` to your `COMPOSE_PROFILES` setting.
10-
- Run `docker compose up` or `docker compose up locust locust-worker` (a more minimal set of services)
11-
- Go to http://localhost:8089/ and you should see the web UI for Locust.
12-
- Set the options you want to test (100 users is reasonable for local tests) and click Start
13-
- If you're testing against a backend that isn't reachable through the `nginx` hostname, you'll need to specify the alternative hostname.
14-
- You can use this to test against deployed environments.
11+
- Install [k6](https://grafana.com/docs/k6/latest/set-up/install-k6/)
12+
13+
```shell
14+
k6 run learn.ts -e BACKEND_BASE_URL=#### -e FRONTEND_BASE_URL=####
15+
```

load_testing/__init__.py

Whitespace-only changes.

load_testing/auth.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { SharedArray } from "k6/data"
2+
3+
export function getAccessToken(): String {
4+
return __ENV.AUTH_ACCESS_TOKEN
5+
}
6+
7+
export const users = new SharedArray("users", function () {
8+
if (!__ENV.USERS_JSON_FILE) {
9+
return []
10+
}
11+
12+
return JSON.parse(open(__ENV.USERS_JSON_FILE))
13+
})
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Params } from "k6/http"
2+
import { BACKEND_BASE_URL } from "../../config.ts"
3+
import { MITLearnAPIClient as V0Client } from "./v0/api.ts"
4+
import { MITLearnAPIClient as V1Client } from "./v1/api.ts"
5+
import { getAccessToken } from "../../auth.ts"
6+
7+
function getCommonRequestParameters(): Params {
8+
const accessToken = getAccessToken()
9+
10+
return {
11+
headers: {
12+
...(!!accessToken ? { Authorization: `Bearer ${accessToken}` } : {}),
13+
},
14+
}
15+
}
16+
17+
export function createV0Client(): V0Client {
18+
return new V0Client({
19+
baseUrl: BACKEND_BASE_URL,
20+
commonRequestParameters: getCommonRequestParameters(),
21+
})
22+
}
23+
export function createV1Client(): V1Client {
24+
return new V1Client({
25+
baseUrl: BACKEND_BASE_URL,
26+
commonRequestParameters: getCommonRequestParameters(),
27+
})
28+
}

0 commit comments

Comments
 (0)