2022-09-13 13:59:54 +02:00
|
|
|
import { randomSeed, check } from 'k6';
|
2022-07-12 14:01:54 +02:00
|
|
|
import http from 'k6/http';
|
|
|
|
import { SharedArray } from 'k6/data';
|
|
|
|
import exec from 'k6/execution';
|
2022-10-05 08:25:16 +02:00
|
|
|
import { Trend } from 'k6/metrics';
|
2022-07-12 14:01:54 +02:00
|
|
|
|
|
|
|
export let options = {
|
|
|
|
discardResponseBodies: true,
|
|
|
|
noConnectionReuse: false,
|
|
|
|
|
|
|
|
vus: 20,
|
|
|
|
iterations: 10000,
|
|
|
|
};
|
|
|
|
|
|
|
|
randomSeed(42)
|
|
|
|
|
|
|
|
const urls = new SharedArray('urls', function () {
|
2022-09-13 13:59:54 +02:00
|
|
|
const urls_path = __ENV.URLS_PATH || './urls.json'
|
|
|
|
let data = JSON.parse(open(urls_path));
|
2022-07-13 10:52:45 +02:00
|
|
|
|
|
|
|
const groups = (__ENV.URL_GROUPS || "").split(",").filter((g) => g != "")
|
|
|
|
if (groups.length > 0) {
|
|
|
|
data = data.filter((d) => groups.includes(d.group))
|
|
|
|
}
|
2022-07-12 14:01:54 +02:00
|
|
|
|
2022-09-13 13:59:54 +02:00
|
|
|
const url_prefix = __ENV.URL_PREFIX || "http://localhost:8082/unsafe"
|
|
|
|
|
2022-07-12 14:01:54 +02:00
|
|
|
let unshuffled = [];
|
|
|
|
data.forEach((e) => {
|
2022-09-13 13:59:54 +02:00
|
|
|
let url = url_prefix + e.url
|
2022-07-12 14:01:54 +02:00
|
|
|
let weight = e.weight || 1
|
|
|
|
|
|
|
|
for (var i = 0; i < weight; i++) {
|
2022-10-05 08:25:16 +02:00
|
|
|
unshuffled.push({url, group: e.group})
|
2022-07-12 14:01:54 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
let shuffled = unshuffled
|
|
|
|
.map(value => ({ value, sort: Math.random() }))
|
|
|
|
.sort((a, b) => a.sort - b.sort)
|
|
|
|
.map(({ value }) => value)
|
|
|
|
|
|
|
|
return shuffled;
|
|
|
|
});
|
|
|
|
|
2022-07-13 10:52:45 +02:00
|
|
|
if (urls.length == 0) {
|
|
|
|
throw "URLs list is empty"
|
|
|
|
}
|
|
|
|
|
2022-10-05 08:25:16 +02:00
|
|
|
let group_durations = [...new Set(urls.map(url => url.group))]
|
|
|
|
.reduce((trends, group) => {
|
|
|
|
trends[group] = new Trend(`http_req_duration_${group}`, true);
|
|
|
|
return trends;
|
|
|
|
}, {});
|
|
|
|
|
2023-05-03 17:21:17 +02:00
|
|
|
let group_sizes = [...new Set(urls.map(url => url.group))]
|
|
|
|
.reduce((trends, group) => {
|
|
|
|
trends[group] = new Trend(`http_res_body_size_${group}`, false);
|
|
|
|
return trends;
|
|
|
|
}, {});
|
|
|
|
|
2022-07-12 14:01:54 +02:00
|
|
|
export default function() {
|
2022-10-05 08:25:16 +02:00
|
|
|
const url = urls[exec.scenario.iterationInTest % urls.length]
|
|
|
|
const res = http.get(url.url);
|
2022-09-13 13:59:54 +02:00
|
|
|
check(res, {
|
|
|
|
'is status 200': (r) => r.status === 200,
|
|
|
|
});
|
2022-10-05 08:25:16 +02:00
|
|
|
group_durations[url.group].add(res.timings.duration);
|
2023-05-03 17:21:17 +02:00
|
|
|
|
|
|
|
const body_size = Math.round(parseInt(res.headers["Content-Length"]) / 10.24) / 100;
|
|
|
|
group_sizes[url.group].add(body_size);
|
2022-07-12 14:01:54 +02:00
|
|
|
}
|