Skip to content

Commit 57bc8f5

Browse files
committed
Latest
1 parent 9672dc3 commit 57bc8f5

7 files changed

Lines changed: 124 additions & 10 deletions

File tree

load_testing/auth.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import { SharedArray } from "k6/data"
22

33
export type AuthCredential = {
4-
username: string
4+
email: string
55
password: string
66
}
77

88
export function getAccessToken(): string | null {
99
return __ENV.AUTH_ACCESS_TOKEN
1010
}
1111

12+
export function hasAccessToken(): boolean {
13+
return !getAccessToken()
14+
}
15+
1216
function _validate_credentials(credentials) {
1317
if (typeof credentials !== "array") {
1418
throw Error("Expected an array of credentials")

load_testing/backend/test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import { createV0Client, createV1Client } from "./client/client.ts"
99
import { NewsEventsListParams } from "./client/v0/api.schemas.ts"
1010
import { FeaturedListParams } from "./client/v1/api.schemas.ts"
11+
import { hasAccessToken } from "../auth.ts"
1112

1213
export function testBackend() {
1314
group("api", function () {
@@ -57,6 +58,14 @@ export function testBackend() {
5758
})
5859
})
5960

61+
res = client.usersMeRetrieve()
62+
63+
check(res, {
64+
"is status 200": (r) => r.response.status === 200,
65+
"expected auth state": (r) =>
66+
r.response.json("is_authenticated") === hasAccessToken(),
67+
})
68+
6069
delete exec.vu.metrics.tags.apiVersion
6170
})
6271

load_testing/config.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { NewBrowserContextOptions } from "k6/browser"
33
export const BACKEND_BASE_URL: string = __ENV.BACKEND_BASE_URL
44
export const FRONTEND_BASE_URL: string = __ENV.FRONTEND_BASE_URL
55

6+
export const IGNORE_HTTPS_ERRORS: boolean =
7+
(__ENV.IGNORE_HTTPS_ERRORS || "false").toLowerCase() == "true"
8+
69
export const BROWSER_CONTEXT_OPTIONS: NewBrowserContextOptions = {
7-
ignoreHTTPSErrors: Object.hasOwn(__ENV, "BROWSER_IGNORE_HTTPS_ERRORS")
8-
? __ENV.BROWSER_IGNORE_HTTPS_ERRORS
9-
: false,
10+
ignoreHTTPSErrors: IGNORE_HTTPS_ERRORS,
1011
}

load_testing/frontend/test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async function login(page: Page) {
4646
const credential: AuthCredential = randomItem(credentials)
4747

4848
if (!!credential) {
49-
log.debug("Skipping login because no credentials provided")
49+
console.debug("Skipping login because no credentials provided")
5050
return
5151
}
5252

@@ -64,14 +64,18 @@ async function loginKeycloak(page: Page, credential: AuthCredential) {
6464
)
6565

6666
const credentialnameInput = await page.locator("input#username")
67-
await credentialnameInput.type(user.email)
67+
await credentialnameInput.type(credential.email)
6868
await page.locator("button#kc-login").click()
6969

7070
const passwordInput = await page.locator("input#password")
7171
await passwordInput.type(credential.password)
7272
await page.locator("button#kc-login").click()
7373
}
7474

