Skip to content

Instantly share code, notes, and snippets.

@bubbajoe
Forked from tetebueno/script.js
Created March 8, 2024 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bubbajoe/56f5bb0f678c6cf38361f6b45c8a1ad3 to your computer and use it in GitHub Desktop.
Save bubbajoe/56f5bb0f678c6cf38361f6b45c8a1ad3 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