-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
339 additions
and
277 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import Foundation | ||
import LCLSpeedtest | ||
|
||
@main | ||
struct Client { | ||
static func main() async throws { | ||
var client = SpeedTestClient() | ||
|
||
client.onDownloadProgress = { progress in | ||
print("progress: \(progress.convertTo(unit: .Mbps)) mbps") | ||
} | ||
try await client.start(with: .download) | ||
} | ||
} | ||
|
||
enum MeasurementUnit: String, CaseIterable, Identifiable, Encodable { | ||
|
||
case Mbps | ||
case MBps | ||
|
||
var id: Self {self} | ||
|
||
var string: String { | ||
switch self { | ||
case .Mbps: | ||
return "mbps" | ||
case .MBps: | ||
return "MB/s" | ||
} | ||
} | ||
} | ||
|
||
extension MeasurementProgress { | ||
|
||
/// data in Mbps | ||
var defaultValueInMegaBits: Double { | ||
get { | ||
self.convertTo(unit: .Mbps) | ||
} | ||
} | ||
|
||
/// data in MB/s | ||
var defaultValueInMegaBytes: Double { | ||
get { | ||
self.convertTo(unit: .MBps) | ||
} | ||
} | ||
|
||
/** | ||
Convert the measurement data to the given unit | ||
|
||
- Parameters: | ||
unit: the target unit to convert to | ||
- Returns: the value in `Double` under the specified unit measurement | ||
*/ | ||
func convertTo(unit: MeasurementUnit) -> Double { | ||
let elapsedTime = appInfo.elapsedTime | ||
let numBytes = appInfo.numBytes | ||
let time = Float64(elapsedTime) / 1000000 | ||
var speed = Float64(numBytes) / time | ||
switch unit { | ||
case .Mbps: | ||
speed *= 8 | ||
case .MBps: | ||
speed *= 1 | ||
} | ||
|
||
speed /= 1000000 | ||
return speed | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.