opt: speed test

Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
bggRGjQaUbCoE
2025-01-07 10:54:58 +08:00
parent f5b50ffcb0
commit 488cb58b85

View File

@@ -53,22 +53,20 @@ class _SelectDialogState<T> extends State<SelectDialog<T>> {
..options.headers['referer'] = 'https://www.bilibili.com/';
int maxSize = 8 * 1024 * 1024;
int downloaded = 0;
Stopwatch stopwatch = Stopwatch()..start();
int start = DateTime.now().millisecondsSinceEpoch;
try {
await dio.get(
videoUrl,
onReceiveProgress: (int count, int total) {
downloaded += count;
if (stopwatch.elapsedMilliseconds > 15 * 1000) {
stopwatch.stop();
int now = DateTime.now().millisecondsSinceEpoch;
if (now - start > 15 * 1000) {
dio.close(force: true);
}
if (downloaded >= maxSize) {
stopwatch.stop();
dio.close(force: true);
_cdnResList[item.index] =
(maxSize / stopwatch.elapsedMilliseconds / 1000)
.toPrecision(2);
(maxSize / (now - start) / 1000).toPrecision(2);
if (mounted) {
setState(() {});
}
@@ -76,7 +74,6 @@ class _SelectDialogState<T> extends State<SelectDialog<T>> {
},
);
} catch (e) {
stopwatch.stop();
if (_cdnResList[item.index] == null) {
_cdnResList[item.index] = '测速失败: $e';
if (mounted) {
@@ -84,9 +81,6 @@ class _SelectDialogState<T> extends State<SelectDialog<T>> {
}
}
}
if (stopwatch.isRunning) {
stopwatch.stop();
}
}
}
} catch (e) {