本篇带来Alamofire中关于Timeline的一些思路html
Timeline翻译后的意思是时间轴,能够表示一个事件从开始到结束的时间节点。时间轴的概念可以应用在不少地方,好比说微博的主页就是一个时间轴。swift
Alamofire中Timeline的代码不多,很是简单。所以本篇文章中,咱们不会把重点放到代码的解读上,咱们经过追踪Timeline的身影,来说讲关于代码设计方面的东东。安全
很简单,我须要知道一个请求过程当中,每一个关键时间点的值或者时间点与时间点之间的距离。这样的一个需求不只可以用于程序的调试,并且能为别的设计提供必要的参数支持。服务器
咱们经过下边的代码进行打印:网络
print(response.timeline)
显示的结果是:优化
Timeline: { "Latency": 0.092 secs, "Request Duration": 0.092 secs, "Serialization Duration": 0.458 secs, "Total Duration": 0.551 secs }
上边的代码提供的信息有:编码
Latency: 0.092 secs
延迟,它表示从请求开始到收到或者发送第一个字节的时间长度,这里把它理解成创建链接花费的时间Request Duration: 0.092 secs
请求时间,它表示从请求开始到结束的时间长度。这里跟Latency: 0.092 secs
都是0.092,缘由是我用的POST请求Serialization Duration: 0.458 secs
序列化用时,这里用了0.458秒,说明在当前的这个请求中,最耗时的操做是数据的序列化,所以,程序能够在这方面进行优化Total Duration: 0.551 secs
总耗时 用序列化完成的时间点减去请求开始的时间点print(response.timeline)
之因此可以打印出上边这些信息,是由于它重写了CustomStringConvertible
协议的var description: String
。固然,若是要打印更详细的信息,能够重写CustomDebugStringConvertible
的var debugDescription: String
。咱们经过代码打印出来:翻译
print(response.timeline.debugDescription)
打印结果是:debug
Timeline: { "Request Start Time": 513055266.217, "Initial Response Time": 513055266.241, "Request Completed Time": 513055266.241, "Serialization Completed Time": 513055266.752, "Latency": 0.024 secs, "Request Duration": 0.024 secs, "Serialization Duration": 0.511 secs, "Total Duration": 0.535 secs }
Alamofire中,无论Request请求成功仍是失败都会返回response。所以Timeline只有跟response绑定才合理。因此应该把他设为response的一个属性,在以前的文章中,咱们也详细的介绍了response,他是一个struct类型的数据存储属性,所以在初始化的时候给Timeline赋值。设计
这样咱们就解决了取出Timeline的问题,那么Timeline又是如何赋值的呢?咱们在Alamofire中追踪Timeline的身影,最终发现只有三个文件中出现了它的身影:
Response.swift
Timeline做为Response的一个属性,确定会出如今这里Timeline.swift
这是它自身的实现ResponseSerialization.swift
在这个文件中,为Request作了一个扩展,代码以下:
extension Request { var timeline: Timeline { let requestCompletedTime = self.endTime ?? CFAbsoluteTimeGetCurrent() let initialResponseTime = self.delegate.initialResponseTime ?? requestCompletedTime return Timeline( requestStartTime: self.startTime ?? CFAbsoluteTimeGetCurrent(), initialResponseTime: initialResponseTime, requestCompletedTime: requestCompletedTime, serializationCompletedTime: CFAbsoluteTimeGetCurrent() ) } }
这个扩展说明Timeline的值最终是经过getter方法获取的,获取的是计算后的值。
对于这样的设计,可以给咱们一些启示,咱们在设计某一个功能的时候,尽可能保持这个功能不去污染其余的程序。咱们应该避免这样的设计:建立一个对象后,在程序的不少地方给它的属性赋值
只是把代码弄上来:
/// Responsible for computing the timing metrics for the complete lifecycle of a `Request`. public struct Timeline { /// The time the request was initialized. public let requestStartTime: CFAbsoluteTime /// The time the first bytes were received from or sent to the server. public let initialResponseTime: CFAbsoluteTime /// The time when the request was completed. public let requestCompletedTime: CFAbsoluteTime /// The time when the response serialization was completed. public let serializationCompletedTime: CFAbsoluteTime /// The time interval in seconds from the time the request started to the initial response from the server. public let latency: TimeInterval /// The time interval in seconds from the time the request started to the time the request completed. public let requestDuration: TimeInterval /// The time interval in seconds from the time the request completed to the time response serialization completed. public let serializationDuration: TimeInterval /// The time interval in seconds from the time the request started to the time response serialization completed. public let totalDuration: TimeInterval /// Creates a new `Timeline` instance with the specified request times. /// /// - parameter requestStartTime: The time the request was initialized. Defaults to `0.0`. /// - parameter initialResponseTime: The time the first bytes were received from or sent to the server. /// Defaults to `0.0`. /// - parameter requestCompletedTime: The time when the request was completed. Defaults to `0.0`. /// - parameter serializationCompletedTime: The time when the response serialization was completed. Defaults /// to `0.0`. /// /// - returns: The new `Timeline` instance. public init( requestStartTime: CFAbsoluteTime = 0.0, initialResponseTime: CFAbsoluteTime = 0.0, requestCompletedTime: CFAbsoluteTime = 0.0, serializationCompletedTime: CFAbsoluteTime = 0.0) { self.requestStartTime = requestStartTime self.initialResponseTime = initialResponseTime self.requestCompletedTime = requestCompletedTime self.serializationCompletedTime = serializationCompletedTime self.latency = initialResponseTime - requestStartTime self.requestDuration = requestCompletedTime - requestStartTime self.serializationDuration = serializationCompletedTime - requestCompletedTime self.totalDuration = serializationCompletedTime - requestStartTime } } // MARK: - CustomStringConvertible extension Timeline: CustomStringConvertible { /// The textual representation used when written to an output stream, which includes the latency, the request /// duration and the total duration. public var description: String { let latency = String(format: "%.3f", self.latency) let requestDuration = String(format: "%.3f", self.requestDuration) let serializationDuration = String(format: "%.3f", self.serializationDuration) let totalDuration = String(format: "%.3f", self.totalDuration) // NOTE: Had to move to string concatenation due to memory leak filed as rdar://26761490. Once memory leak is // fixed, we should move back to string interpolation by reverting commit 7d4a43b1. let timings = [ "\"Latency\": " + latency + " secs", "\"Request Duration\": " + requestDuration + " secs", "\"Serialization Duration\": " + serializationDuration + " secs", "\"Total Duration\": " + totalDuration + " secs" ] return "Timeline: { " + timings.joined(separator: ", ") + " }" } } /// 使用timeline 可让咱们很清楚的查看某个网络请求过程的耗时,能够借此分析服务器端是否是有问题,同时也能够简介的得出当前的网络状况 // MARK: - CustomDebugStringConvertible extension Timeline: CustomDebugStringConvertible { /// The textual representation used when written to an output stream, which includes the request start time, the /// initial response time, the request completed time, the serialization completed time, the latency, the request /// duration and the total duration. public var debugDescription: String { let requestStartTime = String(format: "%.3f", self.requestStartTime) let initialResponseTime = String(format: "%.3f", self.initialResponseTime) let requestCompletedTime = String(format: "%.3f", self.requestCompletedTime) let serializationCompletedTime = String(format: "%.3f", self.serializationCompletedTime) let latency = String(format: "%.3f", self.latency) let requestDuration = String(format: "%.3f", self.requestDuration) let serializationDuration = String(format: "%.3f", self.serializationDuration) let totalDuration = String(format: "%.3f", self.totalDuration) // NOTE: Had to move to string concatenation due to memory leak filed as rdar://26761490. Once memory leak is // fixed, we should move back to string interpolation by reverting commit 7d4a43b1. let timings = [ "\"Request Start Time\": " + requestStartTime, "\"Initial Response Time\": " + initialResponseTime, "\"Request Completed Time\": " + requestCompletedTime, "\"Serialization Completed Time\": " + serializationCompletedTime, "\"Latency\": " + latency + " secs", "\"Request Duration\": " + requestDuration + " secs", "\"Serialization Duration\": " + serializationDuration + " secs", "\"Total Duration\": " + totalDuration + " secs" ] return "Timeline: { " + timings.joined(separator: ", ") + " }" } }
经过解读源码学到了不少,除了学到了一些平时不了解的技术外,最大的收获就是学会了从设计的角度去开发程序。也许若干年后,你依然会记得当初某个程序的设计思想。
有时间会写一个如何管理时间复杂度的文章。
因为知识水平有限,若有错误,还望指出
Alamofire源码解读系列(一)之概述和使用 简书-----博客园
Alamofire源码解读系列(二)之错误处理(AFError) 简书-----博客园
Alamofire源码解读系列(三)之通知处理(Notification) 简书-----博客园
Alamofire源码解读系列(四)之参数编码(ParameterEncoding) 简书-----博客园
Alamofire源码解读系列(五)之结果封装(Result) 简书-----博客园
Alamofire源码解读系列(六)之Task代理(TaskDelegate) 简书-----博客园
Alamofire源码解读系列(七)之网络监控(NetworkReachabilityManager) 简书-----博客园
Alamofire源码解读系列(八)之安全策略(ServerTrustPolicy) 简书-----博客园
Alamofire源码解读系列(九)之响应封装(Response) 简书-----博客园