-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathindex.html
387 lines (366 loc) · 16.1 KB
/
index.html
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<!DOCTYPE html>
<html lang="zh-CN" style="height: 100%">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts@5/dist/echarts.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script type="text/javascript">
// 添加语言包配置
const i18n = {
'zh-CN': {
title: 'HTTP 压力测试工具',
start: '开始压测',
stop: '停止压测',
metrics: {
duration: '监控间隔',
durationPlaceholder: '监控数据刷新间隔,默认2000ms'
},
request: {
url: '请求地址',
urlPlaceholder: '请输入压测目标URL',
method: '请求方法',
methodPlaceholder: '选择请求方法',
body: '请求体',
bodyPlaceholder: '请输入请求体',
bodyType: '请求体类型',
bodyTypePlaceholder: '空或hex,默认空',
type: '协议类型',
typePlaceholder: 'http1, http2, ws, wss,默认http1'
},
stress: {
total: '总请求数',
concurrent: '并发数',
duration: '持续时间',
timeout: '超时时间',
qps: 'QPS限制',
worker: 'Worker API'
},
message: {
start: '开始压测',
stop: '停止压测',
error: '错误'
}
},
'en-US': {
title: 'HTTP Benchmark Tool',
start: 'Start Stress',
stop: 'Stop Stress',
metrics: {
duration: 'Metrics Duration',
durationPlaceholder: 'Metrics refresh interval, default 2000ms'
},
request: {
url: 'Request URL',
urlPlaceholder: 'Enter stress test target URL',
method: 'Request Method',
methodPlaceholder: 'Select request method',
body: 'Request Body',
bodyPlaceholder: 'Enter request body',
bodyType: 'Body Type',
bodyTypePlaceholder: 'empty or hex, default empty',
type: 'Protocol Type',
typePlaceholder: 'http1, http2, ws, wss, default http1'
},
stress: {
total: 'Total Requests',
concurrent: 'Concurrent',
duration: 'Duration',
timeout: 'Timeout',
qps: 'QPS Limit',
worker: 'Worker API'
},
message: {
start: 'Start Stress',
stop: 'Stop Stress',
error: 'Error'
}
}
};
</script>
</head>
<body style="height: 100%; margin: 0">
<div id="container"
style="height: 70%; margin: auto; width:98%; padding: 10px; box-sizing: border-box; box-shadow: rgba(0, 0, 0, 0.3) 0px 0px 20px;">
</div>
<!-- 修改表单标签为多语言 -->
<div id="app" style="margin: 20px;">
<el-select v-model="currentLang" @change="changeLang" size="small" style="margin-right: 15px; margin-bottom: 10px;">
<el-option label="中文" value="zh-CN"></el-option>
<el-option label="English" value="en-US"></el-option>
</el-select>
<el-row>
<el-button type="primary" :loading="g_running" @click="submitStart">{{t('start')}}</el-button>
<el-button type="danger" @click="submitStop">{{t('stop')}}</el-button>
</el-row>
<el-input :placeholder="t('metrics.durationPlaceholder')" v-model="time_metrics" style="margin: 4px 0;">
<template slot="prepend">{{t('metrics.duration')}}</template>
</el-input>
<el-input placeholder="request url" v-model="url" style="margin: 4px 0;">
<template slot="prepend">Stress Url</template>
</el-input>
<el-input placeholder="GET, POST, PUT, DELETE, HEAD, OPTIONS" v-model="request_method" style="margin: 4px 0;">
<template slot="prepend">Request method</template>
</el-input>
<el-input placeholder="Request body" v-model="request_body" type="textarea"
:autosize="{ minRows: 5, maxRows: 100}" style="margin: 4px 0;">
<template slot="prepend">Request body</template>
</el-input>
<el-input placeholder="empty, hex, default empty" v-model="request_bodytype" style="margin: 4px 0;">
<template slot="prepend">Request bodytype</template>
</el-input>
<el-input placeholder="http1, http2, ws, wss, default http1" v-model="request_type" style="margin: 4px 0;">
<template slot="prepend">Request type</template>
</el-input>
<el-input placeholder="n" v-model="n" style="margin: 4px 0;">
<template slot="prepend">N</template>
</el-input>
<el-input placeholder="c" v-model="c" style="margin: 4px 0;">
<template slot="prepend">C</template>
</el-input>
<el-input placeholder="duration(s)" v-model="duration" style="margin: 4px 0;">
<template slot="prepend">Duration</template>
</el-input>
<el-input placeholder="timeout(ms)" v-model="timeout" style="margin: 4px 0;">
<template slot="prepend">Timeout</template>
</el-input>
<el-input placeholder="qps" v-model="qps" style="margin: 4px 0;">
<template slot="prepend">QPS</template>
</el-input>
<el-input placeholder="/api" v-model="worker_api" style="margin: 4px 0;">
<template slot="prepend">Worker API</template>
</el-input>
</div>
<script type="text/javascript">
Date.prototype.format = function (fmt) {
let o = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S": this.getMilliseconds()
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (let k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
}
}
return fmt;
}
const contentType = { 'Content-Type': 'application/json;charset=utf-8' };
const workerApiPath = "/api";
let dom = document.getElementById('container');
let stressChart = echarts.init(dom, 'dark', {
renderer: 'canvas',
useDirtyRect: false
});
function metricsLoad(timeList, qpsList, codeList) {
let option = {
legend: {
data: ['qps']
},
tooltip: {
trigger: 'axis',
axisPointer: { type: 'cross' }
},
xAxis: {
type: 'category',
data: timeList
},
yAxis: {
type: 'value'
},
series: [
{
name: "qps",
data: qpsList,
type: 'line',
smooth: true
},
]
};
for (let key in codeList) {
option.series.push({
name: key,
data: codeList[key],
type: 'line',
smooth: true
});
option.legend.data.push(key);
}
if (option && typeof option === 'object') {
stressChart.setOption(option);
}
}
metricsLoad([new Date().format("hh:mm:ss")], [0])
window.addEventListener('resize', stressChart.resize);
new Vue({
el: '#app',
data: {
currentLang: 'zh-CN',
time_metrics: 2000,
request_method: "GET",
request_body: "",
request_bodytype: "",
request_type: "http1",
n: 0,
c: 1,
duration: 1000,
timeout: 3000,
qps: 0,
disable_compression: false,
disable_keepalives: false,
auth_username: "",
auth_password: "",
url: "http://127.0.0.1:8000?data=1",
worker_api: "",
g_running: false,
g_seqid: Math.floor(Math.random() * 1000000) + 1,
g_interval: undefined,
},
methods: {
// Add t method for i18n
t(key) {
const keys = key.split('.');
let value = i18n[this.currentLang];
for (const k of keys) {
value = value[k];
if (!value) break;
}
return value || key;
},
changeLang(lang) {
this.currentLang = lang;
localStorage.setItem('preferred-lang', lang);
},
submitStart: function (e) {
let request_data = {
cmd: 0,
sequence_id: this.g_seqid,
request_method: this.request_method,
request_body: this.request_body,
request_bodytype: this.request_bodytype,
request_type: this.request_type,
n: parseInt(this.n),
c: parseInt(this.c),
duration: parseInt(this.duration),
timeout: parseInt(this.timeout),
qps: parseInt(this.qps),
disable_compression: this.disable_compression || this.disable_compression == "true" ? true : false,
disable_keepalives: this.disable_keepalives || this.disable_keepalives == "true" ? true : false,
auth_username: this.auth_username,
auth_password: this.auth_password,
url: this.url,
};
let worker_api = workerApiPath;
if (this.worker_api.length > 0) {
worker_api = this.worker_api;
}
fetch(worker_api, {
method: 'POST',
headers: contentType,
body: JSON.stringify(request_data)
}).then(response => response.json()).then(data => {
if (data.err_code != 0) {
this.g_running = false;
this.g_interval && clearInterval(this.g_interval);
this.$message({
showClose: true,
message: 'error:' + data.err_msg,
type: 'error',
duration: 5000,
});
return;
}
this.$message({
showClose: true,
message: 'Start Stress',
type: 'success',
duration: 2000,
});
});
let time_list = [], qps_list = [], lats_total = 0;
let status_code_list = {}, lats_status_code_list = {};
let time_metrics = this.time_metrics > 0 ? this.time_metrics : 2000;
this.g_running = true;
this.g_interval = setInterval(function () {
request_data.cmd = 2; // get metrics
fetch(worker_api, {
method: 'POST',
headers: contentType,
body: JSON.stringify(request_data)
}).then(response => response.json()).then(data => {
if (data && data.lats_total && (data.lats_total - lats_total) >= 0) {
time_list.push(new Date().format("hh:mm:ss"));
qps_list.push((data.lats_total - lats_total) * 1000 / time_metrics);
lats_total = data.lats_total;
if (data.status_code_dist) {
for (let key in data.status_code_dist) {
if (!status_code_list.hasOwnProperty(key)) {
status_code_list[key] = [];
}
if (!lats_status_code_list.hasOwnProperty(key)) {
lats_status_code_list[key] = 0;
}
status_code_list[key].push(
(data.status_code_dist[key] - lats_status_code_list[key]) * 1000 / time_metrics)
}
lats_status_code_list = data.status_code_dist;
}
}
metricsLoad(time_list, qps_list, status_code_list);
})
}, time_metrics);
},
submitStop: function (e) {
this.g_running = false;
this.g_interval && clearInterval(this.g_interval);
let request_data = {
cmd: 1, // stop
sequence_id: this.g_seqid,
};
let worker_api = workerApiPath;
if (this.worker_api.length > 0) {
worker_api = this.worker_api;
}
fetch(worker_api, {
method: 'POST',
headers: contentType,
body: JSON.stringify(request_data)
}).then(response => response.json()).then(data => {
if (data.err_code != 0) {
this.$message({
showClose: true,
message: 'error:' + data.err_msg,
type: 'error',
duration: 5000,
});
return;
}
this.$message({
showClose: true,
message: 'Stop Stress',
type: 'success',
duration: 2000,
});
});
}
},
created() {
// 初始化时读取存储的语言设置
const savedLang = localStorage.getItem('preferred-lang');
if (savedLang && i18n[savedLang]) {
this.currentLang = savedLang;
}
},
})
</script>
</body>
</html>