feat(utils): 添加延迟测试工具类用于TCP连接延迟测试
Some checks failed
Build Windows / build (push) Has been cancelled
Build Windows / 编译 libcore (Windows) (20.15.1) (push) Has been cancelled

新增KRLatencyTester工具类,提供单节点和批量节点的TCP延迟测试功能
支持自定义超时时间和并发数,测试结果包含成功/失败状态和延迟时间
```

```msg
refactor(home): 使用_storage直接保存闪连状态

将_saveQuickConnectStatus方法替换为直接调用_storage.kr_saveBool
简化代码逻辑,保持存储方式的一致性
This commit is contained in:
shanshanzhong 2025-11-09 05:38:02 -08:00
parent 610eb4f20d
commit 78c20f9ae9
2 changed files with 128 additions and 1 deletions

View File

@ -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');
}
}

View 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';
}