Skip to content

Instantly share code, notes, and snippets.

@robingustafsson
Created February 22, 2018 11:01
Show Gist options
  • Save robingustafsson/44d31431dd99899a7afee7a2c0a48fe6 to your computer and use it in GitHub Desktop.
Save robingustafsson/44d31431dd99899a7afee7a2c0a48fe6 to your computer and use it in GitHub Desktop.
k6 modules example
import { check, group, sleep } from "k6";
import http from "k6/http";
import { options as optionsFeaturesPage, pageOptions as pageOptionsFeaturesPage, testFeaturesPage } from "./units/features.js";
import { options as optionsIndexPage, pageOptions as pageOptionsIndexPage, testIndexPage } from "./units/index.js";
import { options as optionsPricingPage, pageOptions as pageOptionsPricingPage, testPricingPage } from "./units/pricing.js";
// Options
export let options = {
stages: [
{ duration: "20s", target: 10 },
{ duration: "20s" },
{ duration: "20s", target: 0 }
],
thresholds: Object.assign(optionsIndexPage.thresholds,
optionsFeaturesPage.thresholds,
optionsPricingPage.thresholds),
ext: {
loadimpact: {
name: "marketing site"
}
}
};
// Scenario
export default function() {
group("Front page", function() {
testIndexPage();
sleep(pageOptionsIndexPage.sleepTime);
});
group("Pricing page", function() {
testPricingPage();
sleep(pageOptionsPricingPage.sleepTime);
});
group("Features page", function() {
testFeaturesPage();
sleep(pageOptionsFeaturesPage.sleepTime);
});
};
import { check, sleep } from "k6";
import http from "k6/http";
// Constants
const baseURL = __ENV.BASE_URL || "https://loadimpact.com";
const mainURL = `${baseURL}/features`;
const avgTimeOnPageSecs = 10;
const thresholdP95 = 100;
// Options
let pageThresholds = {};
pageThresholds[`http_req_duration{url:${mainURL}}`] = [`p(95)<${thresholdP95}`];
export let options = {
thresholds: pageThresholds
};
export let pageOptions = {
sleepTime: avgTimeOnPageSecs
};
// Tests
export function testFeaturesPage() {
let res = http.get(mainURL);
check(res, {
"is status 200": (r) => r.status === 200,
"is header present": (r) => r.body.indexOf("Ship better performing apps and sites, faster.") !== -1
});
};
export default function() {
testFeaturesPage();
};
import { check, group, sleep } from "k6";
import http from "k6/http";
// Constants
const baseURL = __ENV.BASE_URL || "https://loadimpact.com";
const mainURL = `${baseURL}/`;
const avgTimeOnPageSecs = 10;
const thresholdP95 = 100;
const staticAssets = [
`${baseURL}/static/css/6f3d33c6d285.css`,
`${baseURL}/static/js/4b766e34d89f.js`,
];
const thresholdStaticAssetsP95 = 100;
// Options
let pageThresholds = {};
pageThresholds[`http_req_duration{url:${mainURL}}`] = [`p(95)<${thresholdP95}`];
pageThresholds["http_req_duration{staticAsset:yes,page:index}"] = [`p(95)<${thresholdStaticAssetsP95}`];
export let options = {
thresholds: pageThresholds
};
export let pageOptions = {
sleepTime: avgTimeOnPageSecs
};
// Tests
export function testIndexPage() {
let res = http.get(mainURL);
check(res, {
"is status 200": (r) => r.status === 200,
"is header present": (r) => r.body.indexOf("Ship better performing apps and sites, faster.") !== -1
});
group("Static assets", function() {
for (var i = 0; i < staticAssets.length; i++) {
let res = http.get(staticAssets[i], { tags: { staticAsset: "yes", page: "index" } });
check(res, {
"is status 200": (r) => r.status === 200
});
}
});
};
export default function() {
testIndexPage();
};
import { check, sleep } from "k6";
import http from "k6/http";
// Constants
const baseURL = __ENV.BASE_URL || "https://loadimpact.com";
const mainURL = `${baseURL}/pricing`;
const avgTimeOnPageSecs = 10;
const thresholdP95 = 100;
// Options
let pageThresholds = {};
pageThresholds[`http_req_duration{url:${mainURL}}`] = [`p(95)<${thresholdP95}`];
export let options = {
thresholds: pageThresholds
};
export let pageOptions = {
sleepTime: avgTimeOnPageSecs
};
// Tests
export function testPricingPage() {
let res = http.get(mainURL);
check(res, {
"is status 200": (r) => r.status === 200,
"is buy now cta present": (r) => r.body.indexOf("Buy now") !== -1
});
};
export default function() {
testPricingPage();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment