Skip to content

Commit ca7cf64

Browse files
authored
Added: k6 stress test (#58)
1 parent 47b385e commit ca7cf64

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed

k6/index.js

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import http from 'k6/http';
2+
import { check, sleep } from 'k6';
3+
import exec from 'k6/execution';
4+
5+
const vusCount = 500;
6+
const candidatesCount = 10;
7+
const gradesCount = 7;
8+
const url = __ENV["HOSTNAME"] || "http://localhost:8000";
9+
10+
export const options = {
11+
stages: [
12+
{ duration: '30s', target: vusCount },
13+
{ duration: '30s', target: vusCount },
14+
{ duration: '30s', target: 0 },
15+
],
16+
}
17+
18+
export async function setup() {
19+
const isResticted = true;
20+
const votersCount = isResticted ? vusCount : 0;
21+
22+
const election = {
23+
name: "Test",
24+
hide_results: true,
25+
restricted: isResticted,
26+
grades: [],
27+
num_voters: votersCount,
28+
candidates: []
29+
}
30+
31+
for (let i = 0; i < candidatesCount; ++i) {
32+
election.candidates.push({
33+
name: `Candidate ${i}`,
34+
description: "",
35+
image: ""
36+
});
37+
}
38+
39+
for (let i = 0; i < gradesCount; ++i) {
40+
election.grades.push({
41+
name: `Grade ${i}`,
42+
value: i,
43+
});
44+
}
45+
46+
const payload = await http.post(`${url}/elections`, JSON.stringify(election), {
47+
headers: {
48+
'Content-Type': 'application/json',
49+
'Accept': 'application/json',
50+
},
51+
});
52+
53+
if (payload.status < 200 || payload.status >= 300) {
54+
throw new Error(`Error creating election: ${payload}`);
55+
}
56+
57+
const result = JSON.parse(payload.body);
58+
59+
if ("details" in result)
60+
throw new Error(`Error creating election: ${payload.details}`);
61+
62+
return result;
63+
}
64+
65+
export default async function (data) {
66+
const index = exec.vu.idInTest-1;
67+
const token = data.invites != null && data.invites.length > 0 ? data.invites[index] : undefined;
68+
69+
const electionPayload = await http.get(`${url}/elections/${data.ref}`);
70+
71+
check(electionPayload, {
72+
'GET elections returns status 200': (r) => r.status === 200,
73+
});
74+
75+
if (electionPayload.status < 200 || electionPayload.status >= 300) {
76+
console.log("Fail to get election");
77+
sleep(7);
78+
return;
79+
}
80+
81+
const election = JSON.parse(electionPayload.body);
82+
83+
// previous ballots is requested to identify if user already voted (context of a restricted election)
84+
if (token != undefined) {
85+
const ballotsPayload = await http.get(`${url}/ballots`, {
86+
headers: {
87+
"Authorization":`Bearer ${token}`,
88+
'Accept': 'application/json',
89+
}
90+
});
91+
92+
// first time = should be empty = 404
93+
const expectedState = exec.vu.iterationInInstance === 0 ? 404 : 200;
94+
95+
check(ballotsPayload, {
96+
'GET ballots returns status 200': (r) => r.status === expectedState,
97+
});
98+
}
99+
100+
sleep(7);
101+
102+
const grades = election.grades;
103+
const candidates = election.candidates;
104+
const votes = [];
105+
106+
for (let i = 0; i < candidates.length; ++i) {
107+
votes.push({
108+
candidate_id: candidates[i].id,
109+
grade_id: grades[Math.floor(Math.random() * grades.length)].id,
110+
});
111+
}
112+
113+
if (token != null) {
114+
const ballotsPayload = await http.put(`${url}/ballots`, JSON.stringify({
115+
votes: votes
116+
}), {
117+
headers: {
118+
"Authorization":`Bearer ${token}`,
119+
'Content-Type': 'application/json',
120+
'Accept': 'application/json',
121+
}
122+
});
123+
124+
if (ballotsPayload.status != 200)
125+
console.log(ballotsPayload.body);
126+
127+
check(ballotsPayload, {
128+
'PUT ballots returns status 200': (r) => r.status === 200,
129+
});
130+
}
131+
else {
132+
const ballotsPayload = http.post(`${url}/ballots`, JSON.stringify({
133+
votes: votes,
134+
election_ref: election.ref
135+
}), {
136+
headers: {
137+
'Content-Type': 'application/json',
138+
'Accept': 'application/json',
139+
}
140+
});
141+
142+
check(ballotsPayload, {
143+
'POST ballots returns status 200': (r) => r.status === 200,
144+
});
145+
}
146+
147+
sleep(10);
148+
}

0 commit comments

Comments
 (0)