Files
MacMonitor/Tests/MMMemoryTests.swift
gronod 3e8852258a
MacMonitor CI/CD Pipeline / Build & Test (Intel x86_64) (push) Canceled after 0s
feat(memory): implement MMMemoryTelemetryProvider with Mach VM breakdown, swap, and pressure (Issue #8)
2026-09-08 12:19:20 +01:00

65 lines
2.5 KiB
Swift

import XCTest
@testable import MacMonitor
final class MMMemoryTests: XCTestCase {
func testMemoryMetricCalculation() {
var vmInfo = vm_statistics64_data_t()
let pageSize: vm_size_t = 4096
let totalRAM: UInt64 = 16 * 1024 * 1024 * 1024 // 16 GB
// 1GB wired (262144 pages), 2GB app (524288 pages), 1GB compressed (262144 pages)
vmInfo.wire_count = 262_144
vmInfo.internal_page_count = 524_288
vmInfo.purgeable_count = 0
vmInfo.compressor_page_count = 262_144
vmInfo.active_count = 524_288
vmInfo.inactive_count = 1_048_576 // 4GB
vmInfo.free_count = 2_097_152 // 8GB
var swap = xsw_usage()
swap.xsu_total = 2 * 1024 * 1024 * 1024
swap.xsu_used = 512 * 1024 * 1024
swap.xsu_avail = swap.xsu_total - swap.xsu_used
withUnsafePointer(to: vmInfo) { vmPtr in
withUnsafePointer(to: swap) { swapPtr in
let metrics = MMMemoryTelemetryProvider.calculateMemoryMetrics(
withVMInfo: vmPtr,
pageSize: pageSize,
totalPhysicalRAM: totalRAM,
swapUsage: swapPtr
)
let wired = metrics["wiredBytes"] as? UInt64 ?? 0
let app = metrics["appMemoryBytes"] as? UInt64 ?? 0
let compressed = metrics["compressedBytes"] as? UInt64 ?? 0
let used = metrics["usedBytes"] as? UInt64 ?? 0
let pressureStatus = metrics["memoryPressureStatus"] as? String ?? ""
XCTAssertEqual(wired, 1024 * 1024 * 1024)
XCTAssertEqual(app, 2 * 1024 * 1024 * 1024)
XCTAssertEqual(compressed, 1024 * 1024 * 1024)
XCTAssertEqual(used, 4 * 1024 * 1024 * 1024)
XCTAssertEqual(pressureStatus, "Normal")
}
}
}
func testLiveMemoryProvider() throws {
let provider = MMMemoryTelemetryProvider()
XCTAssertTrue(provider.isAvailable)
XCTAssertEqual(provider.domain, .memory)
XCTAssertGreaterThan(provider.totalPhysicalMemoryBytes, 0)
let sample = try provider.sampleTelemetry()
let total = sample["totalBytes"] as? UInt64 ?? 0
let used = sample["usedBytes"] as? UInt64 ?? 0
let free = sample["freeBytes"] as? UInt64 ?? 0
XCTAssertGreaterThan(total, 0)
XCTAssertGreaterThan(used, 0)
XCTAssertGreaterThanOrEqual(free, 0)
}
}