/
benchmark-scenarios.js
168 lines (165 loc) · 6.52 KB
/
benchmark-scenarios.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
// https://k6.io/docs/using-k6/scenarios/
// https://k6.io/docs/using-k6/scenarios/#executors
// scenario 1
// VU=50; REQRATE=10; TIME=30; DOMAIN=https://yourdomain.com/
// taskset -c 0-3 k6 run -e RPS=${REQRATE} -e DURATION=${TIME}s -e USERS=${VU} -e STAGETIME=${TIME}s -e URL=$DOMAIN --no-usage-report --out json=summary-raw-scenarios.gz benchmark-scenarios.js
//
// scenario 2
// VU=50; TIME=30; REQUESTS=100; DOMAIN=https://yourdomain.com/
// taskset -c 0-3 k6 run -e REQUESTS=${REQUESTS} -e DURATION=${TIME}s -e USERS=${VU} -e STAGETIME=${TIME}s -e URL=$DOMAIN --no-usage-report --out json=summary-raw-scenarios.gz benchmark-scenarios.js
//
// scenario 3
// VU=50; TIME=30; DOMAIN=https://yourdomain.com/
// taskset -c 0-3 k6 run -e DURATION=${TIME}s -e USERS=${VU} -e STAGETIME=${TIME}s -e URL=$DOMAIN --no-usage-report --out json=summary-raw-scenarios.gz benchmark-scenarios.js
//
// scenario 4
// TIME=30; STAGEVU1=25; STAGEVU2=50; STAGEVU3=100; STAGEVU4=0; DOMAIN=https://yourdomain.com/
// taskset -c 0-3 k6 run -e STAGETIME=${TIME}s -e STAGE_VU1=${STAGEVU1} -e STAGE_VU2=${STAGEVU2} -e STAGE_VU3=${STAGEVU3} -e STAGE_VU4=${STAGEVU4} -e URL=$DOMAIN --no-usage-report --out json=summary-raw-scenarios.gz benchmark-scenarios.js
//
// scenario 4 with influxdb + grafana
// export K6_INFLUXDB_USERNAME=
// export K6_INFLUXDB_PASSWORD=
// TIME=30; STAGEVU1=25; STAGEVU2=50; STAGEVU3=100; STAGEVU4=0; DOMAIN=https://yourdomain.com/
// taskset -c 0-3 k6 run -e STAGETIME=${TIME}s -e STAGE_VU1=${STAGEVU1} -e STAGE_VU2=${STAGEVU2} -e STAGE_VU3=${STAGEVU3} -e STAGE_VU4=${STAGEVU4} -e URL=$DOMAIN --no-usage-report --out influxdb=http://localhost:8186/k6 benchmark-scenarios.js
//
// with psrecord
// spid=$(cminfo service-info nginx | jq -r '.MainPID')
// psrecord $spid --include-children --interval 0.1 --duration $((TIME*5+30)) --log psrecord-ramping-vus-nginx.log --plot plot-ramping-vus-nginx.png &
// psrecord "taskset -c 0-3 k6 run -e STAGETIME=${TIME}s -e STAGE_VU1=${STAGEVU1} -e STAGE_VU2=${STAGEVU2} -e STAGE_VU3=${STAGEVU3} -e STAGE_VU4=${STAGEVU4} -e URL=$DOMAIN --no-usage-report --out influxdb=http://localhost:8186/k6 benchmark-scenarios.js" --include-children --interval 0.1 --duration $((TIME*5+30)) --log psrecord-ramping-vus.log --plot plot-ramping-vus.png
//
//
import { check } from "k6";
import { group } from 'k6';
import { sleep } from "k6";
import http from "k6/http";
import exec from "k6/execution";
import { tagWithCurrentStageIndex } from "https://jslib.k6.io/k6-utils/1.4.0/index.js";
import { randomIntBetween } from "https://jslib.k6.io/k6-utils/1.4.0/index.js";
// https://k6.io/docs/javascript-api/jslib/utils/randomstring/
//import { randomString } from 'https://jslib.k6.io/k6-utils/1.4.0/index.js';
import {
jUnit,
textSummary,
} from "https://jslib.k6.io/k6-summary/0.1.0/index.js";
export const options = {
// // scenario 1
// https://k6.io/docs/using-k6/scenarios/executors/constant-arrival-rate
// scenarios: {
// constant_arrival_rate: {
// executor: 'constant-arrival-rate',
// startTime: '0s',
// rate: `${__ENV.RPS}`,
// timeUnit: '1s',
// duration: `${__ENV.DURATION}`,
// preAllocatedVUs: `${__ENV.USERS}`,
// maxVUs: 1000,
// tags: { executor: 'constant-arrival-rate' },
// stages: [
// { duration: `${__ENV.STAGETIME}`, target: 25 },
// { duration: `${__ENV.STAGETIME}`, target: 50 },
// { duration: `${__ENV.STAGETIME}`, target: 100 },
// { duration: `${__ENV.STAGETIME}`, target: 0 },
// ],
// },
// },
// // scenario 2
// https://k6.io/docs/using-k6/scenarios/executors/per-vu-iterations
// scenarios: {
// per_vu_iterations: {
// executor: 'per-vu-iterations',
// startTime: '0s',
// vus: `${__ENV.USERS}`,
// iterations: `${__ENV.REQUESTS}`,
// maxDuration: `${__ENV.DURATION}`,
// tags: { executor: 'per-vu-iterations' },
// },
// },
// // scenario 3
// https://k6.io/docs/using-k6/scenarios/executors/constant-vus/
// scenarios: {
// constant_vus: {
// executor: 'constant-vus',
// startTime: '0s',
// vus: `${__ENV.USERS}`,
// duration: `${__ENV.DURATION}`,
// tags: { executor: 'constant-vus' },
// },
// },
// // scenario 4
// https://k6.io/docs/using-k6/scenarios/executors/ramping-vus
scenarios: {
ramping_vus: {
executor: 'ramping-vus',
startTime: '0s',
startVUs: 0,
stages: [
{ duration: `${__ENV.STAGETIME}`, target: `${__ENV.STAGE_VU1}` },
{ duration: `${__ENV.STAGETIME}`, target: `${__ENV.STAGE_VU2}` },
{ duration: `${__ENV.STAGETIME}`, target: `${__ENV.STAGE_VU3}` },
{ duration: `${__ENV.STAGETIME}`, target: `${__ENV.STAGE_VU4}` },
],
gracefulRampDown: '0s',
tags: { executor: 'ramping-vus' },
},
},
// httpDebug: 'full',
insecureSkipTLSVerify: true,
discardResponseBodies: true,
noConnectionReuse: false,
userAgent:
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.66 Safari/537.36 OPR/89.0.4447.38",
summaryTrendStats: [
"avg",
"min",
"med",
"max",
"p(95)",
"p(99)",
"p(99.99)",
"count",
],
tlsVersion: {
min: "tls1.2",
max: "tls1.3",
},
thresholds: {
'http_req_duration{gzip:yes}': ["avg<150", "p(95)<500"],
// http_req_duration: ["avg<150", "p(95)<500"],
// http_req_duration: ['p(90) < 200', 'p(95) < 300', 'p(99.9) < 2000'],
},
};
export function handleSummary(data) {
return {
stdout: textSummary(data, { indent: " ", enableColors: true }),
"summary-scenarios.json": JSON.stringify(data),
};
}
export default function () {
const sleepMin = 1;
const sleepMax = 5;
tagWithCurrentStageIndex();
// console.log(exec.test.options.scenarios.default.stages[0].target)
// console.log(exec.instance.vusActive);
const params = {
headers: {
accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-encoding": "gzip, deflate",
"accept-language": "en-US,en;q=0.9",
connection: "keep-alive",
},
// cookies: { my_cookie: 'value' },
// redirects: 5,
tags: {
gzip: 'yes'
}
};
group('main index page', function () {
const res = http.get(`${__ENV.URL}`, params);
// console.log('Response time was ' + String(res.timings.duration) + ' ms');
check(res, {
"is status 200": (r) => r.status === 200,
});
// sleep(1);
// sleep(randomIntBetween(sleepMin, sleepMax));
});
}