TimingEvent.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. namespace Best.HTTP.Request.Timings
  3. {
  4. /// <summary>
  5. /// Struct to hold information about one timing event recorded for a <see cref="HTTPRequest"/>. Timing events are managed by the <see cref="TimingCollector"/>.
  6. /// </summary>
  7. public struct TimingEvent : IEquatable<TimingEvent>
  8. {
  9. public static readonly TimingEvent Empty = new TimingEvent(null, TimeSpan.Zero);
  10. /// <summary>
  11. /// Name of the event
  12. /// </summary>
  13. public readonly string Name;
  14. /// <summary>
  15. /// Duration of the event.
  16. /// </summary>
  17. public readonly TimeSpan Duration;
  18. /// <summary>
  19. /// When the event occurred.
  20. /// </summary>
  21. public readonly DateTime When;
  22. public TimingEvent(string name, TimeSpan duration)
  23. {
  24. this.Name = name;
  25. this.Duration = duration;
  26. this.When = DateTime.Now;
  27. }
  28. public TimingEvent(string name, DateTime when, TimeSpan duration)
  29. {
  30. this.Name = name;
  31. this.When = when;
  32. this.Duration = duration;
  33. }
  34. public TimeSpan CalculateDuration(TimingEvent @event)
  35. {
  36. if (this.When < @event.When)
  37. return @event.When - this.When;
  38. return this.When - @event.When;
  39. }
  40. public bool Equals(TimingEvent other)
  41. {
  42. return this.Name == other.Name &&
  43. this.Duration == other.Duration &&
  44. this.When == other.When;
  45. }
  46. public override bool Equals(object obj)
  47. {
  48. if (obj is TimingEvent)
  49. return this.Equals((TimingEvent)obj);
  50. return false;
  51. }
  52. public override int GetHashCode()
  53. {
  54. return (this.Name != null ? this.Name.GetHashCode() : 0) ^
  55. this.Duration.GetHashCode() ^
  56. this.When.GetHashCode();
  57. }
  58. public static bool operator ==(TimingEvent lhs, TimingEvent rhs)
  59. {
  60. return lhs.Equals(rhs);
  61. }
  62. public static bool operator !=(TimingEvent lhs, TimingEvent rhs)
  63. {
  64. return !lhs.Equals(rhs);
  65. }
  66. public override string ToString()
  67. {
  68. return string.Format("['{0}': {1}]", this.Name, this.Duration);
  69. }
  70. }
  71. }