feat(utils): 添加延迟测试工具类用于TCP连接延迟测试
新增KRLatencyTester工具类,提供单节点和批量节点的TCP延迟测试功能 支持自定义超时时间和并发数,测试结果包含成功/失败状态和延迟时间 ``` ```msg refactor(home): 使用_storage直接保存闪连状态 将_saveQuickConnectStatus方法替换为直接调用_storage.kr_saveBool 简化代码逻辑,保持存储方式的一致性
This commit is contained in:
parent
610eb4f20d
commit
78c20f9ae9
@ -133,7 +133,7 @@ class KRHomeController extends GetxController with WidgetsBindingObserver {
|
||||
if (value != null) {
|
||||
isQuickConnectEnabled.value = value;
|
||||
// 保存闪连状态到本地存储
|
||||
await _saveQuickConnectStatus(value);
|
||||
await _storage.kr_saveBool(key: _quickConnectKey, value: value);
|
||||
KRLogUtil.kr_i('闪连状态已更新: $value', tag: 'QuickConnect');
|
||||
}
|
||||
}
|
||||
|
||||
127
lib/app/utils/kr_latency_tester.dart
Normal file
127
lib/app/utils/kr_latency_tester.dart
Normal file
@ -0,0 +1,127 @@
|
||||
import 'dart:io';
|
||||
import 'dart:async';
|
||||
import 'package:kaer_with_panels/app/utils/kr_log_util.dart';
|
||||
|
||||
/// 延迟测试工具类
|
||||
/// 提供真实的 TCP 连接延迟测试功能
|
||||
class KRLatencyTester {
|
||||
/// 测试单个节点的延迟
|
||||
///
|
||||
/// 参数:
|
||||
/// - host: 主机地址
|
||||
/// - port: 端口号
|
||||
/// - timeout: 超时时间(毫秒)
|
||||
///
|
||||
/// 返回:
|
||||
/// - 延迟时间(毫秒),如果失败返回 65535
|
||||
static Future<int> testNode({
|
||||
required String host,
|
||||
required int port,
|
||||
int timeout = 5000,
|
||||
}) async {
|
||||
try {
|
||||
final stopwatch = Stopwatch()..start();
|
||||
|
||||
final socket = await Socket.connect(
|
||||
host,
|
||||
port,
|
||||
timeout: Duration(milliseconds: timeout),
|
||||
).timeout(Duration(milliseconds: timeout));
|
||||
|
||||
stopwatch.stop();
|
||||
|
||||
// 立即关闭连接
|
||||
await socket.close();
|
||||
socket.destroy();
|
||||
|
||||
final latency = stopwatch.elapsedMilliseconds;
|
||||
KRLogUtil.kr_i('✅ 延迟测试成功: $host:$port = ${latency}ms', tag: 'KRLatencyTester');
|
||||
|
||||
return latency;
|
||||
} catch (e) {
|
||||
KRLogUtil.kr_w('❌ 延迟测试失败: $host:$port - $e', tag: 'KRLatencyTester');
|
||||
return 65535; // 测试失败返回最大值
|
||||
}
|
||||
}
|
||||
|
||||
/// 批量测试多个节点的延迟
|
||||
///
|
||||
/// 参数:
|
||||
/// - nodes: 节点列表,格式为 [{"host": "example.com", "port": 443}]
|
||||
/// - concurrency: 并发数量
|
||||
/// - timeout: 超时时间(毫秒)
|
||||
///
|
||||
/// 返回:
|
||||
/// - 测试结果映射,键为 "host:port",值为延迟时间
|
||||
static Future<Map<String, int>> testMultipleNodes({
|
||||
required List<MapEntry<String, SocketAddress>> nodes,
|
||||
int concurrency = 10,
|
||||
int timeout = 5000,
|
||||
}) async {
|
||||
final results = <String, int>{};
|
||||
final semaphore = Completer<void>();
|
||||
var activeCount = 0;
|
||||
var completedCount = 0;
|
||||
|
||||
KRLogUtil.kr_i('🚀 开始批量延迟测试,共 ${nodes.length} 个节点,并发数: $concurrency', tag: 'KRLatencyTester');
|
||||
|
||||
Future<void> processNode(MapEntry<String, SocketAddress> node) async {
|
||||
try {
|
||||
final host = node.value.address;
|
||||
final port = node.value.port;
|
||||
final key = node.key;
|
||||
|
||||
final latency = await testNode(
|
||||
host: host,
|
||||
port: port,
|
||||
timeout: timeout,
|
||||
);
|
||||
|
||||
results[key] = latency;
|
||||
} catch (e) {
|
||||
results[node.key] = 65535;
|
||||
KRLogUtil.kr_e('❌ 节点测试异常: ${node.key} - $e', tag: 'KRLatencyTester');
|
||||
} finally {
|
||||
completedCount++;
|
||||
activeCount--;
|
||||
|
||||
if (completedCount >= nodes.length) {
|
||||
semaphore.complete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 分批处理节点
|
||||
for (var i = 0; i < nodes.length; i += concurrency) {
|
||||
final batch = nodes.skip(i).take(concurrency);
|
||||
|
||||
for (final node in batch) {
|
||||
activeCount++;
|
||||
processNode(node);
|
||||
}
|
||||
|
||||
// 等待当前批次完成
|
||||
if (i + concurrency < nodes.length) {
|
||||
await Future.delayed(Duration(milliseconds: 100));
|
||||
}
|
||||
}
|
||||
|
||||
// 等待所有任务完成
|
||||
await semaphore.future;
|
||||
|
||||
KRLogUtil.kr_i('✅ 批量延迟测试完成,成功: ${results.length} 个', tag: 'KRLatencyTester');
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
/// Socket 地址类
|
||||
/// 表示网络地址和端口的组合
|
||||
class SocketAddress {
|
||||
final String address;
|
||||
final int port;
|
||||
|
||||
SocketAddress(this.address, this.port);
|
||||
|
||||
@override
|
||||
String toString() => '$address:$port';
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user