mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-06 00:25:43 +00:00
feat: smooth speed calculation (#99)
This commit is contained in:
1 parent
e77441f0de
commit
701ea59035
2 files changed
+17
-4
No files matched your search
@@ -97,8 +97,9 @@ func (d *Downloader) Setup() error {
|
||||
}
|
||||
d.tasks = tasks
|
||||
|
||||
// 每秒统计一次下载速度
|
||||
// 每个tick统计一次下载速度
|
||||
go func() {
|
||||
|
||||
for !d.closed.Load() {
|
||||
if len(d.tasks) > 0 {
|
||||
for _, task := range d.tasks {
|
||||
@@ -117,9 +118,7 @@ func (d *Downloader) Setup() error {
|
||||
|
||||
current := task.fetcher.Progress().TotalDownloaded()
|
||||
task.Progress.Used = task.timer.Used()
|
||||
currUsedTime := float64(d.refreshInterval) / 1000
|
||||
currSpeed := int64(float64(current-task.Progress.Downloaded) / currUsedTime)
|
||||
task.Progress.Speed = (task.Progress.Speed + currSpeed) / 2
|
||||
task.Progress.Speed = task.calcSpeed(current-task.Progress.Downloaded, float64(d.refreshInterval)/1000)
|
||||
task.Progress.Downloaded = current
|
||||
d.emit(EventKeyProgress, task)
|
||||
|
||||
@@ -516,6 +515,7 @@ func (d *Downloader) restoreFetcher(task *Task) error {
|
||||
func initTask(task *Task) {
|
||||
task.timer = &util.Timer{}
|
||||
task.lock = &sync.Mutex{}
|
||||
task.speedArr = make([]int64, 0)
|
||||
}
|
||||
|
||||
var defaultDownloader = NewDownloader(nil)
|
||||
|
||||
@@ -29,6 +29,7 @@ type Task struct {
|
||||
fetcher fetcher.Fetcher
|
||||
timer *util.Timer
|
||||
lock *sync.Mutex
|
||||
speedArr []int64
|
||||
}
|
||||
|
||||
func NewTask() *Task {
|
||||
@@ -54,6 +55,18 @@ func (t *Task) clone() *Task {
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Task) calcSpeed(downloaded int64, usedTime float64) int64 {
|
||||
t.speedArr = append(t.speedArr, downloaded)
|
||||
if len(t.speedArr) > 6 {
|
||||
t.speedArr = t.speedArr[1:]
|
||||
}
|
||||
var total int64
|
||||
for _, v := range t.speedArr {
|
||||
total += v
|
||||
}
|
||||
return int64(float64(total) / float64(len(t.speedArr)) / usedTime)
|
||||
}
|
||||
|
||||
type DownloaderConfig struct {
|
||||
Controller *controller.Controller
|
||||
FetchBuilders []fetcher.FetcherBuilder
|
||||
|
||||
Reference in new issue
Block a user