Last active
April 22, 2018 22:40
-
-
Save mjs3339/cdf7569de8bf8b87e83bee60a935e398 to your computer and use it in GitHub Desktop.
C# Calculate Items or Bytes Per Second
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
public class CalculateItemsPerSecond | |
{ | |
private readonly int _limit; | |
private readonly Stopwatch _timer; | |
private bool _running; | |
/// <summary> | |
/// Uses the average of a number of readings, so that the results are not as sporadic. | |
/// </summary> | |
private LimitedList<double> _rvlst; | |
private double _tSeconds; | |
public string ElapsedTime; | |
public string RemainingTime; | |
public string TotalTime; | |
public CalculateItemsPerSecond(int limit = 30) | |
{ | |
_timer = new Stopwatch(); | |
_limit = limit; | |
_running = false; | |
_rvlst = new LimitedList<double>(_limit); | |
_tSeconds = 0; | |
ElapsedTime = "Calculating..."; | |
RemainingTime = "Calculating..."; | |
TotalTime = "Calculating..."; | |
} | |
private void CalculateERT(double Percent) | |
{ | |
var Ratio = 100.0 / Percent; | |
var ctSeconds = _tSeconds * Ratio; | |
var etsr = new TimeSpan(TimeSpan.FromSeconds(_tSeconds).Ticks); | |
ElapsedTime = $"{etsr.Hours:00}:{etsr.Minutes:00}:{etsr.Seconds:00}"; | |
var rtsr = new TimeSpan(TimeSpan.FromSeconds(ctSeconds).Subtract(TimeSpan.FromSeconds(_tSeconds)).Ticks); | |
RemainingTime = $"{rtsr.Hours:00}:{rtsr.Minutes:00}:{rtsr.Seconds:00}"; | |
var tts = new TimeSpan(TimeSpan.FromSeconds(ctSeconds).Ticks); | |
TotalTime = $"{tts.Hours:00}:{tts.Minutes:00}:{tts.Seconds:00}"; | |
} | |
/// <summary> | |
/// Starts the timer, and sets running, and reset the limited list. | |
/// </summary> | |
public void Start() | |
{ | |
_rvlst = new LimitedList<double>(_limit); | |
_timer.Restart(); | |
_running = true; | |
} | |
/// <summary> | |
/// Stops the timer and sets as not running. | |
/// </summary> | |
public void Stop() | |
{ | |
_timer.Stop(); | |
_running = false; | |
} | |
/// <summary> | |
/// Returns the Items Per Second at the time called relative to the start() command. | |
/// If totalItems is not zero, then times (Elapsed,Remaining,Total) will also be calculated. | |
/// </summary> | |
/// <param name="itemsProcessed">The number of items processed so far.</param> | |
/// <param name="totalItems">The total number of items to be processed.</param> | |
/// <returns>The Items Per Second</returns> | |
public double ItemsPerSecond(int itemsProcessed, int totalItems) | |
{ | |
return ItemsPerSecond(itemsProcessed, (double) totalItems); | |
} | |
/// <summary> | |
/// Returns the Items Per Second at the time called relative to the start() command. | |
/// If totalItems is not zero, then times (Elapsed,Remaining,Total) will also be calculated. | |
/// </summary> | |
/// <param name="itemsProcessed">The number of items processed so far.</param> | |
/// <param name="totalItems">The total number of items to be processed.</param> | |
/// <returns>The Items Per Second</returns> | |
public double ItemsPerSecond(uint itemsProcessed, uint totalItems) | |
{ | |
return ItemsPerSecond(itemsProcessed, (double) totalItems); | |
} | |
/// <summary> | |
/// Returns the Items Per Second at the time called relative to the start() command. | |
/// If totalItems is not zero, then times (Elapsed,Remaining,Total) will also be calculated. | |
/// </summary> | |
/// <param name="itemsProcessed">The number of items processed so far.</param> | |
/// <param name="totalItems">The total number of items to be processed.</param> | |
/// <returns>The Items Per Second</returns> | |
public double ItemsPerSecond(long itemsProcessed, long totalItems) | |
{ | |
return ItemsPerSecond(itemsProcessed, (double) totalItems); | |
} | |
/// <summary> | |
/// Returns the Items Per Second at the time called relative to the start() command. | |
/// If totalItems is not zero, then times (Elapsed,Remaining,Total) will also be calculated. | |
/// </summary> | |
/// <param name="itemsProcessed">The number of items processed so far.</param> | |
/// <param name="totalItems">The total number of items to be processed.</param> | |
/// <returns>The Items Per Second</returns> | |
public double ItemsPerSecond(ulong itemsProcessed, ulong totalItems) | |
{ | |
return ItemsPerSecond(itemsProcessed, (double) totalItems); | |
} | |
/// <summary> | |
/// Returns the Items Per Second at the time called relative to the start() command. | |
/// If totalItems is not zero, then times (Elapsed,Remaining,Total) will also be calculated. | |
/// </summary> | |
/// <param name="itemsProcessed">The number of items processed so far.</param> | |
/// <param name="totalItems">The total number of items to be processed.</param> | |
/// <returns>The Items Per Second</returns> | |
public double ItemsPerSecond(float itemsProcessed, float totalItems) | |
{ | |
return ItemsPerSecond(itemsProcessed, (double) totalItems); | |
} | |
/// <summary> | |
/// Returns the Items Per Second at the time called relative to the start() command. | |
/// If totalItems is not zero, then times (Elapsed,Remaining,Total) will also be calculated. | |
/// </summary> | |
/// <param name="itemsProcessed">The number of items processed so far.</param> | |
/// <param name="totalItems">The total number of items to be processed.</param> | |
/// <returns>The Items Per Second</returns> | |
public double ItemsPerSecond(double itemsProcessed, double totalItems) | |
{ | |
if ((int) itemsProcessed == 0) | |
return 0; | |
if (totalItems > 0) | |
{ | |
var p = itemsProcessed * 100.0f / totalItems; | |
if (p > 100.0) | |
p = 100; | |
CalculateERT(p); | |
} | |
if (!_running) | |
Start(); | |
_rvlst.Add(itemsProcessed / _timer.Elapsed.TotalSeconds); | |
_tSeconds = _timer.Elapsed.TotalSeconds; | |
var rv = _rvlst.AverageEx(); | |
if (rv < 1) | |
return 0; | |
return rv; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment