Timeline.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. Defaults to `0.0`.
  44. - parameter initialResponseTime: The time the first bytes were received from or sent to the server.
  45. Defaults to `0.0`.
  46. - parameter requestCompletedTime: The time when the request was completed. Defaults to `0.0`.
  47. - parameter serializationCompletedTime: The time when the response serialization was completed. Defaults
  48. to `0.0`.
  49. - returns: The new `Timeline` instance.
  50. */
  51. public init(
  52. requestStartTime: CFAbsoluteTime = 0.0,
  53. initialResponseTime: CFAbsoluteTime = 0.0,
  54. requestCompletedTime: CFAbsoluteTime = 0.0,
  55. serializationCompletedTime: CFAbsoluteTime = 0.0)
  56. {
  57. self.requestStartTime = requestStartTime
  58. self.initialResponseTime = initialResponseTime
  59. self.requestCompletedTime = requestCompletedTime
  60. self.serializationCompletedTime = serializationCompletedTime
  61. self.latency = initialResponseTime - requestStartTime
  62. self.requestDuration = requestCompletedTime - requestStartTime
  63. self.serializationDuration = serializationCompletedTime - requestCompletedTime
  64. self.totalDuration = serializationCompletedTime - requestStartTime
  65. }
  66. }
  67. // MARK: - CustomStringConvertible
  68. extension Timeline: CustomStringConvertible {
  69. /// The textual representation used when written to an output stream, which includes the latency, the request
  70. /// duration and the total duration.
  71. public var description: String {
  72. let latency = String(format: "%.3f", self.latency)
  73. let requestDuration = String(format: "%.3f", self.requestDuration)
  74. let serializationDuration = String(format: "%.3f", self.serializationDuration)
  75. let totalDuration = String(format: "%.3f", self.totalDuration)
  76. let timings = [
  77. "\"Latency\": \(latency) secs",
  78. "\"Request Duration\": \(requestDuration) secs",
  79. "\"Serialization Duration\": \(serializationDuration) secs",
  80. "\"Total Duration\": \(totalDuration) secs"
  81. ]
  82. return "Timeline: { \(timings.joinWithSeparator(", ")) }"
  83. }
  84. }
  85. // MARK: - CustomDebugStringConvertible
  86. extension Timeline: CustomDebugStringConvertible {
  87. /// The textual representation used when written to an output stream, which includes the request start time, the
  88. /// initial response time, the request completed time, the serialization completed time, the latency, the request
  89. /// duration and the total duration.
  90. public var debugDescription: String {
  91. let timings = [
  92. "\"Request Start Time\": \(requestStartTime)",
  93. "\"Initial Response Time\": \(initialResponseTime)",
  94. "\"Request Completed Time\": \(requestCompletedTime)",
  95. "\"Serialization Completed Time\": \(serializationCompletedTime)",
  96. "\"Latency\": \(latency) secs",
  97. "\"Request Duration\": \(requestDuration) secs",
  98. "\"Serialization Duration\": \(serializationDuration) secs",
  99. "\"Total Duration\": \(totalDuration) secs"
  100. ]
  101. return "Timeline: { \(timings.joinWithSeparator(", ")) }"
  102. }
  103. }