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