import ParcelTestSupport import Testing @testable import ParcelAnimation @Suite("Write motion benchmarks", .serialized, .tags(.benchmark)) struct WriteMotionBenchmarks { @Test func coalescerProMotionJitter() throws { let dispatches = try jitterReplay() #expect(dispatches == 600) print("CoalescerProMotionJitter dispatches=\(dispatches)/600") try WriteBenchmarkMeasurement.measure("CoalescerProMotionJitter") { try jitterReplay() } } @Test func animationTick200() throws { let requests = animationRequests() try WriteBenchmarkMeasurement.measure("AnimationTick200", iterations: 5) { var animator = Animator() var coalescer = FrameCoalescer() try animator.schedule(requests, at: 0) var count = 0 for tick in 0..<24 { let time = Double(tick) / 120 for frame in try animator.tick(at: time) { try coalescer.submit(frame) } for batch in try coalescer.takeReady(at: time) { count += batch.frames.count try coalescer.complete(batch, duration: 0.001, at: time) } } return count } } @Test func animationSchedule200() throws { let requests = animationRequests() try WriteBenchmarkMeasurement.measure("AnimationSchedule200") { var animator = Animator() try animator.schedule(requests, at: 0) try animator.schedule(requests, at: 0.05) return animator.activeWindowIDs.count } } @Test func perWindowRateContinuity() throws { var metric = AppWriteMetrics() try metric.record(duration: 0.0084, at: 0) #expect(metric.frameRate(fullRate: 120) >= 100) print("PerWindowRateContinuity rate=\(metric.frameRate(fullRate: 120))") try WriteBenchmarkMeasurement.measure("PerWindowRateContinuity", iterations: 1000) { Int(metric.frameRate(fullRate: 120) * 1000) } } @Test func neighbourPhaseDivergence() throws { let screen = MotionRect(originX: 0, originY: 0, width: 1000, height: 1000) let left = try FramePlanner.plan( MotionRequest( windowID: MotionWindowID(rawValue: 1), appID: MotionAppID(rawValue: 1), start: MotionRect(originX: 0, originY: 0, width: 100, height: 100), target: MotionRect(originX: 0, originY: 0, width: 200, height: 100), screen: screen), at: 0) let right = try FramePlanner.plan( MotionRequest( windowID: MotionWindowID(rawValue: 2), appID: MotionAppID(rawValue: 2), start: MotionRect(originX: 100, originY: 0, width: 200, height: 100), target: MotionRect(originX: 200, originY: 0, width: 100, height: 100), screen: screen), at: 0) func overlap() throws -> Int { var maximum = 0.0 for tick in 0...60 { let time = Double(tick) / 120 let a = try left.sample(at: time).frame.rect let b = try right.sample(at: time).frame.rect maximum = max(maximum, max(0, min(a.maxX, b.maxX) - max(a.originX, b.originX)) * 100) } return Int(maximum) } #expect(try overlap() == 0) print("NeighbourPhaseDivergence growth overlap=\(try overlap())") try WriteBenchmarkMeasurement.measure("NeighbourPhaseDivergence") { try overlap() + 1 } } @Test func neighbourArrivalPrediction() throws { let plan = try FramePlanner.plan(animationRequests()[0], at: 0) func error(predict: Bool) throws -> Int { var maximum = 0.0 for latency in [0.002, 0.020] { for tick in 0..<24 { let dispatch = Double(tick) / 120 + 0.003 let sampled = predict ? dispatch + latency : Double(tick) / 120 let sent = try plan.sample(at: sampled).frame.rect let ideal = try plan.sample(at: dispatch + latency).frame.rect maximum = max(maximum, abs(sent.originX - ideal.originX)) } } return Int(maximum) } #expect(try error(predict: true) == 0) print("NeighbourPhaseDivergence arrival error: \(try error(predict: false)) -> \(try error(predict: true)) pt") try WriteBenchmarkMeasurement.measure("NeighbourPhaseDivergence/arrival/before") { try error(predict: false) + 1 } try WriteBenchmarkMeasurement.measure("NeighbourPhaseDivergence/arrival/after") { try error(predict: true) + 1 } } @MainActor @Test func neighbourPredictionWithFutureDisplayDeadlines() throws { let plan = try FramePlanner.plan(animationRequests()[0], at: 100_000) func error(countDisplayLeadTwice: Bool) throws -> Double { var maximum = 0.0 for windows in [1, 2, 8] { for latency in [0.0, 0.001, 0.02] { var previous: Double? for tick in 0..<24 { let now = plan.startTime + Double(tick) / 120 let deadline = now + 1.0 / 120 let arrival = max(deadline, now + latency * Double(windows)) let predicted = countDisplayLeadTwice ? deadline + latency * Double(windows) : arrival let sample = min(plan.endTime, max(predicted, previous ?? deadline)) previous = sample let sent = try plan.sample(at: sample).frame.rect let ideal = try plan.sample(at: arrival).frame.rect maximum = max(maximum, abs(sent.originX - ideal.originX)) } } } return maximum } #expect(try error(countDisplayLeadTwice: true) > 0) #expect(try error(countDisplayLeadTwice: false) == 0) for (name, legacy) in [("before", true), ("after", false)] { try WriteBenchmarkMeasurement.measureShared("NeighbourPhaseDivergence/display-lead/\(name)") { let maximum = try error(countDisplayLeadTwice: legacy) return BenchmarkObservation( checksum: Int(maximum * 1000) + 1, proofMetrics: ["max-arrival-error-points": maximum]) } } } private func animationRequests() -> [MotionRequest] { (1...200).map { index in MotionRequest( windowID: MotionWindowID(rawValue: UInt64(index)), appID: MotionAppID(rawValue: Int32(index % 20)), start: MotionRect(originX: 0, originY: 0, width: 100, height: 100), target: MotionRect(originX: 500, originY: 500, width: 100, height: 100), screen: MotionRect(originX: 0, originY: 0, width: 2000, height: 2000)) } } private func jitterReplay() throws -> Int { var coalescer = FrameCoalescer(fullFrameRate: 120) var count = 0 for tick in 0..<600 { let timestamp = 100_000 + Double(tick) / 120 + (tick.isMultiple(of: 2) ? -3e-11 : 3e-11) try coalescer.submit( MotionFrame( windowID: MotionWindowID(rawValue: 1), appID: MotionAppID(rawValue: 1), generation: 0, rect: MotionRect(originX: Double(tick), originY: 0, width: 100, height: 100), opacity: 1, scale: 1, offsetHorizontal: 0, offsetVertical: 0, timestamp: timestamp, revision: 1, isFinal: false)) for batch in try coalescer.takeReady(at: timestamp) { count += 1 try coalescer.complete(batch, duration: 0.001, at: timestamp) } } return count } }