TimespanAVGCounter.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. // ReSharper disable InconsistentNaming
  3. namespace BO.ProcessServer.Business.StatisticCounters
  4. {
  5. internal class TimespanAVGCounter : TimespanCounter
  6. {
  7. public TimeSpan TotalValue { get; private set; }
  8. public int Items { get; private set; }
  9. public TimespanAVGCounter(string name) : base(name)
  10. {
  11. Items = 0;
  12. }
  13. public override void Stop()
  14. {
  15. base.Stop();
  16. Items++;
  17. TotalValue.Add(GetValueInternal());
  18. }
  19. public override object GetValue()
  20. {
  21. var msec = GetValueInternal().TotalMilliseconds;
  22. return TimeSpan.FromMilliseconds(msec / Items);
  23. }
  24. private TimeSpan GetValueInternal()
  25. {
  26. if (!StartTimestamp.HasValue)
  27. return TimeSpan.Zero;
  28. if (!StopTimestamp.HasValue)
  29. return DateTime.Now - StartTimestamp.GetValueOrDefault();
  30. return StopTimestamp.GetValueOrDefault() - StartTimestamp.GetValueOrDefault();
  31. }
  32. }
  33. }