WatsonTcpStatistics.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. namespace WatsonTcp
  2. {
  3. using System;
  4. using System.Threading;
  5. /// <summary>
  6. /// Watson TCP statistics.
  7. /// </summary>
  8. public class WatsonTcpStatistics
  9. {
  10. #region Public-Members
  11. /// <summary>
  12. /// The time at which the client or server was started.
  13. /// </summary>
  14. public DateTime StartTime
  15. {
  16. get
  17. {
  18. return _StartTime;
  19. }
  20. }
  21. /// <summary>
  22. /// The amount of time which the client or server has been up.
  23. /// </summary>
  24. public TimeSpan UpTime
  25. {
  26. get
  27. {
  28. return DateTime.UtcNow - _StartTime;
  29. }
  30. }
  31. /// <summary>
  32. /// The number of bytes received.
  33. /// </summary>
  34. public long ReceivedBytes
  35. {
  36. get
  37. {
  38. return _ReceivedBytes;
  39. }
  40. internal set
  41. {
  42. _ReceivedBytes = value;
  43. }
  44. }
  45. /// <summary>
  46. /// The number of messages received.
  47. /// </summary>
  48. public long ReceivedMessages
  49. {
  50. get
  51. {
  52. return _ReceivedMessages;
  53. }
  54. internal set
  55. {
  56. _ReceivedMessages = value;
  57. }
  58. }
  59. /// <summary>
  60. /// Average received message size in bytes.
  61. /// </summary>
  62. public int ReceivedMessageSizeAverage
  63. {
  64. get
  65. {
  66. if (_ReceivedBytes > 0 && _ReceivedMessages > 0)
  67. {
  68. return (int)(_ReceivedBytes / _ReceivedMessages);
  69. }
  70. else
  71. {
  72. return 0;
  73. }
  74. }
  75. }
  76. /// <summary>
  77. /// The number of bytes sent.
  78. /// </summary>
  79. public long SentBytes
  80. {
  81. get
  82. {
  83. return _SentBytes;
  84. }
  85. internal set
  86. {
  87. _SentBytes = value;
  88. }
  89. }
  90. /// <summary>
  91. /// The number of messages sent.
  92. /// </summary>
  93. public long SentMessages
  94. {
  95. get
  96. {
  97. return _SentMessages;
  98. }
  99. internal set
  100. {
  101. _SentMessages = value;
  102. }
  103. }
  104. /// <summary>
  105. /// Average sent message size in bytes.
  106. /// </summary>
  107. public decimal SentMessageSizeAverage
  108. {
  109. get
  110. {
  111. if (_SentBytes > 0 && _SentMessages > 0)
  112. {
  113. return (int)(_SentBytes / _SentMessages);
  114. }
  115. else
  116. {
  117. return 0;
  118. }
  119. }
  120. }
  121. #endregion
  122. #region Private-Members
  123. private DateTime _StartTime = DateTime.UtcNow;
  124. private long _ReceivedBytes = 0;
  125. private long _ReceivedMessages = 0;
  126. private long _SentBytes = 0;
  127. private long _SentMessages = 0;
  128. #endregion
  129. #region Constructors-and-Factories
  130. /// <summary>
  131. /// Instantiate.
  132. /// </summary>
  133. public WatsonTcpStatistics()
  134. {
  135. }
  136. #endregion
  137. #region Public-Methods
  138. /// <summary>
  139. /// Return human-readable version of the object.
  140. /// </summary>
  141. /// <returns></returns>
  142. public override string ToString()
  143. {
  144. string ret =
  145. "--- Statistics ---" + Environment.NewLine +
  146. " Started : " + _StartTime.ToString() + Environment.NewLine +
  147. " Uptime : " + UpTime.ToString() + Environment.NewLine +
  148. " Received : " + Environment.NewLine +
  149. " Bytes : " + ReceivedBytes + Environment.NewLine +
  150. " Messages : " + ReceivedMessages + Environment.NewLine +
  151. " Average : " + ReceivedMessageSizeAverage + " bytes" + Environment.NewLine +
  152. " Sent : " + Environment.NewLine +
  153. " Bytes : " + SentBytes + Environment.NewLine +
  154. " Messages : " + SentMessages + Environment.NewLine +
  155. " Average : " + SentMessageSizeAverage + " bytes" + Environment.NewLine;
  156. return ret;
  157. }
  158. /// <summary>
  159. /// Reset statistics other than StartTime and UpTime.
  160. /// </summary>
  161. public void Reset()
  162. {
  163. _ReceivedBytes = 0;
  164. _ReceivedMessages = 0;
  165. _SentBytes = 0;
  166. _SentMessages = 0;
  167. }
  168. internal void IncrementReceivedMessages()
  169. {
  170. _ReceivedMessages = Interlocked.Increment(ref _ReceivedMessages);
  171. }
  172. internal void IncrementSentMessages()
  173. {
  174. _SentMessages = Interlocked.Increment(ref _SentMessages);
  175. }
  176. internal void AddReceivedBytes(long bytes)
  177. {
  178. _ReceivedBytes = Interlocked.Add(ref _ReceivedBytes, bytes);
  179. }
  180. internal void AddSentBytes(long bytes)
  181. {
  182. _SentBytes = Interlocked.Add(ref _SentBytes, bytes);
  183. }
  184. #endregion
  185. #region Private-Methods
  186. #endregion
  187. }
  188. }