import CoreGraphics import Foundation import ParcelTestSupport import Testing @testable import ParcelPlatform private final class AXWriteCounter: FrameWriteAccess { var rectangle = CGRect(x: 0, y: 0, width: 300, height: 200) var enabled = false var calls = 0 var toggles = 0 var frame: CGRect { calls += 2 return rectangle } var size: CGSize { calls += 1 return rectangle.size } var position: CGPoint { calls += 1 return rectangle.origin } var enhancedUI: Bool { calls += 1 return enabled } func setSize(_ value: CGSize) { calls += 1 rectangle.size = value } func setPosition(_ value: CGPoint) { calls += 1 rectangle.origin = value } func setEnhancedUI(_ value: Bool) { calls += 1 toggles += 1 enabled = value } } @Suite("AX write benchmarks", .serialized, .tags(.benchmark)) struct AXWriteBenchmarks { @Test("AXWriteFastPathCalls baseline") func fastPathBaseline() throws { let probe = AXWriteCounter() _ = probe.frame _ = try legacyFrameWrite( CGRect(x: 1, y: 1, width: 300, height: 200), window: probe, application: probe, assistiveTechnology: AssistiveTechnology(voiceOver: false, switchControl: false)) #expect(probe.calls == 11) print("AXWriteFastPathCalls baseline: 11 AX calls excluding enumeration") try WriteBenchmarkMeasurement.measure("AXWriteFastPathCalls/before") { let probe = AXWriteCounter() _ = probe.frame _ = try legacyFrameWrite( CGRect(x: 1, y: 1, width: 300, height: 200), window: probe, application: probe, assistiveTechnology: AssistiveTechnology(voiceOver: false, switchControl: false)) return probe.calls } } @Test("EnhancedUIToggleCount baseline") func toggleBaseline() throws { let probe = AXWriteCounter() probe.enabled = true for frame in 1...24 { _ = try legacyFrameWrite( CGRect(x: frame, y: frame, width: 300, height: 200), window: probe, application: probe, assistiveTechnology: AssistiveTechnology(voiceOver: false, switchControl: false)) } #expect(probe.toggles == 48) print("EnhancedUIToggleCount baseline: 48 toggles / 24 frames") try WriteBenchmarkMeasurement.measure("EnhancedUIToggleCount/before") { let probe = AXWriteCounter() probe.enabled = true for frame in 1...24 { _ = try legacyFrameWrite( CGRect(x: frame, y: frame, width: 300, height: 200), window: probe, application: probe, assistiveTechnology: AssistiveTechnology(voiceOver: false, switchControl: false)) } return probe.calls + probe.toggles } } @Test("AppWriteBatch/20 baseline") func batchBaseline() throws { #expect(try Self.baselineBatch() == 450) print("AppWriteBatch/20 baseline: 450 AX calls, 20 executor jobs, 20 AT main-actor queries") try WriteBenchmarkMeasurement.measure("AppWriteBatch/20/before") { try Self.baselineBatch() } } @Test("AXWriteFastPathCalls optimized") func fastPathOptimized() throws { func run() throws -> Int { let probe = AXWriteCounter() _ = try executeMotionFrameWrite( CGRect(x: 1, y: 1, width: 300, height: 200), window: probe, current: probe.rectangle, final: false) return probe.calls } #expect(try run() == 1) try WriteBenchmarkMeasurement.measure("AXWriteFastPathCalls/after", operation: run) } @Test("EnhancedUIToggleCount optimized") func toggleOptimized() throws { func run() -> (calls: Int, toggles: Int) { let probe = AXWriteCounter() probe.enabled = true let writer = AppThreadFrameWriter() let writeID = UUID() for frame in 1...24 { _ = writer.write( [ AppThreadFrameWrite( windowID: 1, target: CGRect(x: frame, y: frame, width: 300, height: 200), writeID: writeID, generation: 1, phase: frame == 24 ? .final : .ongoing) ], application: probe, assistiveTechnology: { AssistiveTechnology(voiceOver: false, switchControl: false) }, resolve: { _ in probe }) } return (probe.calls, probe.toggles) } #expect(run().toggles == 2) try WriteBenchmarkMeasurement.measure("EnhancedUIToggleCount/coordinator") { let result = run() return result.calls + result.toggles } } @Test("EnhancedUIToggleCount optimized guard") func toggleGuardOptimized() throws { func run() throws -> (calls: Int, toggles: Int) { let probe = AXWriteCounter() probe.enabled = true var guardState = EnhancedUIBurstGuard() var current = probe.frame for frame in 1...24 { let target = CGRect(x: frame, y: frame, width: 300, height: 200) try guardState.prepare( application: probe, assistiveTechnology: AssistiveTechnology(voiceOver: false, switchControl: false) ) _ = try executeMotionFrameWrite(target, window: probe, current: current, final: frame == 24) current = target } try guardState.restore(application: probe) return (probe.calls, probe.toggles) } #expect(try run().toggles == 2) try WriteBenchmarkMeasurement.measure("EnhancedUIToggleCount/after") { let result = try run() return result.calls + result.toggles } } @Test("AppWriteBatch/20 optimized") func batchOptimized() throws { func run() -> Int { let windows = (0..<20).map { _ in AXWriteCounter() } let application = AXWriteCounter() let writer = AppThreadFrameWriter() var cache = AppThreadWindowCache() for index in windows.indices { cache[UInt32(index)] = windows[index] } let writeID = UUID() let requests = windows.indices.map { AppThreadFrameWrite( windowID: UInt32($0), target: CGRect(x: 1, y: 1, width: 300, height: 200), writeID: writeID, generation: 1, phase: .final) } _ = writer.write( requests, application: application, assistiveTechnology: { AssistiveTechnology(voiceOver: false, switchControl: false) }, resolve: { id in try cache.resolve(id) { throw AppThreadFailure.windowNotFound(id) } }) return application.calls + windows.reduce(0) { $0 + $1.calls } } #expect(run() == 101) try WriteBenchmarkMeasurement.measure("AppWriteBatch/20/after", operation: run) } @Test func echoReadsDuringGlide() throws { let windows = (0..<50).map { _ in AXWriteCounter() } let indexed = Array(windows.enumerated()) let writer = AppThreadFrameWriter() _ = writer.write( (0..<50).map { AppThreadFrameWrite(windowID: UInt32($0), target: windows[$0].rectangle, generation: 1, phase: .ongoing) }, application: AXWriteCounter(), assistiveTechnology: { AssistiveTechnology(voiceOver: false, switchControl: false) }, resolve: { windows[Int($0)] }) try WriteBenchmarkMeasurement.measure("EchoReadsDuringGlide/before", iterations: 5) { var calls = 0 for event in 0..<500 { calls += 1 let window = try #require( try AppThreadWindowLookup.find( in: indexed, windowID: UInt32(event % 50), identify: { calls += 1 return UInt32($0.offset) }, onFailure: { _ in Issue.record("Lookup failed") })) _ = window.element.frame calls += 2 } return calls } let calls = windows.reduce(0) { $0 + $1.calls } try WriteBenchmarkMeasurement.measure("EchoReadsDuringGlide/after", iterations: 5) { (0..<500).reduce(0) { count, event in count + (writer.suppressesFrameNotification(windowID: UInt32(event % 50), pointerIsDown: false) ? 1 : 0) } } #expect(windows.reduce(0) { $0 + $1.calls } == calls) #expect(writer.suppressesFrameNotification(windowID: 1, pointerIsDown: false)) } @Test func queuedFinalEchoReads() throws { let probe = AXWriteCounter() probe.rectangle.origin = CGPoint(x: 10, y: 10) var tracker = AppThreadFrameWriteTracker() let id = UUID() tracker.record(windowID: 1, writeID: id, before: .zero, actual: probe.rectangle, now: 0) #expect( tracker.origin(windowID: 1, axis: .position, pointerIsDown: false, now: 1) == .possiblySelfGenerated(id)) try WriteBenchmarkMeasurement.measure("EchoReadsDuringGlide/final/before") { var echoes = 0 for _ in 0..<500 { if tracker.match(windowID: 1, axis: .position, frame: probe.frame, now: 1) == .possiblySelfGenerated(id) { echoes += 1 } } return echoes } let reads = probe.calls try WriteBenchmarkMeasurement.measure("EchoReadsDuringGlide/final/after") { var echoes = 0 for _ in 0..<500 { if tracker.origin(windowID: 1, axis: .position, pointerIsDown: false, now: 1) == .possiblySelfGenerated(id) { echoes += 1 } } return echoes } #expect(probe.calls == reads) } private static func baselineBatch() throws -> Int { let windows = (0..<20).map { _ in AXWriteCounter() } let application = AXWriteCounter() var lookupCalls = 0 for index in windows.indices { lookupCalls += 1 let window = try #require( try AppThreadWindowLookup.find( in: Array(windows.enumerated()), windowID: UInt32(index), identify: { lookupCalls += 1 return UInt32($0.offset) }, onFailure: { _ in Issue.record("Unexpected identification failure") })?.element) _ = window.frame _ = try legacyFrameWrite( CGRect(x: 1, y: 1, width: 300, height: 200), window: window, application: application, assistiveTechnology: AssistiveTechnology(voiceOver: false, switchControl: false)) } let calls = lookupCalls + application.calls + windows.reduce(0) { $0 + $1.calls } return calls } } // Captured against the unmodified write path: 11 calls, 48 toggles, 450 batch calls. // Minimum retired instructions: 1967.8, 28470.15, 544564.0 respectively (release, 20 iterations). // Retain the former path so the same counting fake can compare revisions without live AX access. private func legacyFrameWrite( _ target: CGRect, window: Window, application: Application, assistiveTechnology: AssistiveTechnology ) throws -> CGRect { let plan = FrameWritePlan(current: try window.frame, target: target) guard !plan.steps.isEmpty else { return target } var restoreEnhancedUI = false if !assistiveTechnology.voiceOver && !assistiveTechnology.switchControl { let enhancedUI: Bool do { enhancedUI = try application.enhancedUI } catch AXPlatformError.attributeUnsupported { enhancedUI = false } if enhancedUI { do { try application.setEnhancedUI(false) restoreEnhancedUI = true } catch { do { try application.setEnhancedUI(true) } catch let restoreError { throw FrameWriteFailure(writeError: error, restoreError: restoreError) } throw error } } } do { for step in plan.steps { switch step { case .size(let desired): if try window.size != desired { try window.setSize(desired) } case .position(let desired): if try window.position != desired { try window.setPosition(desired) } } } } catch { if restoreEnhancedUI { do { try application.setEnhancedUI(true) } catch let restoreError { throw FrameWriteFailure(writeError: error, restoreError: restoreError) } } throw error } if restoreEnhancedUI { try application.setEnhancedUI(true) } return try window.frame }