NetworkStatsCollector.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Threading;
  3. namespace Best.HTTP.Profiler.Network
  4. {
  5. public static class NetworkStatsCollector
  6. {
  7. public static long TotalNetworkBytesReceived { get => _totalNetworkBytesReceived; }
  8. private static long _totalNetworkBytesReceived;
  9. public static long TotalNetworkBytesSent { get => _totalNetworkBytesSent; }
  10. private static long _totalNetworkBytesSent;
  11. public static int TotalConnections { get => _totalConnections; }
  12. private static int _totalConnections;
  13. public static int OpenConnections { get => _openConnections; }
  14. private static int _openConnections;
  15. public static int BufferedToSend { get => _bufferedToSend; }
  16. private static int _bufferedToSend;
  17. public static int ReceivedAndUnprocessed { get => _receivedAndUnprocessed; }
  18. private static int _receivedAndUnprocessed;
  19. internal static void IncrementCurrentConnections()
  20. {
  21. Interlocked.Increment(ref _totalConnections);
  22. Interlocked.Increment(ref _openConnections);
  23. }
  24. internal static void DecrementCurrentConnections() => Interlocked.Decrement(ref _openConnections);
  25. internal static void IncrementTotalNetworkBytesReceived(int amount) => Interlocked.Add(ref _totalNetworkBytesReceived, amount);
  26. internal static void IncrementTotalNetworkBytesSent(int amount) => Interlocked.Add(ref _totalNetworkBytesSent, amount);
  27. internal static void IncrementBufferedToSend(int amount) => Interlocked.Add(ref _bufferedToSend, amount);
  28. internal static void IncrementReceivedAndUnprocessed(int amount) => Interlocked.Add(ref _receivedAndUnprocessed, amount);
  29. internal static void ResetNetworkStats()
  30. {
  31. Interlocked.Exchange(ref _totalNetworkBytesReceived, 0);
  32. Interlocked.Exchange(ref _totalNetworkBytesSent, 0);
  33. Interlocked.Exchange(ref _totalConnections, 0);
  34. Interlocked.Exchange(ref _openConnections, 0);
  35. Interlocked.Exchange(ref _bufferedToSend, 0);
  36. Interlocked.Exchange(ref _receivedAndUnprocessed, 0);
  37. }
  38. }
  39. }