Timeline(时间统计)
Timeline是Alamofire提供的贯穿整个request生命周期的时间统计方案,可以通过response.timeline来访问.
1Alamofire.request("https://httpbin.org/get").responseJSON { response in 2 print(response.timeline) 3}
如下打印
1Timeline: 2 { 3 "Latency": 1.474 secs, //请求到服务器响应 4 "Request Duration": 1.478 secs,//请求开始到完成 5 "Serialization Duration": 0.013 secs,//序列化时间 6 "Total Duration": 1.491 secs //总时间 7 }
URL Session Task Metrics
在iOS10中,苹果提供了URLSessionTaskMetrics的API,和timeline非常相似,它支持更多的统计数据,并支持任何类型的响应.
1Alamofire.request("https://httpbin.org/get").responseJSON { response in 2 //版本限定 3 if #available(iOS 10.0, *) { 4 print(response.metrics) 5 } 6} 7 8 9Request) <NSURLRequest: 0x60000001f9a0> { URL: https://httpbin.org/get } 10(Response) <NSHTTPURLResponse: 0x600000223f60> { URL: https://httpbin.org/get } { status code: 200, headers { 11 "Access-Control-Allow-Credentials" = true; 12 "Access-Control-Allow-Origin" = "*"; 13 Connection = "keep-alive"; 14 "Content-Length" = 375; 15 "Content-Type" = "application/json"; 16 Date = "Tue, 04 Jul 2017 00:43:51 GMT"; 17 Server = "meinheld/0.6.1"; 18 Via = "1.1 vegur"; 19 "X-Powered-By" = Flask; 20 "X-Processed-Time" = "0.00100803375244"; 21} } 22(Fetch Start) 2017-07-04 00:43:54 +0000 23(Domain Lookup Start) 2017-07-04 00:43:54 +0000 24(Domain Lookup End) 2017-07-04 00:43:54 +0000 25(Connect Start) 2017-07-04 00:43:54 +0000 26(Secure Connection Start) 2017-07-04 00:43:55 +0000 27(Secure Connection End) 2017-07-04 00:43:55 +0000 28(Connect End) 2017-07-04 00:43:55 +0000 29(Request Start) 2017-07-04 00:43:55 +0000 30(Request End) 2017-07-04 00:43:55 +0000 31(Response Start) 2017-07-04 00:43:55 +0000 32(Response End) 2017-07-04 00:43:55 +0000 33(Protocol Name) http/1.1 34(Proxy Connection) NO 35(Reused Connection) NO 36(Fetch Type) Network Load
cURL Command Output(输出请求url)
开发过程中我们都有自己拼接参数到url的经历,幸运的是Alamofire实现调试过程中直接输出请求url和参数,方便了我们在xcode以外做接口调试,比如浏览器,postman等工具.
1let request = Alamofire.request("https://httpbin.org/get", parameters: ["foo": "bar"]) 2debugPrint(request)
输出
1$ curl -i \ 2 -H "User-Agent: Alamofire/4.0.0" \ 3 -H "Accept-Encoding: gzip;q=1.0, compress;q=0.5" \ 4 -H "Accept-Language: en;q=1.0,fr;q=0.9,de;q=0.8,zh-Hans;q=0.7,zh-Hant;q=0.6,ja;q=0.5" \ 5 "https://httpbin.org/get?foo=bar"