实时返回速度测试
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
2addbe30dd
commit
6dcdfe4d23
|
|
@ -916,12 +916,29 @@ namespace IRaCIS.Core.API.Controllers
|
|||
var notifyInterval = TimeSpan.FromSeconds(1);
|
||||
var lastNotify = DateTime.UtcNow;
|
||||
|
||||
// 用于计算下载速度
|
||||
long lastReceivedSize = 0;
|
||||
DateTime lastSpeedCheck = DateTime.UtcNow;
|
||||
|
||||
async Task NotifyProgressAsync(bool force = false)
|
||||
{
|
||||
if (!force && DateTime.UtcNow - lastNotify < notifyInterval)
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
var elapsedSeconds = (now - lastSpeedCheck).TotalSeconds;
|
||||
|
||||
// 如果没有强制推送,并且未到推送间隔,则返回
|
||||
if (!force && elapsedSeconds < notifyInterval.TotalSeconds)
|
||||
return;
|
||||
|
||||
// 计算下载速度(字节/秒)
|
||||
double speedBps = 0;
|
||||
if (elapsedSeconds > 0)
|
||||
{
|
||||
speedBps = (receivedSize - lastReceivedSize) / elapsedSeconds;
|
||||
}
|
||||
|
||||
lastSpeedCheck = now;
|
||||
lastReceivedSize = receivedSize;
|
||||
lastNotify = DateTime.UtcNow;
|
||||
|
||||
var progress = new
|
||||
|
|
@ -932,7 +949,11 @@ namespace IRaCIS.Core.API.Controllers
|
|||
|
||||
SizePercent = totalSize > 0
|
||||
? Math.Round(receivedSize * 100m / totalSize, 2).ToString() + "%"
|
||||
: "0%"
|
||||
: "0%",
|
||||
|
||||
Speed = (speedBps / 1024 >= 1024
|
||||
? (speedBps / 1024 / 1024).ToString("0.00") + " MB/s"
|
||||
: (speedBps / 1024).ToString("0.00") + " KB/s")
|
||||
};
|
||||
|
||||
// 不阻塞下载流程
|
||||
|
|
|
|||
Loading…
Reference in New Issue