Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133#if !PARCEL_BENCH_CORE_ONLY import Foundation import Testing
extension PerformanceTestSuite { @Suite("Event instruction measurements", .tags(.benchmark, .appKit)) @MainActor struct EventInstructionBenchmarks { @Test func nativeEventPaths() async throws { let environment = ProcessInfo.processInfo.environment guard let output = environment["PARCEL_EVENT_BENCH_OUTPUT"] else { return } #if DEBUG throw BenchmarkFailure.debugBuild #else try BenchmarkCounters.probe(read: BenchmarkCounters.processRusage) let machine = try BenchmarkMachine.current() var records: [EventInstructionRecord] = [] let cases = ["FocusFastPath/50", "CreatedToWriteHops", "SpaceChangeTurn", "FocusLatencyWithHungApp"] + HotPathMotionCadence.allCases.flatMap { cadence in HotPathMotionReadPolicy.allCases.map { "MoveStormCoalesce/\(cadence.rawValue)/\($0.rawValue)" } } for name in cases { var instructions: [Double] = [] var walls: [Double] = [] var metrics: [String: [Double]] = [:] for sample in 0..<14 { let fixture = HotPathNativeFixture( count: name == "FocusLatencyWithHungApp" ? 10 : 50, workspaces: name == "SpaceChangeTurn" ? 8 : 1) try await fixture.start() defer { fixture.stop() } let before = try BenchmarkCounters.processRusage() let start = ContinuousClock.now let observation: BenchmarkObservation switch name { case "FocusFastPath/50": observation = try await fixture.focus() case "CreatedToWriteHops": observation = try await fixture.created(speculate: true) case "SpaceChangeTurn": observation = try await fixture.changeSpace() case "FocusLatencyWithHungApp": observation = try await fixture.hungFocus() default: let parts = name.split(separator: "/") let cadence = try #require(HotPathMotionCadence(rawValue: String(parts[1]))) let policy = try #require(HotPathMotionReadPolicy(rawValue: String(parts[2]))) observation = try await fixture.moved(count: 500, cadence: cadence, readPolicy: policy) } let elapsed = BenchmarkHarness.nanoseconds(start.duration(to: .now)) let counts = try BenchmarkCounters.processRusage().delta(from: before) guard sample >= 3 else { continue } instructions.append(Double(counts.instructions)) walls.append(elapsed) for (key, value) in observation.proofMetrics { metrics[key, default: []].append(value) } } records.append( EventInstructionRecord( name: name, instructions: BenchmarkHarness.percentile(instructions, fraction: 0.5), wallNanoseconds: BenchmarkHarness.percentile(walls, fraction: 0.5), metrics: metrics.mapValues { BenchmarkHarness.percentile($0, fraction: 0.5) })) } let report = try EventInstructionReport( workloadVersion: 3, host: machine.host(source: .rusage), records: records) let encoder = JSONEncoder() encoder.outputFormatting = [.prettyPrinted, .sortedKeys] try encoder.encode(report).write(to: URL(fileURLWithPath: output), options: .atomic) if let baseline = environment["PARCEL_EVENT_BENCH_BASELINE"] { let previous = try JSONDecoder().decode( EventInstructionReport.self, from: Data(contentsOf: URL(fileURLWithPath: baseline))) try Self.validateCompatibility(previous, report) for record in records { let reference = try #require(previous.records.first { $0.name == record.name }) #expect( record.instructions <= reference.instructions * 1.03, "\(record.name): instructions \(reference.instructions) -> \(record.instructions)") } } for record in records { print( "EVENT_BENCH \(record.name): \(Int(record.instructions)) instructions, \(Int(record.wallNanoseconds)) ns, \(record.metrics)" ) } #endif }
@Test func baselineComparisonRejectsDifferentWorkloadsPoliciesAndHosts() throws { let record = EventInstructionRecord( name: "MoveStormCoalesce/burst/deferred", instructions: 100, wallNanoseconds: 100, metrics: [:]) let host = BenchmarkComparisonTests.host let current = EventInstructionReport(workloadVersion: 3, host: host, records: [record]) try Self.validateCompatibility(current, current) let oldWorkload = EventInstructionReport(workloadVersion: 1, host: host, records: [record]) #expect(throws: BenchmarkFailure.self) { try Self.validateCompatibility(oldWorkload, current) } let eager = EventInstructionRecord( name: "MoveStormCoalesce/burst/eager", instructions: 100, wallNanoseconds: 100, metrics: [:]) let otherPolicy = EventInstructionReport(workloadVersion: 3, host: host, records: [eager]) #expect(throws: BenchmarkFailure.self) { try Self.validateCompatibility(otherPolicy, current) } let otherHost = BenchmarkHost( hostKey: host.hostKey, model: host.model, cores: host.cores, osBuild: "other", swiftVersion: host.swiftVersion, buildConfiguration: host.buildConfiguration, counterSource: host.counterSource) let otherMachine = EventInstructionReport(workloadVersion: 3, host: otherHost, records: [record]) #expect(throws: BenchmarkFailure.self) { try Self.validateCompatibility(otherMachine, current) } }
static func validateCompatibility(_ previous: EventInstructionReport, _ current: EventInstructionReport) throws { guard previous.workloadVersion == current.workloadVersion, previous.host.compatibilityKey == current.host.compatibilityKey, Set(previous.records.map(\.name)) == Set(current.records.map(\.name)) else { throw BenchmarkFailure.measurement( "incompatible event benchmark host, workload version or policies") } } } }
struct EventInstructionReport: Codable { let workloadVersion: Int let host: BenchmarkHost let records: [EventInstructionRecord] }
struct EventInstructionRecord: Codable { let name: String let instructions: Double let wallNanoseconds: Double let metrics: [String: Double] }#endif