-
Notifications
You must be signed in to change notification settings - Fork 15
/
07.hybrid.js
126 lines (111 loc) · 3.24 KB
/
07.hybrid.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { browser } from "k6/experimental/browser";
import { check, sleep } from "k6";
import http from "k6/http";
import { Trend } from "k6/metrics";
import { LoginPage } from "./pages/login-page.js";
import { RecommendationsPage } from "./pages/recommendations-page.js";
import { PageUtils } from "./pages/page-utils.js";
const BASE_URL = __ENV.BASE_URL || "http://localhost:3333";
export const options = {
scenarios: {
smoke: {
exec: "getPizza",
executor: "constant-vus",
vus: 1,
duration: "10s",
},
stress: {
exec: "getPizza",
executor: "ramping-vus",
stages: [
{ duration: '5s', target: 5 },
{ duration: '10s', target: 5 },
{ duration: '5s', target: 0 },
],
startTime: "10s",
},
pizzaRecommendations: {
executor: "constant-vus",
vus: 1,
duration: "30s",
options: {
browser: {
type: "chromium",
},
},
exec: 'pizzaRecommendations'
},
admin: {
executor: "constant-vus",
vus: 1,
duration: "30s",
options: {
browser: {
type: "chromium",
},
},
exec: 'admin'
},
},
thresholds: {
http_req_failed: ['rate<0.01'],
http_req_duration: ['p(95)<500', 'p(99)<1000'],
browser_web_vital_fcp: ["p(95) < 1000"],
browser_web_vital_lcp: ["p(95) < 2000"],
}
};
const myTrend = new Trend('totalActionTime');
export function getPizza() {
let restrictions = {
maxCaloriesPerSlice: 500,
mustBeVegetarian: false,
excludedIngredients: ["pepperoni"],
excludedTools: ["knife"],
maxNumberOfToppings: 6,
minNumberOfToppings: 2
}
let res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
headers: {
'Content-Type': 'application/json',
'Authorization': 'token abcdef0123456789',
},
});
check(res, { "status is 200": (res) => res.status === 200 });
sleep(1);
}
export async function admin() {
const page = browser.newPage();
try {
const loginPage = new LoginPage(page);
await loginPage.goto(BASE_URL);
await loginPage.login();
check(loginPage, {
"logout button text": loginPage.getLogoutButtonText() == "Logout",
});
} finally {
page.close();
}
}
export async function pizzaRecommendations() {
const page = browser.newPage();
const recommendationsPage = new RecommendationsPage(page);
const pageUtils = new PageUtils(page);
try {
await recommendationsPage.goto(BASE_URL);
pageUtils.addPerformanceMark('page-visit');
check(recommendationsPage, {
header: recommendationsPage.getHeadingTextContent() == "Looking to break out of your pizza routine?",
});
await recommendationsPage.getPizzaRecommendation();
pageUtils.addPerformanceMark('recommendations-returned');
check(recommendationsPage, {
recommendation: recommendationsPage.getPizzaRecommendationsContent() != "",
});
//Get time difference between visiting the page and pizza recommendations returned
pageUtils.measurePerformance('total-action-time', 'page-visit', 'recommendations-returned')
const totalActionTime = pageUtils.getPerformanceDuration('total-action-time');
myTrend.add(totalActionTime);
} finally {
page.close();
}
}