简体   繁体   中英

Calculating network speed in iOS. Round trip time with less data won't inform about exact network speed

I am trying to calculate network speed in my app, so I can upload only that much of data which can be uploaded in such network conditions. For that what my plan is:

  1. My App will have a default speed may be (500KB/s).
  2. My app will send request which have 500kb of data.
  3. It will monitor round trip time taken by system to upload data and get response. eg App received 200kb data in 5 seconds than speed will be (500+200)/5.
  4. According to above example my bandwidth is 140KB/s for that request.
  5. I'll be calculating exponential moving average from my bandwidth.
  6. I'll send next request based on the calculated average bandwidth.

Here is my implementation for NSURLSession:

//Initializing NSURLSession [one time only]
let configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("com.test.ios.background")
self.backgroundSession = NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil)

// Create session task: 
var urlSessionTask = self.backgroundSession.uploadTaskWithRequest(request, fromFile: NSURL.fileURLWithPath(filePath!)) urlSessionTask.resume() 
let requestStartTime = NSDate().timeIntervalSinceReferenceDate

//On didCompleteWithError calculating speed.
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
    let downloadEndTime = NSDate().timeIntervalSinceReferenceDate
    let networkSpeed = Int(Double(sessionTask.receivedData.length + task.countOfBytesSent) / (downloadEndTime - requestStartTime))
}

If I loose bandwidth I can find it using this approach, as my average get down, but the problem is once I reached to low bandwidth and than my network conditions get improved, my bandwidth won't get improved because I am keep sending less data in request and it will keep saying that bandwidth is low.

Is there any way by which I can find if my network condition are improved and I can send more data?

One solution in my mind is, I can send max data (may be 500KB) in every 10th request, so at that time I can get higher bandwidth in network conditions are improved.

In order to get metrics such as request start and end times, I would recommend implementing the following delegate method on your URLSession

@available(iOS 10.0, *)
optional public func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics)

The URLSessionTaskMetrics contains a property taskInterval which will give you the round trip time.

As for dynamically modifying the amount of data you send up, I don't understand the approach. Regardless of how big your request body is, your app is constrained by network bandwidth; which is out of your control. You can try leveraging a new URLSessionConfiguration property to maximize network bandwidth:

@available(iOS 11.0, *)
open var multipathServiceType: URLSessionConfiguration.MultipathServiceType

I recommend leaving the request body as-is and letting the OS deal with the finer details of optimizing for poor network connectivity.

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