-
Notifications
You must be signed in to change notification settings - Fork 0
/
k6loadtest.js
140 lines (120 loc) · 3.77 KB
/
k6loadtest.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { SharedArray } from "k6/data";
import { sleep, check } from "k6";
import http from "k6/http";
import { exec, scenario } from "k6/execution";
import {
tagWithCurrentStageIndex,
tagWithCurrentStageProfile,
} from "https://jslib.k6.io/k6-utils/1.3.0/index.js";
import { URL } from "https://jslib.k6.io/url/1.0.0/index.js";
const offset = __ENV.SCENARIO_OFFSET ? parseInt(__ENV.SCENARIO_OFFSET) : 0;
const urlPrefix = "https://api.dwebsearch.org";
const sleepAfter = 60;
const visits = new SharedArray("visits", function () {
// All heavy work (opening and processing big files for example) should be done inside here.
// This way it will happen only once and the result will be shared between all VUs, saving time and memory.
const f = JSON.parse(open("./visits.json"));
if (offset) {
console.log(`Using scenario offset of ${offset}.`);
}
console.log("Read visits:", f.length);
return f;
});
export const options = {
scenarios: {
ramp_vus: {
executor: "ramping-vus",
startVUs: 2800,
stages: [
{ duration: "4m", target: 2800 },
{ duration: "4m", target: 3200 },
{ duration: "2m", target: 3200 },
{ duration: "4m", target: 3600 },
{ duration: "2m", target: 3600 },
],
},
},
thresholds: {
"checks{check:cache_hit}": ["rate>0.1"],
"checks{check:status_200_or_308}": ["rate>0.9"],
"checks{check:JSON}": ["rate>0.9"],
"checks{check:gzip}": ["rate>0.9"],
"checks{check:etag}": ["rate>0.9"],
"checks{check:cache_headers}": ["rate>0.9"],
http_req_failed: ["rate<0.1"],
http_req_duration: ["p(90)<1000"],
},
discardResponseBodies: true,
};
function getNameFromURL(url) {
try {
const urlObj = new URL(url);
const pathSplit = urlObj.pathname.split("/");
if (pathSplit.length < 3) throw new Error("Less than 3 elements in path.");
return pathSplit[2];
} catch (e) {
console.error("Error getting name from URL:", e);
return "";
}
}
function getURLFromPath(path) {
if (!path) throw new Error("Empty path");
return urlPrefix + path;
}
export default function () {
// Pick a new batch for every iteration.
const visitIteration = scenario.iterationInTest + offset;
// Abort test on out of bound condition.
if (visitIteration > visits.length - 1) {
exec.test.abort("Out of visits for iteration.");
}
const visit = visits[visitIteration];
for (const batch of visit) {
const requests = batch.paths
.filter((p) => p) // Filter empty paths.
.map(function (path) {
const url = getURLFromPath(path);
return {
method: "GET",
url: url,
params: {
tags: {
name: getNameFromURL(url),
},
headers: {
"Accept-Encoding": "gzip",
},
},
};
});
tagWithCurrentStageIndex();
tagWithCurrentStageProfile();
sleep(batch.seconds_before);
const responses = http.batch(requests);
for (const response of responses) {
check(
response,
{
cache_hit: (r) =>
"X-Cache-Status" in r.headers &&
r.headers["X-Cache-Status"] === "HIT",
status_200_or_308: (r) => r.status === 200 || r.status === 308,
JSON: (r) =>
"Content-Type" in r.headers &&
r.headers["Content-Type"] === "application/json; charset=utf-8",
gzip: (r) =>
"Content-Encoding" in r.headers &&
r.headers["Content-Encoding"] === "gzip",
cache_headers: (r) =>
"Cache-Control" in r.headers &&
"Expires" in r.headers &&
"Etag" in r.headers,
},
{
name: getNameFromURL(response.request.url),
}
);
}
}
sleep(sleepAfter * Math.random());
}