Sample script
import http from "k6/http";
import { check, group, sleep } from "k6";
import { Counter, Rate, Trend } from "k6/metrics";
import { randomIntBetween } from "https://jslib.k6.io/k6-utils/1.0.0/index.js";
const loginData = JSON.parse(open("./users.json"));
export let options = {
stages: [
{ target: 200, duration: "1m" },
{ target: 200, duration: "3m" },
{ target: 0, duration: "1m" }
],
thresholds: {
"http_req_duration": ["p(95)<500"],
"http_req_duration{staticAsset:yes}": ["p(95)<100"],
"check_failure_rate": ["rate<0.3"]
},
ext: {
loadimpact: {
name: "Insights Demo with Cloud Execution",
distribution: {
scenarioLabel1: { loadZone: "amazon:us:ashburn", percent: 50 },
scenarioLabel2: { loadZone: "amazon:ie:dublin", percent: 50 }
}
}
}
};
let successfulLogins = new Counter("successful_logins");
let checkFailureRate = new Rate("check_failure_rate");
let timeToFirstByte = new Trend("time_to_first_byte", true);
export default function() {
group("Front page", function() {
let res = null;
if (__ENV.URL_ALERT) {
res = http.get("http://test.k6.io/?ts=" + Math.round(randomIntBetween(1,2000)));
} else {
res = http.get("http://test.k6.io/?ts=" + Math.round(randomIntBetween(1,2000)), { tags: { name: "http://test.k6.io/ Aggregated"}});
}
let checkRes = check(res, {
"Homepage body size is 11026 bytes": (r) => r.body.length === 11026,
"Homepage welcome header present": (r) => r.body.indexOf("Welcome to the k6.io demo site!") !== -1
});
checkFailureRate.add(!checkRes);
timeToFirstByte.add(res.timings.waiting, { ttfbURL: res.url });
group("Static assets", function() {
let res = http.batch([
["GET", "http://test.k6.io/static/css/site.css", {}, { tags: { staticAsset: "yes" } }],
["GET", "http://test.k6.io/static/js/prisms.js", {}, { tags: { staticAsset: "yes" } }]
]);
checkRes = check(res[0], {
"Is stylesheet 4859 bytes?": (r) => r.body.length === 4859,
});
checkFailureRate.add(!checkRes);
timeToFirstByte.add(res[0].timings.waiting, { ttfbURL: res[0].url, staticAsset: "yes" });
timeToFirstByte.add(res[1].timings.waiting, { ttfbURL: res[1].url, staticAsset: "yes" });
});
});
sleep(10);
group("Login", function() {
let res = http.get("http://test.k6.io/my_messages.php");
let checkRes = check(res, {
"Users should not be auth'd. Is unauthorized header present?": (r) => r.body.indexOf("Unauthorized") !== -1
});
const vars = {};
vars["csrftoken"] = res
.html()
.find("input[name=csrftoken]")
.first()
.attr("value");
checkFailureRate.add(!checkRes);
let position = Math.floor(Math.random()*loginData.users.length);
let credentials = loginData.users[position];
res = http.post("http://test.k6.io/login.php", { login: credentials.username, password: credentials.password, redir: '1', csrftoken: `${vars["csrftoken"]}` });
checkRes = check(res, {
"is logged in welcome header present": (r) => r.body.indexOf("Welcome, admin!") !== -1
});
if (checkRes) {
successfulLogins.add(1);
}
checkFailureRate.add(!checkRes, { page: "login" });
timeToFirstByte.add(res.timings.waiting, { ttfbURL: res.url });
sleep(10);
});
}