-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloadtest.js
More file actions
46 lines (39 loc) · 1.29 KB
/
loadtest.js
File metadata and controls
46 lines (39 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import http from 'k6/http';
import { check } from 'k6';
// Get the APIGW_ENDPOINT environment variable.
const APIGW_ENDPOINT = __ENV.APIGW_ENDPOINT;
if (!APIGW_ENDPOINT) {
throw new Error("Please set the APIGW_ENDPOINT environment variable");
}
export let options = {
vus: 10, // Number of virtual users.
duration: '60s', // Duration of the test.
};
export default function () {
// Get current utc timestamp.
const timestamp = Math.floor(Date.now() / 1000);
// Create unique id for each request.
const id = `${__VU}-${__ITER}`;
// Create a JSON payload.
const data = {
id: id.toString(),
message: "Inbound request",
timestamp: timestamp,
};
const body = JSON.stringify(data);
// Send a POST request to the server.
let response = http.post(
APIGW_ENDPOINT,
body,
{ headers: { 'Content-Type': 'application/json' } },
);
// Check the response.
check(response, {
'status was 200': (r) => r.status == 200,
'transaction time OK': (r) => r.timings.duration < 4000,
});
// If the status code is not 200, print the status code and the body of the response.
if (response.status !== 200) {
console.log(`Response status: ${response.status}, body: ${response.body}`);
}
}