|
| 1 | +import Foundation |
| 2 | +import ArgumentParser |
| 3 | +import prlctl |
| 4 | +import Logging |
| 5 | +import libhostmgr |
| 6 | + |
| 7 | +private let startDate = Date() |
| 8 | + |
| 9 | +struct NetworkBenchmark: ParsableCommand { |
| 10 | + |
| 11 | + static let configuration = CommandConfiguration( |
| 12 | + commandName: "network", |
| 13 | + abstract: "Test Network Speed" |
| 14 | + ) |
| 15 | + |
| 16 | + func run() throws { |
| 17 | + guard let file = try VMRemoteImageManager().list().sorted(by: { $0.size < $1.size }).first else { |
| 18 | + throw CleanExit.message("Unable to find a remote image to use as a network benchmark") |
| 19 | + } |
| 20 | + |
| 21 | + try S3Manager().streamingDownloadFile( |
| 22 | + region: Configuration.shared.vmImagesRegion, |
| 23 | + bucket: Configuration.shared.vmImagesBucket, |
| 24 | + key: file.imageKey, |
| 25 | + destination: URL(fileURLWithPath: "/dev/null"), |
| 26 | + progressCallback: self.showProgress |
| 27 | + ) |
| 28 | + } |
| 29 | + |
| 30 | + private func showProgress(availableBytes: Int, downloadedBytes: Int, totalBytes: Int64) -> Void { |
| 31 | + |
| 32 | + /// Sample only one in 100 entries |
| 33 | + guard Int.random(in: 0...1000) == 0 else { |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + let downloadedSize = ByteCountFormatter.string(fromByteCount: Int64(downloadedBytes), countStyle: .file) |
| 38 | + let totalSize = ByteCountFormatter.string(fromByteCount: totalBytes, countStyle: .file) |
| 39 | + |
| 40 | + let secondsElapsed = Date().timeIntervalSince(startDate) |
| 41 | + let perSecond = Double(downloadedBytes) / Double(secondsElapsed) |
| 42 | + let rate = ByteCountFormatter.string(fromByteCount: Int64(perSecond), countStyle: .file) |
| 43 | + |
| 44 | + logger.info("Downloaded \(downloadedSize) of \(totalSize) [Rate: \(rate) per second]") |
| 45 | + } |
| 46 | +} |
0 commit comments