-
Notifications
You must be signed in to change notification settings - Fork 4
/
loadgenerator.js
180 lines (142 loc) · 4.78 KB
/
loadgenerator.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import http from 'k6/http';
import { sleep,check} from 'k6';
import { Counter } from "k6/metrics";
//import tracing, { Http } from 'k6/x/tracing';
/**
* Hipster workload generator by k6
* @param __ENV.FRONTEND_ADDR
* @constructor yuxiaoba
*/
let errors = new Counter("errors");
const baseurl = `http://${__ENV.FRONTEND_ADDR}`;
const tasks = {
"index": 1,
"setCurrency": 2,
"browseProduct": 10,
"addToCart": 2,
"viewCart": 3,
"checkout": 1
};
const products = [
'0PUK6V6EV0',
'1YMWWN1N4O',
'2ZYFJ3GM2N',
'66VCHSJNUP',
'6E92ZMYYFZ',
'9SIQT8TOJO',
'L9ECAV7KIM',
'LS4PSXUNUM',
'OLJCESPC7Z'];
const waittime = [1,2,3,4,5,6,7,8,9,10]
export function setup() {
// console.log(`Running xk6-distributed-tracing v${tracing.version}`);
}
export const options = {
discardResponseBodies: true,
vus: 70,
duration: '10m',
};
export default function() {
// const http = new Http({
// exporter: "otlp",
// propagator: "w3c",
//endpoint: url
//});
//Access index page
for ( let i=0; i<tasks["index"]; i++)
{
let res = http.get(`${baseurl}/`);
let checkRes = check(res, { "status is 200": (r) => r.status === 200 });
// show the error per second in grafana
if (checkRes === false ){
errors.add(1);
}
sleep(waittime[Math.floor(Math.random() * waittime.length)])
}
//Access setCurrency page
for ( let i=0; i<tasks["setCurrency"]; i++)
{
const currencies = ['EUR', 'USD', 'JPY', 'CAD'];
let res = http.post(`${baseurl}/setCurrency`, {
'currency_code': currencies[Math.floor(Math.random() * currencies.length)]
});
let checkRes = check(res, { "status is 200": (r) => r.status === 200 });
// show the error per second in grafana
if (checkRes === false ){
errors.add(1);
}
sleep(waittime[Math.floor(Math.random() * waittime.length)])
}
//Access browseProduct page
for ( let i=0; i<tasks["browseProduct"]; i++)
{
let product = products[Math.floor(Math.random() * products.length)]
let res = http.get(`${baseurl}/product/${product}`);
let checkRes = check(res, { "status is 200": (r) => r.status === 200 });
// show the error per second in grafana
if (checkRes === false ){
errors.add(1);
}
sleep(waittime[Math.floor(Math.random() * waittime.length)])
}
//Access addToCart page
for ( let i=0; i<tasks["addToCart"]; i++)
{
let product = products[Math.floor(Math.random() * products.length)]
let res = http.get(`${baseurl}/product/${product}`);
let checkRes = check(res, { "status is 200": (r) => r.status === 200 });
// show the error per second in grafana
if (checkRes === false ){
errors.add(1);
}
sleep(waittime[Math.floor(Math.random() * waittime.length)])
const quantity = [1,2,3,4,5,10]
res = http.post(`${baseurl}/cart`, {
'product_id': product,
'quantity': quantity[Math.floor(Math.random() * quantity.length)]
});
checkRes = check(res, { "status is 200": (r) => r.status === 200 });
// show the error per second in grafana
if (checkRes === false ){
errors.add(1);
}
sleep(waittime[Math.floor(Math.random() * waittime.length)])
}
// Access viewCart page
for ( let i=0; i<tasks["viewCart"]; i++)
{
let res = http.get(`${baseurl}/cart`);
let checkRes = check(res, { "status is 200": (r) => r.status === 200 });
// show the error per second in grafana
if (checkRes === false ){
errors.add(1);
}
sleep(waittime[Math.floor(Math.random() * waittime.length)])
}
//Access checkout page
for ( let i=0; i<tasks["checkout"]; i++)
{
let res = http.post(`${baseurl}/cart/checkout`, {
'email': '[email protected]',
'street_address': '1600 Amphitheatre Parkway',
'zip_code': '94043',
'city': 'Mountain View',
'state': 'CA',
'country': 'United States',
'credit_card_number': '4432-8015-6152-0454',
'credit_card_expiration_month': '1',
'credit_card_expiration_year': '2039',
'credit_card_cvv': '672',
});
let checkRes = check(res, { "status is 200": (r) => r.status === 200 });
// show the error per second in grafana
if (checkRes === false ){
errors.add(1);
}
sleep(waittime[Math.floor(Math.random() * waittime.length)])
}
}
export function teardown(){
// Cleanly shutdown and flush telemetry when k6 exits.
// tracing.shutdown();
}