import Darwin import ParcelTestSupport import Testing struct BenchmarkAllocations { let blocks: Int let bytes: Int static func current() -> Self { var statistics = malloc_statistics_t() malloc_zone_statistics(nil, &statistics) return Self(blocks: Int(statistics.blocks_in_use), bytes: Int(statistics.size_in_use)) } func delta(from previous: Self, operations: Int) -> (blocks: Double, bytes: Double) { precondition(operations > 0) return ( Double(blocks - previous.blocks) / Double(operations), Double(bytes - previous.bytes) / Double(operations) ) } } extension PerformanceTestSuite { @Suite("Retained allocation diagnostics", .tags(.benchmark)) struct BenchmarkAllocationTests { @Test func deltasAreSignedAndNormalized() { let delta = BenchmarkAllocations(blocks: 8, bytes: 100).delta( from: BenchmarkAllocations(blocks: 10, bytes: 80), operations: 2) #expect(delta.blocks == -1) #expect(delta.bytes == 10) } @Test func statisticsAreReadable() { let value = BenchmarkAllocations.current() #expect(value.blocks >= 0) #expect(value.bytes >= 0) } } }