FlowAnalyzer.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Linq;
  4. using System.Threading;
  5. namespace FastGithub.FlowAnalyze
  6. {
  7. sealed class FlowAnalyzer : IFlowAnalyzer
  8. {
  9. private const int INTERVAL_SECONDS = 5;
  10. private readonly FlowQueues readQueues = new(INTERVAL_SECONDS);
  11. private readonly FlowQueues writeQueues = new(INTERVAL_SECONDS);
  12. /// <summary>
  13. /// 收到数据
  14. /// </summary>
  15. /// <param name="flowType"></param>
  16. /// <param name="length"></param>
  17. public void OnFlow(FlowType flowType, int length)
  18. {
  19. if (flowType == FlowType.Read)
  20. {
  21. this.readQueues.OnFlow(length);
  22. }
  23. else
  24. {
  25. this.writeQueues.OnFlow(length);
  26. }
  27. }
  28. /// <summary>
  29. /// 获取流量分析
  30. /// </summary>
  31. /// <returns></returns>
  32. public FlowStatistics GetFlowStatistics()
  33. {
  34. return new FlowStatistics
  35. {
  36. TotalRead = this.readQueues.TotalBytes,
  37. TotalWrite = this.writeQueues.TotalBytes,
  38. ReadRate = this.readQueues.GetRate(),
  39. WriteRate = this.writeQueues.GetRate()
  40. };
  41. }
  42. private class FlowQueues
  43. {
  44. private int cleaning = 0;
  45. private long totalBytes = 0L;
  46. private record QueueItem(long Ticks, int Length);
  47. private readonly ConcurrentQueue<QueueItem> queues = new();
  48. private readonly int intervalSeconds;
  49. public long TotalBytes => this.totalBytes;
  50. public FlowQueues(int intervalSeconds)
  51. {
  52. this.intervalSeconds = intervalSeconds;
  53. }
  54. public void OnFlow(int length)
  55. {
  56. Interlocked.Add(ref this.totalBytes, length);
  57. this.CleanInvalidRecords();
  58. this.queues.Enqueue(new QueueItem(Environment.TickCount64, length));
  59. }
  60. public double GetRate()
  61. {
  62. this.CleanInvalidRecords();
  63. return (double)this.queues.Sum(item => item.Length) / this.intervalSeconds;
  64. }
  65. /// <summary>
  66. /// 清除无效记录
  67. /// </summary>
  68. /// <returns></returns>
  69. private bool CleanInvalidRecords()
  70. {
  71. if (Interlocked.CompareExchange(ref this.cleaning, 1, 0) != 0)
  72. {
  73. return false;
  74. }
  75. var ticks = Environment.TickCount64;
  76. while (this.queues.TryPeek(out var item))
  77. {
  78. if (ticks - item.Ticks < this.intervalSeconds * 1000)
  79. {
  80. break;
  81. }
  82. else
  83. {
  84. this.queues.TryDequeue(out _);
  85. }
  86. }
  87. Interlocked.Exchange(ref this.cleaning, 0);
  88. return true;
  89. }
  90. }
  91. }
  92. }