PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/Main/src/Samples/v0.2/PerfCounter/Filters.cs

#
C# | 91 lines | 79 code | 12 blank | 0 comment | 8 complexity | 57d6762b42b835ff1c510ab92244ee7c MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace PerfCounterChart
  6. {
  7. public class MaxSizeFilter : IFilter<PerformanceInfo>
  8. {
  9. TimeSpan length = TimeSpan.FromSeconds(10);
  10. public IList<PerformanceInfo> Filter(IList<PerformanceInfo> c)
  11. {
  12. if (c.Count == 0)
  13. return new List<PerformanceInfo>();
  14. DateTime end = c[c.Count - 1].Time;
  15. int startIndex = 0;
  16. for (int i = 0; i < c.Count; i++)
  17. {
  18. if (end - c[i].Time <= length)
  19. {
  20. startIndex = i;
  21. break;
  22. }
  23. }
  24. List<PerformanceInfo> res = new List<PerformanceInfo>(c.Count - startIndex);
  25. for (int i = startIndex; i < c.Count; i++)
  26. {
  27. res.Add(c[i]);
  28. }
  29. return res;
  30. }
  31. }
  32. public class FilterChain : IFilter<PerformanceInfo>
  33. {
  34. private readonly IFilter<PerformanceInfo>[] filters;
  35. public FilterChain(params IFilter<PerformanceInfo>[] filters)
  36. {
  37. this.filters = filters;
  38. }
  39. #region IFilter<PerformanceInfo> Members
  40. public IList<PerformanceInfo> Filter(IList<PerformanceInfo> c)
  41. {
  42. foreach (var filter in filters)
  43. {
  44. c = filter.Filter(c);
  45. }
  46. return c;
  47. }
  48. #endregion
  49. }
  50. public class AverageFilter : IFilter<PerformanceInfo>
  51. {
  52. private int number = 2;
  53. public int Number
  54. {
  55. get { return number; }
  56. set { number = value; }
  57. }
  58. public IList<PerformanceInfo> Filter(IList<PerformanceInfo> c)
  59. {
  60. int num = number - 1;
  61. if (c.Count - num <= 0)
  62. return c;
  63. List<PerformanceInfo> res = new List<PerformanceInfo>(c.Count - num);
  64. for (int i = 0; i < c.Count - num; i++)
  65. {
  66. double doubleSum = 0;
  67. long ticksSum = 0;
  68. for (int j = i; j < i + number; j++)
  69. {
  70. doubleSum += c[j].Value;
  71. ticksSum += c[j].Time.Ticks / number;
  72. }
  73. doubleSum /= number;
  74. res.Add(new PerformanceInfo { Time = new DateTime(ticksSum), Value = doubleSum });
  75. }
  76. return res;
  77. }
  78. }
  79. }