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