本篇帶來(lái)Alamofire中關(guān)于Timeline的一些思路
前言
Timeline翻譯后的意思是時(shí)間軸湃鹊,可以表示一個(gè)事件從開(kāi)始到結(jié)束的時(shí)間節(jié)點(diǎn)。時(shí)間軸的概念能夠應(yīng)用在很多地方,比如說(shuō)微博的主頁(yè)就是一個(gè)時(shí)間軸敷燎。
Alamofire中Timeline的代碼很少暂筝,非常簡(jiǎn)單。因此本篇文章中硬贯,我們不會(huì)把重點(diǎn)放到代碼的解讀上焕襟,我們通過(guò)追蹤Timeline的身影,來(lái)講講關(guān)于代碼設(shè)計(jì)方面的東東饭豹。
為什么要設(shè)計(jì)Timeline
很簡(jiǎn)單鸵赖,我需要知道一個(gè)請(qǐng)求過(guò)程中,每個(gè)關(guān)鍵時(shí)間點(diǎn)的值或者時(shí)間點(diǎn)與時(shí)間點(diǎn)之間的距離拄衰。這樣的一個(gè)需求不僅能夠用于程序的調(diào)試它褪,而且能為別的設(shè)計(jì)提供必要的參數(shù)支持。
我們通過(guò)下邊的代碼進(jìn)行打忧滔ぁ:
print(response.timeline)
顯示的結(jié)果是:
Timeline: { "Latency": 0.092 secs, "Request Duration": 0.092 secs, "Serialization Duration": 0.458 secs, "Total Duration": 0.551 secs }
上邊的代碼提供的信息有:
-
Latency: 0.092 secs
延遲茫打,它表示從請(qǐng)求開(kāi)始到收到或者發(fā)送第一個(gè)字節(jié)的時(shí)間長(zhǎng)度,這里把它理解成建立連接花費(fèi)的時(shí)間 -
Request Duration: 0.092 secs
請(qǐng)求時(shí)間妖混,它表示從請(qǐng)求開(kāi)始到結(jié)束的時(shí)間長(zhǎng)度老赤。這里跟Latency: 0.092 secs
都是0.092,原因是我用的POST請(qǐng)求 -
Serialization Duration: 0.458 secs
序列化用時(shí)制市,這里用了0.458秒抬旺,說(shuō)明在當(dāng)前的這個(gè)請(qǐng)求中,最耗時(shí)的操作是數(shù)據(jù)的序列化祥楣,因此开财,程序可以在這方面進(jìn)行優(yōu)化 -
Total Duration: 0.551 secs
總耗時(shí) 用序列化完成的時(shí)間點(diǎn)減去請(qǐng)求開(kāi)始的時(shí)間點(diǎn)
print(response.timeline)
之所以能夠打印出上邊這些信息,是因?yàn)樗貙?xiě)了CustomStringConvertible
協(xié)議的var description: String
误褪。當(dāng)然责鳍,如果要打印更詳細(xì)的信息,可以重寫(xiě)CustomDebugStringConvertible
的var debugDescription: String
兽间。我們通過(guò)代碼打印出來(lái):
print(response.timeline.debugDescription)
打印結(jié)果是:
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 }
如何設(shè)計(jì)Timeline
Alamofire中薇搁,不管Request請(qǐng)求成功還是失敗都會(huì)返回response。因此Timeline只有跟response綁定才合理渡八。所以應(yīng)該把他設(shè)為response的一個(gè)屬性啃洋,在之前的文章中,我們也詳細(xì)的介紹了response屎鳍,他是一個(gè)struct類(lèi)型的數(shù)據(jù)存儲(chǔ)屬性宏娄,因此在初始化的時(shí)候給Timeline賦值。
這樣我們就解決了取出Timeline的問(wèn)題逮壁,那么Timeline又是如何賦值的呢孵坚?我們?cè)贏lamofire中追蹤Timeline的身影,最終發(fā)現(xiàn)只有三個(gè)文件中出現(xiàn)了它的身影:
Response.swift
Timeline作為Response的一個(gè)屬性,肯定會(huì)出現(xiàn)在這里Timeline.swift
這是它自身的實(shí)現(xiàn)-
ResponseSerialization.swift
在這個(gè)文件中卖宠,為Request做了一個(gè)擴(kuò)展巍杈,代碼如下: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() ) } }
這個(gè)擴(kuò)展說(shuō)明Timeline的值最終是通過(guò)getter方法獲取的,獲取的是計(jì)算后的值扛伍。
對(duì)于這樣的設(shè)計(jì)筷畦,能夠給我們一些啟示,我們?cè)谠O(shè)計(jì)某一個(gè)功能的時(shí)候刺洒,盡量保持這個(gè)功能不去污染其他的程序鳖宾。我們應(yīng)該避免這樣的設(shè)計(jì):創(chuàng)建一個(gè)對(duì)象后,在程序的很多地方給它的屬性賦值
代碼
只是把代碼弄上來(lái):
/// 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 可以讓我們很清楚的查看某個(gè)網(wǎng)絡(luò)請(qǐng)求過(guò)程的耗時(shí)逆航,可以借此分析服務(wù)器端是不是有問(wèn)題鼎文,同時(shí)也可以簡(jiǎn)介的得出當(dāng)前的網(wǎng)絡(luò)情況
// 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: ", ") + " }"
}
}
總結(jié)
通過(guò)解讀源碼學(xué)到了很多,除了學(xué)到了一些平時(shí)不了解的技術(shù)外因俐,最大的收獲就是學(xué)會(huì)了從設(shè)計(jì)的角度去開(kāi)發(fā)程序拇惋。也許若干年后,你依然會(huì)記得當(dāng)初某個(gè)程序的設(shè)計(jì)思想抹剩。
有時(shí)間會(huì)寫(xiě)一個(gè)如何管理時(shí)間復(fù)雜度的文章撑帖。
由于知識(shí)水平有限,如有錯(cuò)誤吧兔,還望指出
鏈接
Alamofire源碼解讀系列(一)之概述和使用 簡(jiǎn)書(shū)-----博客園
Alamofire源碼解讀系列(二)之錯(cuò)誤處理(AFError) 簡(jiǎn)書(shū)-----博客園
Alamofire源碼解讀系列(三)之通知處理(Notification) 簡(jiǎn)書(shū)-----博客園
Alamofire源碼解讀系列(四)之參數(shù)編碼(ParameterEncoding) 簡(jiǎn)書(shū)-----博客園
Alamofire源碼解讀系列(五)之結(jié)果封裝(Result) 簡(jiǎn)書(shū)-----博客園
Alamofire源碼解讀系列(六)之Task代理(TaskDelegate) 簡(jiǎn)書(shū)-----博客園
Alamofire源碼解讀系列(七)之網(wǎng)絡(luò)監(jiān)控(NetworkReachabilityManager) 簡(jiǎn)書(shū)-----博客園
Alamofire源碼解讀系列(八)之安全策略(ServerTrustPolicy) 簡(jiǎn)書(shū)-----博客園
Alamofire源碼解讀系列(九)之響應(yīng)封裝(Response) 簡(jiǎn)書(shū)-----博客園
Alamofire源碼解讀系列(十)之序列化(ResponseSerialization) 簡(jiǎn)書(shū)-----博客園
Alamofire源碼解讀系列(十一)之多表單(MultipartFormData) 簡(jiǎn)書(shū)-----博客園