Skip to content

Instantly share code, notes, and snippets.

@tetebueno
Last active April 17, 2024 11:30
Show Gist options
  • Save tetebueno/64ea08aed61ed1772ae7a67c778f64c8 to your computer and use it in GitHub Desktop.
Save tetebueno/64ea08aed61ed1772ae7a67c778f64c8 to your computer and use it in GitHub Desktop.
K6 website test
import http from 'k6/http';
import { check, sleep } from 'k6';
export const URL = 'https://test.k6.io';
export default function () {
let res = http.get(URL);
check(res, {
'resource returns status 200': (r) => r.status === 200,
});
getResources(res);
sleep(1);
}
export function resolveUrl(url, baseUrl) {
if (url.indexOf("/") == 0) {
return baseUrl + url;
}
return url;
}
export function createHeader(baseUrl) {
return {
"Referer": baseUrl
};
}
export function getResources(response) {
const resources = [];
response
.html()
.find('*[href]:not(a)')
.each((index, element) => {
resources.push(element.attributes().href.value);
});
response
.html()
.find('*[src]:not(a)')
.each((index, element) => {
resources.push(element.attributes().src.value);
});
const responses = http.batch(
resources.map((r) => {
return ['GET', resolveUrl(r, response.url), null, { headers: createHeader(response.url) }];
})
);
responses.forEach(() => {
check(response, {
'resource returns status 200': (r) => r.status === 200,
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment