-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy path05.custom-metrics.js
94 lines (84 loc) · 2.35 KB
/
05.custom-metrics.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { browser } from "k6/browser";
import { check } from "k6";
import { Trend } from "k6/metrics";
const BASE_URL = __ENV.BASE_URL || "http://localhost:3333";
export const options = {
scenarios: {
pizzaRecommendations: {
executor: "shared-iterations",
options: {
browser: {
type: "chromium",
},
},
exec: "pizzaRecommendations",
},
admin: {
executor: "shared-iterations",
options: {
browser: {
type: "chromium",
},
},
exec: "admin",
},
},
thresholds: {
browser_web_vital_fcp: ["p(95) < 1000"],
browser_web_vital_lcp: ["p(95) < 2000"],
},
};
const myTrend = new Trend("totalActionTime");
export async function admin() {
let checkData;
const page = await browser.newPage();
try {
await page.goto(`${BASE_URL}/admin`);
await page.locator('button[type="submit"]').click();
checkData = await page.locator('//*[text()="Logout"]').textContent();
check(page, {
"logout button text": checkData == "Logout",
});
} finally {
await page.close();
}
}
export async function pizzaRecommendations() {
let checkData;
const page = await browser.newPage();
try {
await page.goto(BASE_URL);
await page.evaluate(() => window.performance.mark('page-visit'));
checkData = await page.locator("h1").textContent();
check(page, {
header: checkData == "Looking to break out of your pizza routine?",
});
await page.locator('//button[. = "Pizza, Please!"]').click();
await page.waitForTimeout(500);
await page.screenshot({ path: "screenshot.png" });
page.evaluate(() => window.performance.mark("recommendations-returned"));
checkData = await page.locator("div#recommendations").textContent();
check(page, {
recommendation: checkData != "",
});
//Get time difference between visiting the page and pizza recommendations returned
await page.evaluate(() =>
window.performance.measure(
"total-action-time",
"page-visit",
"recommendations-returned"
)
);
const totalActionTime = await page.evaluate(
() =>
JSON.parse(
JSON.stringify(
window.performance.getEntriesByName("total-action-time")
)
)[0].duration
);
myTrend.add(totalActionTime);
} finally {
await page.close();
}
}