75+
async function dashboard(page: Page) {
76+
await page.goto(`${FRONTEND_BASE_URL}/dashboard`)
77+
}
78+
7579
export async function testFrontend() {
7680
const page = await browser.newPage(BROWSER_CONTEXT_OPTIONS)
7781

@@ -83,6 +87,7 @@ export async function testFrontend() {
8387
await departments(page)
8488
await units(page)
8589
await login(page)
90+
await dashboard(page)
8691
} finally {
8792
await page.close()
8893
}

load_testing/learn.average-load.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { IGNORE_HTTPS_ERRORS } from "./config.ts"
2+
3+
export { testBackend } from "./backend/test.ts"
4+
export { testFrontend } from "./frontend/test.ts"
5+
6+
const MAX_VUS = 100
7+
const BROWSER_VU_SHARE = 0.5
8+
const BACKEND_VU_SHARE = 1 - BROWSER_VU_SHARE
9+
10+
const BROWSER_VUS = Math.floor(MAX_VUS * BROWSER_VU_SHARE)
11+
const BACKEND_VUS = Math.floor(MAX_VUS * BACKEND_VU_SHARE)
12+
13+
export const options = {
14+
scenarios: {
15+
browser: {
16+
exec: "testFrontend",
17+
executor: "ramping-vus",
18+
options: {
19+
browser: {
20+
type: "chromium",
21+
},
22+
},
23+
stages: [
24+
{ duration: "5m", target: BROWSER_VUS },
25+
{ duration: "30m", target: BROWSER_VUS },
26+
{ duration: "5m", target: 0 },
27+
],
28+
},
29+
backend: {
30+
exec: "testBackend",
31+
executor: "ramping-vus",
32+
stages: [
33+
{ duration: "5m", target: BACKEND_VUS },
34+
{ duration: "30m", target: BACKEND_VUS },
35+
{ duration: "5m", target: 0 },
36+
],
37+
},
38+
},
39+
thresholds: {
40+
// the rate of successful checks should be higher than 90%
41+
checks: ["rate>0.9"],
42+
},
43+
insecureSkipTLSVerify: IGNORE_HTTPS_ERRORS,
44+
}
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1+
import { IGNORE_HTTPS_ERRORS } from "./config.ts"
2+
13
export { testBackend } from "./backend/test.ts"
24
export { testFrontend } from "./frontend/test.ts"
35

6+
const MAX_VUS = 4
7+
const BROWSER_VU_SHARE = 0.5
8+
const BACKEND_VU_SHARE = 1 - BROWSER_VU_SHARE
9+
410
export const options = {
511
scenarios: {
612
browser: {
713
exec: "testFrontend",
814
executor: "constant-vus",
9-
vus: 10,
10-
duration: "30s",
15+
vus: Math.floor(MAX_VUS * BROWSER_VU_SHARE),
16+
duration: "1m",
1117
options: {
1218
browser: {
1319
type: "chromium",
@@ -17,12 +23,13 @@ export const options = {
1723
backend: {
1824
exec: "testBackend",
1925
executor: "constant-vus",
20-
vus: 10,
21-
duration: "30s",
26+
vus: Math.floor(MAX_VUS * BACKEND_VU_SHARE),
27+
duration: "1m",
2228
},
2329
},
2430
thresholds: {
2531
// the rate of successful checks should be higher than 90%
2632
checks: ["rate>0.9"],
2733
},
34+
insecureSkipTLSVerify: IGNORE_HTTPS_ERRORS,
2835
}

load_testing/learn.stress.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { IGNORE_HTTPS_ERRORS } from "./config.ts"
2+
3+
export { testBackend } from "./backend/test.ts"
4+
export { testFrontend } from "./frontend/test.ts"
5+
6+
const MAX_VUS = 200
7+
const BROWSER_VU_SHARE = 0.5
8+
const BACKEND_VU_SHARE = 1 - BROWSER_VU_SHARE
9+
10+
const BROWSER_VUS = Math.floor(MAX_VUS * BROWSER_VU_SHARE)
11+
const BACKEND_VUS = Math.floor(MAX_VUS * BACKEND_VU_SHARE)
12+
13+
export const options = {
14+
scenarios: {
15+
browser: {
16+
exec: "testFrontend",
17+
executor: "ramping-vus",
18+
options: {
19+
browser: {
20+
type: "chromium",
21+
},
22+
},
23+
stages: [
24+
{ duration: "5m", target: BROWSER_VUS },
25+
{ duration: "30m", target: BROWSER_VUS },
26+
{ duration: "5m", target: 0 },
27+
],
28+
},
29+
backend: {
30+
exec: "testBackend",
31+
executor: "ramping-vus",
32+
stages: [
33+
{ duration: "5m", target: BACKEND_VUS },
34+
{ duration: "30m", target: BACKEND_VUS },
35+
{ duration: "5m", target: 0 },
36+
],
37+
},
38+
},
39+
thresholds: {
40+
// the rate of successful checks should be higher than 90%
41+
checks: ["rate>0.9"],
42+
},
43+
insecureSkipTLSVerify: IGNORE_HTTPS_ERRORS,
44+
}

0 commit comments

Comments
 (0)