🚀 Speed Test

Speed Test / 测速

NetworkDiagnostic.runSpeedTest() measures download and upload throughput and samples latency during the run, reporting progress through onProgress.

NetworkDiagnostic.runSpeedTest() 测量下载与上传吞吐量,并在过程中采样延迟,通过 onProgress 汇报进度。

Basic / 基础

final speed = await NetworkDiagnostic.runSpeedTest(
  includeUpload: true,
  includePing: true,
  onProgress: (progress) {
    print('${progress.phase.name}: '
          '${progress.speedMbps.toStringAsFixed(1)} Mbps '
          '(${progress.bytes} bytes, ${progress.elapsed.inMilliseconds} ms)');
  },
);
 
print('download : ${speed.downloadSpeed.toStringAsFixed(2)} Mbps');
print('upload   : ${speed.uploadSpeed.toStringAsFixed(2)} Mbps');
print('ping     : ${speed.ping.toStringAsFixed(1)} ms');
print('jitter   : ${speed.jitter.toStringAsFixed(2)} ms');
print('loss     : ${speed.packetLoss.toStringAsFixed(1)} %');
print('server   : ${speed.server}');
print('duration : ${speed.duration.inMilliseconds} ms');
ParameterDefaultMeaning
downloadUrl / uploadUrlfrom configEndpoints used
timeoutconfig.speedTestTimeoutPer-request timeout
maxDurationconfig.speedTestMaxDurationSampling window per phase
uploadPayloadBytesconfig.uploadPayloadBytesUpload size
pingHost / pingCountfrom configLatency sampled during the test
includeUploadtrueSkip the upload phase
includePingtrueSkip the latency sample
onProgressnullCalled repeatedly during both phases

Progress phases: SpeedTestPhase.downloadSpeedTestPhase.uploadSpeedTestPhase.completed.

进度阶段依次为 downloaduploadcompleted

Fast, low-traffic variant / 轻量低流量变体

final quick = await NetworkDiagnostic.runSpeedTest(
  includeUpload: false,
  includePing: false,
  maxDuration: const Duration(seconds: 5),
);

The default endpoints point at Cloudflare’s public speed service and move real traffic (≈25 MB download by default). Ask for consent before running it on a metered connection, and override downloadUrl / uploadUrl for production. 默认端点指向 Cloudflare 公共服务并会产生真实流量(默认下载约 25 MB)。在按量计费 网络上运行前请征得同意,并为生产环境替换 downloadUrl / uploadUrl