发布网络质量探测 1.1.2:新增客户端源文件(一)
署名:言灸
This commit is contained in:
1 parent
8ca2185c01
commit
bc39956b6c
4 files changed
+285
No files matched your search
@@ -0,0 +1,65 @@
|
||||
// Copyright (C) 2026 言灸
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
/// Latency over successful round-trips only.
|
||||
///
|
||||
/// * current: the latest successful sample, stored separately as `rttMs`
|
||||
/// * min / max: smallest and largest successful sample
|
||||
/// * mean: arithmetic mean
|
||||
/// * median: the middle sample after sorting; when the count is even, the
|
||||
/// mean of the two middle samples
|
||||
/// * p95: nearest-rank. Rank is `ceil(0.95 * n)` computed as
|
||||
/// `(95 * n + 99) ~/ 100` so binary floating point cannot move the rank.
|
||||
/// Rank is 1-based and then clamped to the sample count.
|
||||
class LatencyStats {
|
||||
const LatencyStats({
|
||||
required this.min,
|
||||
required this.mean,
|
||||
required this.median,
|
||||
required this.p95,
|
||||
required this.max,
|
||||
});
|
||||
|
||||
final double min;
|
||||
final double mean;
|
||||
final double median;
|
||||
final double p95;
|
||||
final double max;
|
||||
|
||||
static LatencyStats? from(List<double> samples) {
|
||||
if (samples.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
final sorted = [...samples]..sort();
|
||||
var sum = 0.0;
|
||||
for (final value in sorted) {
|
||||
sum += value;
|
||||
}
|
||||
final count = sorted.length;
|
||||
final median = count.isOdd
|
||||
? sorted[count ~/ 2]
|
||||
: (sorted[count ~/ 2 - 1] + sorted[count ~/ 2]) / 2;
|
||||
final rank = nearestRank95(count);
|
||||
return LatencyStats(
|
||||
min: sorted.first,
|
||||
mean: sum / count,
|
||||
median: median,
|
||||
p95: sorted[rank - 1],
|
||||
max: sorted.last,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
int nearestRank95(int count) {
|
||||
if (count <= 1) {
|
||||
return 1;
|
||||
}
|
||||
final rank = (95 * count + 99) ~/ 100;
|
||||
if (rank < 1) {
|
||||
return 1;
|
||||
}
|
||||
if (rank > count) {
|
||||
return count;
|
||||
}
|
||||
return rank;
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
// Copyright (C) 2026 言灸
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import 'sample_book.dart';
|
||||
|
||||
/// A candidate "responded" when the address itself answered: success, a
|
||||
/// protocol mismatch, or an explicit refusal. Silence, timeout and a
|
||||
/// connect error are not a response, so another address may be tried.
|
||||
bool selectionResponded(Outcome outcome) {
|
||||
switch (outcome.status) {
|
||||
case Outcome.ok:
|
||||
case Outcome.protocol:
|
||||
case Outcome.refused:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
class SelectionDecision {
|
||||
const SelectionDecision({required this.commit, this.skipped = 0});
|
||||
|
||||
final List<Outcome> commit;
|
||||
final int skipped;
|
||||
}
|
||||
|
||||
/// Separates address selection from measured probes.
|
||||
///
|
||||
/// Before any candidate has responded, failures are held. A later response
|
||||
/// drops those held failures: they are reported as a fallback diagnostic and
|
||||
/// are not added to sent, timeout or loss. If every candidate fails in the
|
||||
/// round, the held failures are committed, so the session cannot show a
|
||||
/// false 0% success. After a response, further failures are committed
|
||||
/// immediately. [endMeasurement] is called when the picker leaves the
|
||||
/// address that was being measured; the failure that caused the leave has
|
||||
/// already been committed.
|
||||
class SelectionLedger {
|
||||
SelectionLedger(this.candidateCount);
|
||||
|
||||
final int candidateCount;
|
||||
bool measuring = false;
|
||||
final List<Outcome> _held = [];
|
||||
|
||||
SelectionDecision take(Outcome outcome, {required bool responded}) {
|
||||
if (candidateCount <= 1) {
|
||||
if (responded) {
|
||||
measuring = true;
|
||||
}
|
||||
return SelectionDecision(commit: [outcome]);
|
||||
}
|
||||
if (measuring) {
|
||||
return SelectionDecision(commit: [outcome]);
|
||||
}
|
||||
if (responded) {
|
||||
final skipped = _held.length;
|
||||
final next = skipped == 0
|
||||
? outcome
|
||||
: _attach(outcome, _fallbackText(_held, outcome), skipped);
|
||||
_held.clear();
|
||||
measuring = true;
|
||||
return SelectionDecision(commit: [next], skipped: skipped);
|
||||
}
|
||||
_held.add(outcome);
|
||||
if (_held.length >= candidateCount) {
|
||||
final commit = List<Outcome>.of(_held);
|
||||
_held.clear();
|
||||
return SelectionDecision(commit: commit);
|
||||
}
|
||||
return const SelectionDecision(commit: []);
|
||||
}
|
||||
|
||||
void endMeasurement() {
|
||||
measuring = false;
|
||||
}
|
||||
}
|
||||
|
||||
Outcome _attach(Outcome outcome, String fallback, int skipped) {
|
||||
return Outcome(
|
||||
status: outcome.status,
|
||||
kind: outcome.kind,
|
||||
rttMs: outcome.rttMs,
|
||||
note: outcome.note,
|
||||
reconnect: outcome.reconnect,
|
||||
selectionSkipped: outcome.selectionSkipped + skipped,
|
||||
detail: {...outcome.detail, 'fallback': fallback},
|
||||
);
|
||||
}
|
||||
|
||||
String _fallbackText(List<Outcome> held, Outcome success) {
|
||||
final failed = held
|
||||
.map((outcome) {
|
||||
final address = outcome.detail['address'];
|
||||
final family = outcome.detail['family'];
|
||||
final where = address == null || address.isEmpty
|
||||
? '未知地址'
|
||||
: (family == null || family.isEmpty)
|
||||
? address
|
||||
: '$address($family)';
|
||||
final why = outcome.note.isEmpty ? '没有成功' : outcome.note;
|
||||
return '$where:$why';
|
||||
})
|
||||
.join(';');
|
||||
final used = success.detail['address'] ?? '';
|
||||
final usedFamily = success.detail['family'] ?? '';
|
||||
final usedText = used.isEmpty
|
||||
? '另一个地址'
|
||||
: usedFamily.isEmpty
|
||||
? used
|
||||
: '$used($usedFamily)';
|
||||
return '候选未成功:$failed。已改用 $usedText。这些失败没有计入正式丢包或失败率。';
|
||||
}
|
||||
|
||||
/// Stop discards the attempt that was still in flight. A finished attempt
|
||||
/// whose note says it was stopped is not a measured loss either.
|
||||
bool countsAfterStop(Outcome? outcome, {required bool cancelled}) {
|
||||
if (cancelled || outcome == null) {
|
||||
return false;
|
||||
}
|
||||
return outcome.note != '已停止';
|
||||
}
|
||||
Reference in new issue
Block a user