-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathSpeedTest.swift
72 lines (65 loc) · 2.36 KB
/
SpeedTest.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// SpeedTest.swift
// Lockdown
//
//
// Copyright © 2018 Confirmed, Inc. All rights reserved.
//
// Original: https://github.com/rldaulton/connectedness
import Foundation
import SystemConfiguration
import CoreTelephony
import CocoaLumberjackSwift
import PromiseKit
public class SpeedTest: NSObject, URLSessionDelegate, URLSessionDataDelegate {
var startTime: CFAbsoluteTime!
var stopTime: CFAbsoluteTime!
var bytesReceived: Int!
var fileName = "10MB.bin"
var fileSizeBytes:Double = 10485760
public func testDownloadSpeedWithTimeout(timeout: TimeInterval) -> Promise<Double> {
if let r = reachability, r.connection == .wifi {
fileName = "50MB.bin"
fileSizeBytes = 52428800
} else {
fileName = "10MB.bin"
fileSizeBytes = 10485760
}
return firstly {
Client.getSpeedTestBucket()
}
.then { speedTestBucket -> Promise<(data: Data, response: URLResponse)> in
DDLogInfo("Bucket \(speedTestBucket.bucket)")
self.startTime = CFAbsoluteTimeGetCurrent()
self.stopTime = self.startTime
self.bytesReceived = 0
let configuration = URLSessionConfiguration.ephemeral
configuration.timeoutIntervalForResource = timeout
let session = URLSession(configuration: configuration)
return session.dataTask(.promise,
with: try self.makeDownloadRequest(urlString: "https://\(speedTestBucket.bucket).s3-accelerate.amazonaws.com/\(self.fileName)"))
.validate()
}
.map { data, response -> Double in
self.stopTime = CFAbsoluteTimeGetCurrent()
let elapsed = self.stopTime - self.startTime
if elapsed == 0 {
throw "File download failed: no time elapsed."
}
else {
return self.fileSizeBytes / elapsed / 1024.0 / 1024.0
}
}
}
func makeDownloadRequest(urlString: String) throws -> URLRequest {
if let url = URL(string: urlString) {
var rq = URLRequest(url: url)
rq.cachePolicy = .reloadIgnoringLocalAndRemoteCacheData
rq.httpMethod = "GET"
return rq
}
else {
throw "Invalid URL string: \(urlString)"
}
}
}