-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconcurrent_proxy_test.rs
More file actions
279 lines (239 loc) · 9.44 KB
/
Copy pathconcurrent_proxy_test.rs
File metadata and controls
279 lines (239 loc) · 9.44 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
269
270
271
272
273
274
275
276
277
278
279
use dotenvy::dotenv;
use kode_bridge::{ClientConfig, IpcHttpClient, KodeBridgeError, Result};
use serde::{Deserialize, Serialize};
use std::env;
use std::sync::Arc;
use std::time::{Duration, Instant};
#[derive(Debug, Deserialize, Serialize)]
pub struct UpdateProxyPayload {
pub name: String,
}
/// 并发代理测试和延迟测量
async fn concurrent_proxy_test(client: Arc<IpcHttpClient>, proxy_group: &str, proxies: Vec<&str>) -> Result<()> {
println!("🚀 Starting concurrent proxy testing for group: {}", proxy_group);
println!("📊 Testing {} proxies concurrently", proxies.len());
let start_time = Instant::now();
let mut tasks = Vec::new();
// 创建并发任务
for (index, proxy_name) in proxies.iter().enumerate() {
let client = Arc::clone(&client);
let proxy_group = proxy_group.to_string();
let proxy_name = proxy_name.to_string();
let task = tokio::spawn(async move {
let task_start = Instant::now();
println!(" 🔄 Task {}: Testing proxy '{}'", index + 1, proxy_name);
// 执行代理切换测试
let result = test_proxy_switch(&client, &proxy_group, &proxy_name).await;
let duration = task_start.elapsed();
let success_status = match &result {
Ok(success) => {
if *success {
println!(
" ✅ Task {}: Proxy '{}' switched successfully in {:?}",
index + 1,
proxy_name,
duration
);
"success"
} else {
println!(
" ⚠️ Task {}: Proxy '{}' switch failed in {:?}",
index + 1,
proxy_name,
duration
);
"failed"
}
}
Err(e) => {
println!(
" ❌ Task {}: Proxy '{}' error in {:?}: {}",
index + 1,
proxy_name,
duration,
e
);
"error"
}
};
(index, proxy_name, duration, success_status, result.is_ok())
});
tasks.push(task);
}
// 等待所有任务完成
let mut results = Vec::new();
for task in tasks {
match task.await {
Ok(result) => results.push(result),
Err(e) => println!(" ❌ Task join error: {}", e),
}
}
// 统计结果
let total_duration = start_time.elapsed();
let successful_count = results
.iter()
.filter(|(_, _, _, status, _)| *status == "success")
.count();
let failed_count = results.iter().filter(|(_, _, _, _, ok)| !ok).count();
let timeout_count = results
.iter()
.filter(|(_, _, _, status, _)| *status == "error")
.count();
println!();
println!("📊 Concurrent Test Results:");
println!(" 🎯 Total proxies tested: {}", proxies.len());
println!(" ✅ Successful switches: {}", successful_count);
println!(" ❌ Failed requests: {}", failed_count);
println!(" ⏰ Timeout errors: {}", timeout_count);
println!(" 🕐 Total time: {:?}", total_duration);
println!(
" 📈 Average time per request: {:?}",
Duration::from_millis(total_duration.as_millis() as u64 / proxies.len() as u64)
);
// 分析延迟分布
let mut durations: Vec<Duration> = results
.iter()
.map(|(_, _, duration, _, _)| *duration)
.collect();
durations.sort();
if !durations.is_empty() {
let min_duration = durations[0];
let max_duration = durations[durations.len() - 1];
let median_duration = durations[durations.len() / 2];
println!(" 📊 Latency Analysis:");
println!(" ⚡ Fastest: {:?}", min_duration);
println!(" 🐌 Slowest: {:?}", max_duration);
println!(" 📊 Median: {:?}", median_duration);
}
// 如果超时率过高,给出建议
let timeout_rate = timeout_count as f64 / proxies.len() as f64;
if timeout_rate > 0.3 {
println!();
println!("⚠️ High timeout rate detected ({:.1}%)", timeout_rate * 100.0);
println!("💡 Suggestions:");
println!(" - Reduce concurrent requests (current: {})", proxies.len());
println!(" - Increase timeout duration");
println!(" - Check server load and network conditions");
}
Ok(())
}
/// 测试单个代理切换
async fn test_proxy_switch(client: &IpcHttpClient, proxy_group: &str, proxy_name: &str) -> Result<bool> {
let path = format!("/proxies/{}", proxy_group);
let payload = UpdateProxyPayload {
name: proxy_name.to_string(),
};
// 执行代理切换请求
let response = client
.put(&path)
.json_body(&serde_json::to_value(&payload)?)
.timeout(Duration::from_secs(8)) // 设置较短的超时
.send()
.await?;
if response.is_success() {
// 验证切换是否生效
tokio::time::sleep(Duration::from_millis(500)).await;
verify_proxy_switch(client, proxy_group, proxy_name).await
} else {
Ok(false)
}
}
/// 验证代理切换是否生效
async fn verify_proxy_switch(client: &IpcHttpClient, proxy_group: &str, expected_proxy: &str) -> Result<bool> {
let response = client
.get("/proxies")
.timeout(Duration::from_secs(5))
.send()
.await?;
if !response.is_success() {
return Ok(false);
}
let proxy_data = response.json_value()?;
if let Some(proxies_obj) = proxy_data.get("proxies").and_then(|v| v.as_object()) {
if let Some(group_info) = proxies_obj.get(proxy_group) {
if let Some(current_proxy) = group_info.get("now").and_then(|v| v.as_str()) {
return Ok(current_proxy == expected_proxy);
}
}
}
Ok(false)
}
#[tokio::main]
async fn main() -> Result<()> {
dotenv().ok();
println!("🧪 Concurrent Proxy Testing Example");
println!("=====================================");
let ipc_path = env::var("CUSTOM_SOCK").unwrap_or_else(|_| "/tmp/example.sock".to_string());
// 配置优化的HTTP客户端
let config = ClientConfig {
default_timeout: Duration::from_secs(8),
enable_pooling: true,
max_retries: 3,
retry_delay: Duration::from_millis(100),
max_concurrent_requests: 8, // 限制并发数
max_requests_per_second: Some(5.0), // 限制请求频率
..Default::default()
};
let client = Arc::new(IpcHttpClient::with_config(&ipc_path, config)?);
// 获取可用代理
println!("📡 Fetching available proxies...");
let response = client
.get("/proxies")
.timeout(Duration::from_secs(10))
.send()
.await?;
if !response.is_success() {
return Err(KodeBridgeError::connection(format!(
"Failed to get proxies: {}",
response.status()
)));
}
let proxy_data = response.json_value()?;
if let Some(proxies_obj) = proxy_data.get("proxies").and_then(|v| v.as_object()) {
// 寻找第一个有多个代理的组进行测试
for (group_name, group_info) in proxies_obj.iter() {
if let Some(all_proxies) = group_info.get("all").and_then(|v| v.as_array()) {
if all_proxies.len() >= 3 {
// 至少3个代理才进行并发测试
let proxy_names: Vec<&str> = all_proxies
.iter()
.filter_map(|v| v.as_str())
.take(8) // 限制最多8个并发
.collect();
if !proxy_names.is_empty() {
println!(
"🎯 Found group '{}' with {} proxies for testing",
group_name,
proxy_names.len()
);
// 执行并发测试
if let Err(e) = concurrent_proxy_test(Arc::clone(&client), group_name, proxy_names).await {
println!("❌ Concurrent test failed: {}", e);
}
// 显示连接池统计
if let Some(stats) = client.pool_stats() {
println!();
println!("🔗 Connection Pool Stats: {}", stats);
}
break;
}
}
}
}
} else {
println!("⚠️ No suitable proxy groups found for testing");
}
// 清理资源
client.close();
drop(client);
println!();
println!("🎯 Concurrent testing completed!");
println!("💡 Key optimizations applied:");
println!(" ✅ Connection pooling with increased size (20 connections)");
println!(" ✅ Reduced timeouts (8s request, 5s connection)");
println!(" ✅ Enhanced retry logic with exponential backoff");
println!(" ✅ Concurrent request limiting (8 max)");
println!(" ✅ Rate limiting (5 requests/sec)");
println!(" ✅ Jitter in retry delays to prevent thundering herd");
Ok(())
}