You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
6.8 KiB

6 years ago
5 years ago
6 years ago
  1. //
  2. // Timeline.swift
  3. //
  4. // Copyright (c) 2014 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. // NOTE: Had to move to string concatenation due to memory leak filed as rdar://26761490. Once memory leak is
  79. // fixed, we should move back to string interpolation by reverting commit 7d4a43b1.
  80. let timings = [
  81. "\"Latency\": " + latency + " secs",
  82. "\"Request Duration\": " + requestDuration + " secs",
  83. "\"Serialization Duration\": " + serializationDuration + " secs",
  84. "\"Total Duration\": " + totalDuration + " secs"
  85. ]
  86. return "Timeline: { " + timings.joined(separator: ", ") + " }"
  87. }
  88. }
  89. // MARK: - CustomDebugStringConvertible
  90. extension Timeline: CustomDebugStringConvertible {
  91. /// The textual representation used when written to an output stream, which includes the request start time, the
  92. /// initial response time, the request completed time, the serialization completed time, the latency, the request
  93. /// duration and the total duration.
  94. public var debugDescription: String {
  95. let requestStartTime = String(format: "%.3f", self.requestStartTime)
  96. let initialResponseTime = String(format: "%.3f", self.initialResponseTime)
  97. let requestCompletedTime = String(format: "%.3f", self.requestCompletedTime)
  98. let serializationCompletedTime = String(format: "%.3f", self.serializationCompletedTime)
  99. let latency = String(format: "%.3f", self.latency)
  100. let requestDuration = String(format: "%.3f", self.requestDuration)
  101. let serializationDuration = String(format: "%.3f", self.serializationDuration)
  102. let totalDuration = String(format: "%.3f", self.totalDuration)
  103. // NOTE: Had to move to string concatenation due to memory leak filed as rdar://26761490. Once memory leak is
  104. // fixed, we should move back to string interpolation by reverting commit 7d4a43b1.
  105. let timings = [
  106. "\"Request Start Time\": " + requestStartTime,
  107. "\"Initial Response Time\": " + initialResponseTime,
  108. "\"Request Completed Time\": " + requestCompletedTime,
  109. "\"Serialization Completed Time\": " + serializationCompletedTime,
  110. "\"Latency\": " + latency + " secs",
  111. "\"Request Duration\": " + requestDuration + " secs",
  112. "\"Serialization Duration\": " + serializationDuration + " secs",
  113. "\"Total Duration\": " + totalDuration + " secs"
  114. ]
  115. return "Timeline: { " + timings.joined(separator: ", ") + " }"
  116. }
  117. }