-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
268 lines (234 loc) · 9.38 KB
/
Form1.cs
File metadata and controls
268 lines (234 loc) · 9.38 KB
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
using test.Models;
using test.Services;
namespace test
{
public partial class Form1 : Form
{
private bool _isTesting = false;
private TestStatistics _testStatistics = new TestStatistics();
// 服务类
private readonly UserAgentManager _userAgentManager;
private readonly RandomDataGenerator _randomGenerator;
private readonly VariableReplacer _variableReplacer;
private readonly FormDataParser _formDataParser;
private readonly HttpTestService _httpTestService;
public Form1()
{
InitializeComponent();
// 初始化服务
_userAgentManager = new UserAgentManager();
_randomGenerator = new RandomDataGenerator();
_variableReplacer = new VariableReplacer(_randomGenerator);
_formDataParser = new FormDataParser();
_httpTestService = new HttpTestService(_variableReplacer, _formDataParser);
InitializeUAComboBox();
comboBoxMethod.SelectedIndexChanged += ComboBoxMethod_SelectedIndexChanged;
}
private void ComboBoxMethod_SelectedIndexChanged(object? sender, EventArgs e)
{
// 当选择POST时启用POST数据输入框
textBoxPostData.Enabled = comboBoxMethod.SelectedItem?.ToString() == "POST";
}
private void InitializeUAComboBox()
{
comboBoxUA.Items.Add("自定义");
foreach (var uaName in _userAgentManager.GetBuiltInUANames())
{
comboBoxUA.Items.Add(uaName);
}
comboBoxUA.SelectedIndex = 0;
comboBoxUA.SelectedIndexChanged += ComboBoxUA_SelectedIndexChanged;
}
private void ComboBoxUA_SelectedIndexChanged(object? sender, EventArgs e)
{
if (comboBoxUA.SelectedIndex > 0)
{
string? selectedUA = comboBoxUA.SelectedItem?.ToString();
if (!string.IsNullOrEmpty(selectedUA))
{
string? ua = _userAgentManager.GetUserAgent(selectedUA);
if (ua != null)
{
textBoxCustomUA.Text = ua;
}
}
}
}
private async void button1_Click(object sender, EventArgs e)
{
if (_isTesting)
{
MessageBox.Show("测试正在进行中,请稍候...", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
// 验证输入
if (!ValidateInput())
{
return;
}
// 开始测试
StartTest();
// 获取测试参数
string url = textBoxUrl.Text.Trim();
int concurrentCount = (int)numericUpDownConcurrent.Value;
int requestCount = (int)numericUpDownRequests.Value;
string userAgent = textBoxCustomUA.Text.Trim();
string httpMethod = comboBoxMethod.SelectedItem?.ToString() ?? "GET";
string postData = textBoxPostData.Text.Trim();
// 显示测试信息
DisplayTestInfo(url, httpMethod, postData, userAgent, concurrentCount, requestCount);
DateTime startTime = DateTime.Now;
// 执行并发测试
await ExecuteConcurrentTest(url, httpMethod, postData, userAgent, concurrentCount, requestCount);
DateTime endTime = DateTime.Now;
TimeSpan duration = endTime - startTime;
// 显示测试结果
DisplayTestResults(endTime, duration, requestCount);
// 结束测试
EndTest();
}
/// <summary>
/// 验证输入参数
/// </summary>
private bool ValidateInput()
{
string url = textBoxUrl.Text.Trim();
string httpMethod = comboBoxMethod.SelectedItem?.ToString() ?? "GET";
string postData = textBoxPostData.Text.Trim();
if (string.IsNullOrEmpty(url))
{
MessageBox.Show("请输入URL", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
if (httpMethod == "POST" && string.IsNullOrEmpty(postData))
{
MessageBox.Show("POST请求需要填写请求数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return true;
}
/// <summary>
/// 开始测试
/// </summary>
private void StartTest()
{
_testStatistics.Reset();
_isTesting = true;
button1.Enabled = false;
button1.Text = "测试中...";
}
/// <summary>
/// 结束测试
/// </summary>
private void EndTest()
{
button1.Enabled = true;
button1.Text = "开始测试";
_isTesting = false;
}
/// <summary>
/// 显示测试信息
/// </summary>
private void DisplayTestInfo(string url, string httpMethod, string postData, string userAgent, int concurrentCount, int requestCount)
{
textBoxResult.Clear();
textBoxResult.AppendText($"开始HTTP并发测试...\r\n");
textBoxResult.AppendText($"请求方法: {httpMethod}\r\n");
textBoxResult.AppendText($"URL: {url}\r\n");
if (httpMethod == "POST")
{
textBoxResult.AppendText($"POST数据: {postData}\r\n");
}
textBoxResult.AppendText($"User-Agent: {userAgent}\r\n");
textBoxResult.AppendText($"并发数: {concurrentCount}\r\n");
textBoxResult.AppendText($"总请求数: {requestCount}\r\n");
textBoxResult.AppendText($"开始时间: {DateTime.Now:yyyy-MM-dd HH:mm:ss}\r\n\r\n");
}
/// <summary>
/// 执行并发测试
/// </summary>
private async Task ExecuteConcurrentTest(string url, string httpMethod, string postData, string userAgent, int concurrentCount, int requestCount)
{
// 使用SemaphoreSlim控制并发数
SemaphoreSlim semaphore = new SemaphoreSlim(concurrentCount, concurrentCount);
List<Task> tasks = new List<Task>();
for (int i = 0; i < requestCount; i++)
{
int requestIndex = i + 1;
tasks.Add(Task.Run(async () =>
{
await semaphore.WaitAsync();
try
{
await ProcessHttpRequest(url, httpMethod, postData, userAgent, requestIndex);
}
finally
{
semaphore.Release();
}
}));
}
// 等待所有任务完成
await Task.WhenAll(tasks);
}
/// <summary>
/// 处理 HTTP 请求
/// </summary>
private async Task ProcessHttpRequest(string url, string httpMethod, string postData, string userAgent, int requestIndex)
{
var result = await _httpTestService.SendHttpRequestAsync(url, httpMethod, postData, userAgent, requestIndex);
// 更新统计
if (result.IsSuccess)
{
_testStatistics.IncrementSuccess();
}
else
{
_testStatistics.IncrementFail();
}
// 在UI线程更新结果
if (InvokeRequired)
{
Invoke(new Action(() => AppendResult(result)));
}
else
{
AppendResult(result);
}
}
/// <summary>
/// 追加结果到文本框
/// </summary>
private void AppendResult(HttpRequestResult result)
{
if (result.IsSuccess)
{
textBoxResult.AppendText($"[{result.RequestIndex}] 成功 - 状态码: {result.StatusCode}, 响应时间: {result.ResponseTimeMs:F2}ms\r\n");
}
else
{
if (!string.IsNullOrEmpty(result.ErrorMessage))
{
textBoxResult.AppendText($"[{result.RequestIndex}] 失败 - 错误: {result.ErrorMessage}\r\n");
}
else
{
textBoxResult.AppendText($"[{result.RequestIndex}] 失败 - 状态码: {result.StatusCode}, 响应时间: {result.ResponseTimeMs:F2}ms\r\n");
}
}
}
/// <summary>
/// 显示测试结果
/// </summary>
private void DisplayTestResults(DateTime endTime, TimeSpan duration, int requestCount)
{
textBoxResult.AppendText($"\r\n========== 测试完成 ==========\r\n");
textBoxResult.AppendText($"结束时间: {endTime:yyyy-MM-dd HH:mm:ss}\r\n");
textBoxResult.AppendText($"总耗时: {duration.TotalSeconds:F2} 秒\r\n");
textBoxResult.AppendText($"成功: {_testStatistics.SuccessCount}\r\n");
textBoxResult.AppendText($"失败: {_testStatistics.FailCount}\r\n");
textBoxResult.AppendText($"成功率: {_testStatistics.SuccessRate:F2}%\r\n");
textBoxResult.AppendText($"平均响应时间: {(duration.TotalMilliseconds / requestCount):F2} ms\r\n");
}
}
}