-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Release 1.10.0
- Loading branch information
Showing
19 changed files
with
538 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace gamevault.Helper | ||
{ | ||
public class ExponentialMovingAverage | ||
{ | ||
private double alpha; | ||
private double average; | ||
|
||
public ExponentialMovingAverage(double alpha) | ||
{ | ||
this.alpha = alpha; | ||
average = 0; | ||
} | ||
|
||
public void Add(double value) | ||
{ | ||
if (average == 0) | ||
average = value; | ||
else | ||
average = alpha * value + (1 - alpha) * average; | ||
} | ||
|
||
public double Calculate() | ||
{ | ||
return average; | ||
} | ||
} | ||
public class DownloadSpeedCalculator | ||
{ | ||
private ExponentialMovingAverage speedAverage; | ||
private long lastDownloadedBytes; | ||
private DateTime lastUpdateTime; | ||
|
||
public DownloadSpeedCalculator(double smoothingFactor = 0.3) | ||
{ | ||
speedAverage = new ExponentialMovingAverage(smoothingFactor); | ||
lastDownloadedBytes = 0; | ||
lastUpdateTime = DateTime.Now; | ||
} | ||
|
||
public void UpdateSpeed(long currentDownloadedBytes) | ||
{ | ||
DateTime currentTime = DateTime.Now; | ||
TimeSpan elapsedTime = currentTime - lastUpdateTime; | ||
|
||
if (elapsedTime.TotalSeconds > 0) | ||
{ | ||
double downloadSpeed = (currentDownloadedBytes - lastDownloadedBytes) / elapsedTime.TotalSeconds; | ||
speedAverage.Add(downloadSpeed); | ||
lastDownloadedBytes = currentDownloadedBytes; | ||
lastUpdateTime = currentTime; | ||
} | ||
} | ||
|
||
public double GetCurrentSpeed() | ||
{ | ||
return speedAverage.Calculate(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.