Timeline.swift 6.2 KB

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