123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- namespace WatsonTcp
- {
- using System;
- using System.Threading;
- /// <summary>
- /// Watson TCP statistics.
- /// </summary>
- public class WatsonTcpStatistics
- {
- #region Public-Members
- /// <summary>
- /// The time at which the client or server was started.
- /// </summary>
- public DateTime StartTime
- {
- get
- {
- return _StartTime;
- }
- }
- /// <summary>
- /// The amount of time which the client or server has been up.
- /// </summary>
- public TimeSpan UpTime
- {
- get
- {
- return DateTime.UtcNow - _StartTime;
- }
- }
- /// <summary>
- /// The number of bytes received.
- /// </summary>
- public long ReceivedBytes
- {
- get
- {
- return _ReceivedBytes;
- }
- internal set
- {
- _ReceivedBytes = value;
- }
- }
- /// <summary>
- /// The number of messages received.
- /// </summary>
- public long ReceivedMessages
- {
- get
- {
- return _ReceivedMessages;
- }
- internal set
- {
- _ReceivedMessages = value;
- }
- }
- /// <summary>
- /// Average received message size in bytes.
- /// </summary>
- public int ReceivedMessageSizeAverage
- {
- get
- {
- if (_ReceivedBytes > 0 && _ReceivedMessages > 0)
- {
- return (int)(_ReceivedBytes / _ReceivedMessages);
- }
- else
- {
- return 0;
- }
- }
- }
- /// <summary>
- /// The number of bytes sent.
- /// </summary>
- public long SentBytes
- {
- get
- {
- return _SentBytes;
- }
- internal set
- {
- _SentBytes = value;
- }
- }
- /// <summary>
- /// The number of messages sent.
- /// </summary>
- public long SentMessages
- {
- get
- {
- return _SentMessages;
- }
- internal set
- {
- _SentMessages = value;
- }
- }
- /// <summary>
- /// Average sent message size in bytes.
- /// </summary>
- public decimal SentMessageSizeAverage
- {
- get
- {
- if (_SentBytes > 0 && _SentMessages > 0)
- {
- return (int)(_SentBytes / _SentMessages);
- }
- else
- {
- return 0;
- }
- }
- }
- #endregion
- #region Private-Members
- private DateTime _StartTime = DateTime.UtcNow;
- private long _ReceivedBytes = 0;
- private long _ReceivedMessages = 0;
- private long _SentBytes = 0;
- private long _SentMessages = 0;
- #endregion
- #region Constructors-and-Factories
- /// <summary>
- /// Instantiate.
- /// </summary>
- public WatsonTcpStatistics()
- {
- }
- #endregion
- #region Public-Methods
- /// <summary>
- /// Return human-readable version of the object.
- /// </summary>
- /// <returns></returns>
- public override string ToString()
- {
- string ret =
- "--- Statistics ---" + Environment.NewLine +
- " Started : " + _StartTime.ToString() + Environment.NewLine +
- " Uptime : " + UpTime.ToString() + Environment.NewLine +
- " Received : " + Environment.NewLine +
- " Bytes : " + ReceivedBytes + Environment.NewLine +
- " Messages : " + ReceivedMessages + Environment.NewLine +
- " Average : " + ReceivedMessageSizeAverage + " bytes" + Environment.NewLine +
- " Sent : " + Environment.NewLine +
- " Bytes : " + SentBytes + Environment.NewLine +
- " Messages : " + SentMessages + Environment.NewLine +
- " Average : " + SentMessageSizeAverage + " bytes" + Environment.NewLine;
- return ret;
- }
- /// <summary>
- /// Reset statistics other than StartTime and UpTime.
- /// </summary>
- public void Reset()
- {
- _ReceivedBytes = 0;
- _ReceivedMessages = 0;
- _SentBytes = 0;
- _SentMessages = 0;
- }
- internal void IncrementReceivedMessages()
- {
- _ReceivedMessages = Interlocked.Increment(ref _ReceivedMessages);
- }
- internal void IncrementSentMessages()
- {
- _SentMessages = Interlocked.Increment(ref _SentMessages);
- }
- internal void AddReceivedBytes(long bytes)
- {
- _ReceivedBytes = Interlocked.Add(ref _ReceivedBytes, bytes);
- }
- internal void AddSentBytes(long bytes)
- {
- _SentBytes = Interlocked.Add(ref _SentBytes, bytes);
- }
- #endregion
- #region Private-Methods
- #endregion
- }
- }
|