使用 swift3 制作下载速度测试应用程序的最佳方法是什么?
What is the best way to make download speed test app using swift3?
我正在使用从这个问题中截取的下面的代码 Right way of determining internet speed in iOS 8 to make speed test in my app but when make a test and compare with the speed test tool http://www.speedtest.net/ 如果我的应用程序速度结果为 1mbps speedtest.net 结果,我的应用程序的结果比 speedTest.net 少一半是 2mbps 或更多
class ViewController: UIViewController, NSURLSessionDelegate, NSURLSessionDataDelegate {
override func viewDidLoad() {
super.viewDidLoad()
testDownloadSpeedWithTimout(5.0) { (megabytesPerSecond, error) -> () in
print("\(megabytesPerSecond); \(error)")
}
}
var startTime: CFAbsoluteTime!
var stopTime: CFAbsoluteTime!
var bytesReceived: Int!
var speedTestCompletionHandler: ((megabytesPerSecond: Double?, error: NSError?) -> ())!
func testDownloadSpeedWithTimout(timeout: NSTimeInterval, completionHandler:(megabytesPerSecond: Double?, error: NSError?) -> ()) {
let url = NSURL(string: "http://insert.your.site.here/yourfile")!
startTime = CFAbsoluteTimeGetCurrent()
stopTime = startTime
bytesReceived = 0
speedTestCompletionHandler = completionHandler
let configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
configuration.timeoutIntervalForResource = timeout
let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil)
session.dataTaskWithURL(url).resume()
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
bytesReceived! += data.length
stopTime = CFAbsoluteTimeGetCurrent()
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
let elapsed = stopTime - startTime
guard elapsed != 0 && (error == nil || (error?.domain == NSURLErrorDomain && error?.code == NSURLErrorTimedOut)) else {
speedTestCompletionHandler(megabytesPerSecond: nil, error: error)
return
}
let speed = elapsed != 0 ? Double(bytesReceived) / elapsed / 1024.0 / 1024.0 : -1
speedTestCompletionHandler(megabytesPerSecond: speed, error: nil)
}
}
我把我的文件放在不同的服务器上,每一个都给我不同的结果所以问题是速度结果取决于你下载的服务器,解决方案是在多个服务器上有文件并检测最佳服务器客户端并从中下载。
您的代码似乎使用单线程测试速度,这可能是它测量结果较低的主要原因之一。
Speedtest.net 和我们这样的大多数测试人员也使用多线程方法。这是估算互联网容量的更好方法 link。
您可以在此处找到 iOS 执行多线程测量的 SDK:
我正在使用从这个问题中截取的下面的代码 Right way of determining internet speed in iOS 8 to make speed test in my app but when make a test and compare with the speed test tool http://www.speedtest.net/ 如果我的应用程序速度结果为 1mbps speedtest.net 结果,我的应用程序的结果比 speedTest.net 少一半是 2mbps 或更多
class ViewController: UIViewController, NSURLSessionDelegate, NSURLSessionDataDelegate {
override func viewDidLoad() {
super.viewDidLoad()
testDownloadSpeedWithTimout(5.0) { (megabytesPerSecond, error) -> () in
print("\(megabytesPerSecond); \(error)")
}
}
var startTime: CFAbsoluteTime!
var stopTime: CFAbsoluteTime!
var bytesReceived: Int!
var speedTestCompletionHandler: ((megabytesPerSecond: Double?, error: NSError?) -> ())!
func testDownloadSpeedWithTimout(timeout: NSTimeInterval, completionHandler:(megabytesPerSecond: Double?, error: NSError?) -> ()) {
let url = NSURL(string: "http://insert.your.site.here/yourfile")!
startTime = CFAbsoluteTimeGetCurrent()
stopTime = startTime
bytesReceived = 0
speedTestCompletionHandler = completionHandler
let configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
configuration.timeoutIntervalForResource = timeout
let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil)
session.dataTaskWithURL(url).resume()
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
bytesReceived! += data.length
stopTime = CFAbsoluteTimeGetCurrent()
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
let elapsed = stopTime - startTime
guard elapsed != 0 && (error == nil || (error?.domain == NSURLErrorDomain && error?.code == NSURLErrorTimedOut)) else {
speedTestCompletionHandler(megabytesPerSecond: nil, error: error)
return
}
let speed = elapsed != 0 ? Double(bytesReceived) / elapsed / 1024.0 / 1024.0 : -1
speedTestCompletionHandler(megabytesPerSecond: speed, error: nil)
}
}
我把我的文件放在不同的服务器上,每一个都给我不同的结果所以问题是速度结果取决于你下载的服务器,解决方案是在多个服务器上有文件并检测最佳服务器客户端并从中下载。
您的代码似乎使用单线程测试速度,这可能是它测量结果较低的主要原因之一。
Speedtest.net 和我们这样的大多数测试人员也使用多线程方法。这是估算互联网容量的更好方法 link。
您可以在此处找到 iOS 执行多线程测量的 SDK: