简体   繁体   中英

Alamofire slowing down on each request

I am using Alamofire to do a basic requests to an API endpoint. I noticed that the more often I do these tests, the longer the Alamofire request seems to take.

I can reproduce this behaviour with the code sample below. This does a bunch of requests and prints the duration of the request to the console. The last request is about .5 seconds slower than the first one. The amount of slow down seems to be related to the amount of JSON the API returns (our API returns much more data and the slow down is much more significant)

Am I hitting some kind of caching mechanism here?

let testURL = "https://httpbin.org/get"

for var i = 0; i < 100; i++ {

    let startDate = NSDate()
    Alamofire.request(.GET, testURL)
        .responseJSON { response in
        print("Duration of request: \(NSDate().timeIntervalSinceDate(startDate))")
    }
}

The problem here is not Alamofire, but how you are measuring the latency. You are queueing 100 requests, so the time it takes for each is very small. But since they are queued up, when the last request runs, will depend on the majority of previous requests finishing.

You should use the request timeline object to obtain the latency, with

request.timeline.totalDuration , for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM