Timeline.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Timeline.swift
  2. //
  3. // Copyright (c) 2014–2016 Alamofire Software Foundation (http://alamofire.org/)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. import Foundation
  23. /// Responsible for computing the timing metrics for the complete lifecycle of a `Request`.
  24. public struct Timeline {
  25. /// The time the request was initialized.
  26. public let requestStartTime: CFAbsoluteTime
  27. /// The time the first bytes were received from or sent to the server.
  28. public let initialResponseTime: CFAbsoluteTime
  29. /// The time when the request was completed.
  30. public let requestCompletedTime: CFAbsoluteTime
  31. /// The time when the response serialization was completed.
  32. public let serializationCompletedTime: CFAbsoluteTime
  33. /// The time interval in seconds from the time the request started to the initial response from the server.
  34. public let latency: NSTimeInterval
  35. /// The time interval in seconds from the time the request started to the time the request completed.
  36. public let requestDuration: NSTimeInterval
  37. /// The time interval in seconds from the time the request completed to the time response serialization completed.
  38. public let serializationDuration: NSTimeInterval
  39. /// The time interval in seconds from the time the request started to the time response serialization completed.
  40. public let totalDuration: NSTimeInterval
  41. /**
  42. Creates a new `Timeline` instance with the specified request times.
  43. - parameter requestStartTime: The time the request was initialized.
  44. - parameter initialResponseTime: The time the first bytes were received from or sent to the server.
  45. - parameter requestCompletedTime: The time when the request was completed.
  46. - parameter serializationCompletedTime: The time when the response serialization was completed.
  47. - returns: The new `Timeline` instance.
  48. */
  49. public init(
  50. requestStartTime: CFAbsoluteTime,
  51. initialResponseTime: CFAbsoluteTime,
  52. requestCompletedTime: CFAbsoluteTime,
  53. serializationCompletedTime: CFAbsoluteTime)
  54. {
  55. self.requestStartTime = requestStartTime
  56. self.initialResponseTime = initialResponseTime
  57. self.requestCompletedTime = requestCompletedTime
  58. self.serializationCompletedTime = serializationCompletedTime
  59. self.latency = initialResponseTime - requestStartTime
  60. self.requestDuration = requestCompletedTime - requestStartTime
  61. self.serializationDuration = serializationCompletedTime - requestCompletedTime
  62. self.totalDuration = serializationCompletedTime - requestStartTime
  63. }
  64. }
  65. // MARK: - CustomStringConvertible
  66. extension Timeline: CustomStringConvertible {
  67. /// The textual representation used when written to an output stream, which includes the latency, the request
  68. /// duration and the total duration.
  69. public var description: String {
  70. let latency = String(format: "%.3f", self.latency)
  71. let requestDuration = String(format: "%.3f", self.requestDuration)
  72. let serializationDuration = String(format: "%.3f", self.serializationDuration)
  73. let totalDuration = String(format: "%.3f", self.totalDuration)
  74. let timings = [
  75. "\"Latency\": \(latency) secs",
  76. "\"Request Duration\": \(requestDuration) secs",
  77. "\"Serialization Duration\": \(serializationDuration) secs",
  78. "\"Total Duration\": \(totalDuration) secs"
  79. ]
  80. return "Timeline: { \(timings.joinWithSeparator(", ")) }"
  81. }
  82. }
  83. // MARK: - CustomDebugStringConvertible
  84. extension Timeline: CustomDebugStringConvertible {
  85. /// The textual representation used when written to an output stream, which includes the request start time, the
  86. /// initial response time, the request completed time, the serialization completed time, the latency, the request
  87. /// duration and the total duration.
  88. public var debugDescription: String {
  89. let timings = [
  90. "\"Request Start Time\": \(requestStartTime)",
  91. "\"Initial Response Time\": \(initialResponseTime)",
  92. "\"Request Completed Time\": \(requestCompletedTime)",
  93. "\"Serialization Completed Time\": \(serializationCompletedTime)",
  94. "\"Latency\": \(latency) secs",
  95. "\"Request Duration\": \(requestDuration) secs",
  96. "\"Serialization Duration\": \(serializationDuration) secs",
  97. "\"Total Duration\": \(totalDuration) secs"
  98. ]
  99. return "Timeline: { \(timings.joinWithSeparator(", ")) }"
  100. }
  101. }