Timeline.swift 5.0 KB

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