perf(monitors): 复用网络接口元数据并一致读取路径状态

This commit is contained in:
2026-11-16 00:06:03 +08:00
parent 4b8deb2e48
commit 97c1bc3b7f
5 changed files with 45 additions and 24 deletions
+21
View File
@@ -2,7 +2,28 @@ import Foundation
import Darwin
@main struct NetworkAddressChecks {
static func checkBaselineChanges() {
var monitor = NetworkMonitor()
var time: TimeInterval = 0
func next(_ metadata: NetworkMonitor.InterfaceMetadata) -> NetworkInterfaceMetrics {
time += 1
let bytes = UInt64(time) * 10
return monitor.update(counters: ["en0": (bytes, bytes * 2)], metadata: ["en0": metadata], at: time)[0]
}
for metadata in [NetworkMonitor.InterfaceMetadata(index: 1, addresses: ["192.0.2.1"], active: true),
.init(index: 1, addresses: ["192.0.2.2"], active: true),
.init(index: 2, addresses: ["192.0.2.2"], active: true),
.init(index: 2, addresses: ["192.0.2.2"], active: false)] {
let changed = next(metadata)
precondition(changed.download == nil && changed.upload == nil)
let stable = next(metadata)
precondition(stable.download == 10 && stable.upload == 20)
}
}
static func main() {
checkBaselineChanges()
var buffer = [CChar](repeating: 0, count: Int(NI_MAXHOST))
func check(_ address: UnsafePointer<sockaddr>) {
var original = [CChar](repeating: 0, count: Int(NI_MAXHOST))