Skip to content

Commit 8460d87

Browse files
committed
Create a k6 load test
1 parent dc25147 commit 8460d87

File tree

6 files changed

+140
-2
lines changed

6 files changed

+140
-2
lines changed

.eslintrc.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ module.exports = {
6262
rules: {},
6363
},
6464
{
65-
files: ['**/*.cjs'],
65+
files: ['**/*.cjs', '**/*.js'],
6666
rules: {
6767
'@typescript-eslint/no-var-requires': 'off',
6868
},
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
3+
set -eu
4+
5+
docker container run \
6+
--name "graphana-k6" \
7+
--interactive \
8+
--tty \
9+
--rm \
10+
--user "k6" \
11+
--workdir "/home/k6" \
12+
--volume "${PWD}:/home/k6" \
13+
--entrypoint "/bin/sh" \
14+
"grafana/k6:0.49.0"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
3+
set -eu
4+
5+
docker container run \
6+
--name "graphana-k6" \
7+
--interactive \
8+
--tty \
9+
--rm \
10+
--user "root" \
11+
--workdir "/home/k6" \
12+
--volume "${PWD}:/home/k6" \
13+
"grafana/k6:0.49.0" \
14+
$@
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// @ts-expect-error
2+
import http from 'k6/http';
3+
// @ts-expect-error
4+
import { sleep } from 'k6';
5+
6+
export const options = {
7+
// A number specifying the number of VUs to run concurrently.
8+
vus: 10,
9+
// A string specifying the total duration of the test run.
10+
duration: '30s',
11+
12+
// The following section contains configuration options for execution of this
13+
// test script in Grafana Cloud.
14+
//
15+
// See https://grafana.com/docs/grafana-cloud/k6/get-started/run-cloud-tests-from-the-cli/
16+
// to learn about authoring and running k6 test scripts in Grafana k6 Cloud.
17+
//
18+
// ext: {
19+
// loadimpact: {
20+
// // The ID of the project to which the test is assigned in the k6 Cloud UI.
21+
// // By default tests are executed in default project.
22+
// projectID: "",
23+
// // The name of the test in the k6 Cloud UI.
24+
// // Test runs with the same name will be grouped.
25+
// name: "script.js"
26+
// }
27+
// },
28+
29+
// Uncomment this section to enable the use of Browser API in your tests.
30+
//
31+
// See https://grafana.com/docs/k6/latest/using-k6-browser/running-browser-tests/ to learn more
32+
// about using Browser API in your test scripts.
33+
//
34+
// scenarios: {
35+
// // The scenario name appears in the result summary, tags, and so on.
36+
// // You can give the scenario any name, as long as each name in the script is unique.
37+
// ui: {
38+
// // Executor is a mandatory parameter for browser-based tests.
39+
// // Shared iterations in this case tells k6 to reuse VUs to execute iterations.
40+
// //
41+
// // See https://grafana.com/docs/k6/latest/using-k6/scenarios/executors/ for other executor types.
42+
// executor: 'shared-iterations',
43+
// options: {
44+
// browser: {
45+
// // This is a mandatory parameter that instructs k6 to launch and
46+
// // connect to a chromium-based browser, and use it to run UI-based
47+
// // tests.
48+
// type: 'chromium',
49+
// },
50+
// },
51+
// },
52+
// }
53+
};
54+
55+
// The function that defines VU logic.
56+
//
57+
// See https://grafana.com/docs/k6/latest/examples/get-started-with-k6/ to learn more
58+
// about authoring k6 scripts.
59+
//
60+
export default function () {
61+
http.get('https://test.k6.io');
62+
sleep(1);
63+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// @ts-expect-error
2+
import http from 'k6/http';
3+
// @ts-expect-error
4+
import { check, sleep } from 'k6';
5+
// @ts-expect-error
6+
import { Rate } from 'k6/metrics';
7+
// @ts-expect-error
8+
import { htmlReport } from 'https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js';
9+
10+
// Define the failure rate
11+
let failureRate = new Rate('failed_requests');
12+
13+
export let options = {
14+
stages: [
15+
{ duration: '10s', target: 10 }, // ramp up to 5 requests per second over 10 seconds
16+
{ duration: '10s', target: 10 }, // stay at 5 requests per second for 10 seconds
17+
{ duration: '10s', target: 1 }, // ramp down to 1 request per second over 10 seconds
18+
],
19+
thresholds: {
20+
'http_req_duration{scenario:default}': ['p(95)<5000'], // Test fails if 95th percentile of request durations is higher than 5 seconds
21+
failed_requests: ['rate<0.01'], // Fail if more than 1% of requests fail
22+
},
23+
};
24+
25+
export default function () {
26+
let response = http.get('http://staging-code-snippet-sharing.nodeexx.com');
27+
28+
// Check each response for a 200 status code
29+
const checkRes = check(response, {
30+
// @ts-expect-error
31+
'status is 200': (r) => r.status === 200,
32+
});
33+
34+
// Track the failed requests
35+
failureRate.add(!checkRes);
36+
37+
// Since the test doesn't precisely control the request rate to exactly match the target,
38+
// sleeping for a short duration helps to regulate the execution pace.
39+
sleep(1);
40+
}
41+
42+
// @ts-expect-error
43+
export function handleSummary(data) {
44+
return {
45+
'summary.html': htmlReport(data),
46+
};
47+
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"./.*.ts",
2727
"./scripts/app/**/*.js",
2828
"./scripts/stack/**/*.js",
29-
"./scripts/ci/**/*.js"
29+
"./scripts/testing/**/*.js"
3030
],
3131
"compilerOptions": {
3232
"useDefineForClassFields": true,

0 commit comments

Comments
 (0)