Skip to content

Commit 87090e1

Browse files
authored
Merge pull request #9 from Automattic/add/benchmarking
Add Benchmarking + Configuration Backward Compatibility
2 parents f22e0c3 + d431781 commit 87090e1

21 files changed

+457
-194
lines changed

Package.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,24 @@ let package = Package(
3131
.product(name: "Tqdm", package: "swift-tqdm"),
3232
.product(name: "Logging", package: "swift-log"),
3333
.product(name: "kcpassword", package: "kcpassword"),
34+
.target(name: "libhostmgr")
3435
],
3536
exclude: ["resources"]
3637
),
38+
.target(
39+
name: "libhostmgr",
40+
dependencies: [
41+
.product(name: "ArgumentParser", package: "swift-argument-parser"),
42+
.product(name: "SotoS3", package: "soto"),
43+
]
44+
),
3745
.testTarget(
38-
name: "hostmgrTests",
39-
dependencies: ["hostmgr"]
46+
name: "libhostmgrTests",
47+
dependencies: ["libhostmgr"],
48+
resources: [
49+
.copy("resources/configurations/0.6.0.json"),
50+
.copy("resources/configurations/defaults.json"),
51+
]
4052
),
4153
]
4254
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Foundation
2+
import ArgumentParser
3+
4+
struct BenchmarkCommand: ParsableCommand {
5+
static let configuration = CommandConfiguration(
6+
commandName: "benchmark",
7+
abstract: "System tests",
8+
subcommands: [
9+
NetworkBenchmark.self,
10+
DiskBenchmark.self
11+
]
12+
)
13+
}

Sources/hostmgr/SyncCommand.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Foundation
22
import ArgumentParser
3+
import libhostmgr
34

45
struct SyncCommand: ParsableCommand {
56
static let configuration = CommandConfiguration(
@@ -15,7 +16,7 @@ struct SyncCommand: ParsableCommand {
1516
var list: Bool = false
1617

1718
@Argument
18-
var task: SchedulableSyncCommand?
19+
var task: Configuration.SchedulableSyncCommand?
1920

2021
func run() throws {
2122

@@ -25,7 +26,7 @@ struct SyncCommand: ParsableCommand {
2526
}
2627

2728
if list {
28-
SchedulableSyncCommand.allCases.forEach { print($0) }
29+
Configuration.SchedulableSyncCommand.allCases.forEach { print($0) }
2930
return
3031
}
3132

@@ -38,10 +39,10 @@ struct SyncCommand: ParsableCommand {
3839
}
3940
}
4041

41-
private func perform(task: SchedulableSyncCommand) throws {
42+
private func perform(task: Configuration.SchedulableSyncCommand) throws {
4243
switch task {
43-
case .authorized_keys: try SyncAuthorizedKeysTask().run()
44-
case .vm_images: try SyncVMImagesTask().run()
44+
case .authorizedKeys: try SyncAuthorizedKeysTask().run()
45+
case .vmImages: try SyncVMImagesTask().run()
4546
}
4647
}
4748
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import Foundation
2+
import ArgumentParser
3+
import prlctl
4+
import Logging
5+
6+
private let startDate = Date()
7+
8+
struct DiskBenchmark: ParsableCommand {
9+
static let configuration = CommandConfiguration(
10+
commandName: "disk",
11+
abstract: "Test Disk Write"
12+
)
13+
14+
func run() throws {
15+
let byteCount = 4096
16+
let bytes = Data([UInt8](repeating: 0, count: byteCount))
17+
let tempFile = try FileManager.default.createTemporaryFile()
18+
let file = try FileHandle(forWritingTo: tempFile)
19+
20+
var totalBytesWritten: Int64 = 0
21+
22+
while true {
23+
file.write(bytes)
24+
totalBytesWritten += Int64(byteCount)
25+
26+
/// Sample only one in 100 entries
27+
guard Int.random(in: 0...1000) == 0 else {
28+
continue
29+
}
30+
31+
let writtenSize = ByteCountFormatter.string(fromByteCount: totalBytesWritten, countStyle: .file)
32+
33+
let secondsElapsed = Date().timeIntervalSince(startDate)
34+
let perSecond = Double(totalBytesWritten) / Double(secondsElapsed)
35+
let rate = ByteCountFormatter.string(fromByteCount: Int64(perSecond), countStyle: .file)
36+
37+
print("\(writtenSize) written [Rate: \(rate) per second]")
38+
}
39+
}
40+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
}

Sources/hostmgr/commands/generate/GenerateBuildkiteJobScript.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Foundation
22
import ArgumentParser
33
import prlctl
4+
import libhostmgr
45

56
struct GenerateBuildkiteJobScript: ParsableCommand {
67

Sources/hostmgr/commands/generate/GenerateGitMirrorManifestCommand.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Foundation
22
import ArgumentParser
33
import Logging
4+
import libhostmgr
45

56
struct GenerateGitMirrorManifestCommand: ParsableCommand {
67

Sources/hostmgr/commands/sync/SyncAuthorizedKeysCommand.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Foundation
22
import ArgumentParser
33
import SotoS3
4+
import libhostmgr
45

56
struct SyncAuthorizedKeysCommand: ParsableCommand {
67

Sources/hostmgr/commands/sync/SyncVMImagesCommand.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Foundation
22
import ArgumentParser
33
import SotoS3
44
import prlctl
5+
import libhostmgr
56

67
struct SyncVMImagesCommand: ParsableCommand {
78

Sources/hostmgr/commands/vm/image/local/VMLocalImageManager.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Foundation
22
import prlctl
3+
import libhostmgr
34

45
struct VMLocalImageManager {
56

0 commit comments

Comments
 (0)