Validating the response API time in iOS using Swift 3?

I know that testing API response time is mostly done using Server or Backend, but since I work for a single application, I need to also check the api response time from iOS End.

How can i do this? I read a few links saying it needs to be done with the start of a timer and a timer, and then find the response time for endTime - startTime , but that seems unacceptable.

I want to use Xcode (even if XCTest exists).

Here is my one of the APIs (I wrote the whole method of using web services in a separate class in the ApiManager class):

LoginVC :

//Call Webservice
let apiManager      = ApiManager()
apiManager.delegate = self
apiManager.getUserInfoAPI()

ApiManager :

func getUserInfoAPI()  {
    //Header
    let headers =       [
        "Accept"        : "application/json",
        "Content-Type"  : "application/json",
    ]

    //Call Web API using Alamofire library
    AlamoFireSharedManagerInit()
    Alamofire.request(HCConstants.URL, method: .post, parameters: nil, encoding: JSONEncoding.default, headers: headers).responseJSON {  response in

        do{
            //Checking For Error
            if let error = response.result.error {
                //Stop AcitivityIndicator
                self.hideHud()
                //Call failure delegate method
                //print(error)
                  self.delegate?.APIFailureResponse(HCConstants.EXCEPTION_MESSAGES.SERVICE_FAILURE)
                return
            }

            //Store Response
            let responseValue = try JSONSerialization.jsonObject(with: response.data!, options: JSONSerialization.ReadingOptions()) as! Dictionary<String, AnyObject>
            print(responseValue)

            //Save token 
            if let mEmail = responseValue[HCConstants.Email] as? String {
                UserDefaults.standard.setValue(mEmail, forKey: HCConstants. mEmail)
            }

            //Stop AcitivityIndicator
            self.hideHud()
            //Check Success Flag
            if let _ = responseValue["info"] as? String {
                //Call success delegate method
                self.delegate?.apiSuccessResponse(responseValue)
            }
            else {
                //Failure message
                self.delegate?.APIFailureResponse(responseValue["message"] as? String ?? HCConstants.EXCEPTION_MESSAGES.SERVICE_FAILURE)
            }

        } catch {print("Exception is there "}
    }
}
+6
2

Timer, Date. Date, API, requestHandler API Date().timeIntervalSince(date: startDate), .

, , , :

let startDate = Date()
callMyAPI(completion: { returnValue in
    let executionTime = Date().timeIntervalSince(date: startDate)
})

Xcode - , Time Profiler , , .

: startDate . ( ): ( ) if statement .

func getUserInfoAPI()  {
    let startDate = Date()
    ...
    Alamofire.request(HCConstants.URL, method: .post, parameters: nil, encoding: JSONEncoding.default, headers: headers).responseJSON {  response in
        //calculate the time here if you only care about the time taken for the network request
        let requestExecutionTime = Date().timeIntervalSince(date: startDate)
        do{
            if let error = response.result.error {
                self.hideHud()
                let executionTimeWithError = Date().timeIntervalSince(date: startDate)
                self.delegate?.APIFailureResponse(HCConstants.EXCEPTION_MESSAGES.SERVICE_FAILURE)
                return
            }

            //Store Response
            ...
            //Check Success Flag
            if let _ = responseValue["info"] as? String {
                //Call success delegate method
                let executionTimeWithSuccess = Date().timeIntervalSince(date: startDate)
                self.delegate?.apiSuccessResponse(responseValue)
            }
            else {
                //Failure message
                let executionTimeWithFailure = Date().timeIntervalSince(date: startDate)
                self.delegate?.APIFailureResponse(responseValue["message"] as? String ?? HCConstants.EXCEPTION_MESSAGES.SERVICE_FAILURE)
            }
        } catch {print("Exception is there "}
    }
}
+13

Alamofire response.timeline.totalDuration, .

0

Source: https://habr.com/ru/post/1684376/


All Articles