Skip to content

Commit

Permalink
Add basic load test utilities to the application.
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-schilling committed Nov 20, 2024
1 parent 0b1d4ad commit ea0f886
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
14 changes: 14 additions & 0 deletions load_tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Load Tests

This utilizes [k6](https://github.com/grafana/k6) to test our application
under a defined amount of load. You can
[install k6 here](https://grafana.com/docs/k6/latest/get-started/installation/).

These tests aren't promised to work out of the box. You may need
to tweak things future-djangonaut.

To run a test:

```shell
k6 run test_blog.js
```
26 changes: 26 additions & 0 deletions load_tests/basic_plan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import http from "k6/http";
import { check, sleep } from "k6";

// Test configuration
export const options = {
thresholds: {
// Assert that 99% of requests finish within 3000ms.
http_req_duration: ["p(99) < 3000"],
},
// Ramp the number of virtual users up and down
stages: [
{ duration: "30s", target: 15 },
{ duration: "1m", target: 15 },
{ duration: "20s", target: 0 },
],
};

// Simulated user behavior
export function basicPlan(url) {
return function () {
let res = http.get(url);
// Validate response status
check(res, { "status was 200": (r) => r.status == 200 });
sleep(1);
}
}
8 changes: 8 additions & 0 deletions load_tests/test_blog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {options, basicPlan} from "./basic_plan.js";

exports.options = options

const testBlog = basicPlan("https://djangonaut.space/comms/2024/03/08/2024-session-1-mission-debrief/")

// Simulated user behavior
export default testBlog
8 changes: 8 additions & 0 deletions load_tests/test_session.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {options, basicPlan} from "./basic_plan.js";

exports.options = options

const testSession = basicPlan("https://djangonaut.space/sessions/2024-session-2/")

// Simulated user behavior
export default testSession
27 changes: 27 additions & 0 deletions load_tests/test_survey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import http from "k6/http";
import { check, sleep } from "k6";
import {options} from "./basic_plan.js";

// Test configuration
exports.options = options

const PASSWORD = ''; // Reset the password on production

// Simulated user behavior
export default function () {
let res = http.get("https://djangonaut.space/accounts/login/");
check(res, { "status was 200": (r) => r.status == 200 });
res = res.submitForm({
formSelector: 'form',
fields: { username: `load_test${__ITER}`, password: PASSWORD },
});
check(res, { "status was 200": (r) => r.status == 200 });
res = http.get("https://djangonaut.space/survey_response/create/load-test/");
check(res, { "status was 200": (r) => r.status == 200 });
res = res.submitForm({
formSelector: 'form',
fields: { field_survey_1: 'load_test', field_survey_2: 2, field_survey_3: "final value over 100 characters. final value over 100 characters. final value over 100 characters. final value over 100 characters. final value over 100 characters. final value over 100 characters. final value over 100 characters. final value over 100 characters. final value over 100 characters. final value over 100 characters. " },
});
check(res, { "status was 200": (r) => r.status == 200 });
sleep(1);
}

0 comments on commit ea0f886

Please sign in to comment.