Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271#if !PARCEL_BENCH_CORE_ONLY import AppKit import Foundation import ParcelTestSupport import Synchronization import Testing
@testable import ParcelPlatform
struct DisplayEventBenchmarkSample: Codable { let name: String let instructions: UInt64 let cycles: UInt64 let latencyNanoseconds: Double }
@MainActor enum DisplayEventBenchmarks { static func completionLatencyWithinBudget(_ latencies: [Double], full: Bool) -> Bool { !full || BenchmarkHarness.percentile(latencies, fraction: 0.5) < 60_000_000 }
static func run( _ harness: inout BenchmarkHarness, record: (DisplayEventBenchmarkSample) -> Void = { _ in } ) async throws { harness.measure("DisplayChangeScheduling/screen-only", iterations: 100) { var debouncer = DisplayChangeDebouncer() let start = ContinuousClock.now _ = debouncer.record(at: start) _ = debouncer.record(at: start.advanced(by: .milliseconds(25))) let published = debouncer.consumeIfDue(at: start.advanced(by: .seconds(1))) return BenchmarkObservation(checksum: published ? 1 : 2) } let fixture = DisplayEventBenchmarkFixture() defer { do { try fixture.displays.stop() } catch { Issue.record(error) } } var instructions: [Double] = [] var cycles: [Double] = [] var latencies: [Double] = [] try BenchmarkCounters.probe(read: BenchmarkCounters.processRusage) try await harness.measureAsync( "DisplayChangeLatency/screen-only", iterations: 1, reportingOnly: true, reason: "Process counters across the native timer wait include unrelated AppKit work; gate scheduling and completion instead.", latencySamples: 0 ) { let before = try BenchmarkCounters.processRusage() let start = ContinuousClock.now try await fixture.screenChange() let elapsed = BenchmarkHarness.nanoseconds(start.duration(to: .now)) let counts = try BenchmarkCounters.processRusage().delta(from: before) instructions.append(Double(counts.instructions)) cycles.append(Double(counts.cycles)) latencies.append(elapsed) record( DisplayEventBenchmarkSample( name: "DisplayChangeLatency/screen-only", instructions: counts.instructions, cycles: counts.cycles, latencyNanoseconds: elapsed)) return BenchmarkObservation(checksum: 1) } guard let index = harness.results.indices.last else { throw BenchmarkFailure.measurement("display latency result missing") } harness.results[index].proofMetrics = [ "process-instructions-p50": BenchmarkHarness.percentile(instructions, fraction: 0.5), "process-cycles-p50": BenchmarkHarness.percentile(cycles, fraction: 0.5), "event-to-publish-p50-nanoseconds": BenchmarkHarness.percentile(latencies, fraction: 0.5), "process-counter-samples": Double(instructions.count), ] try await measureCompletion(&harness, record: record) }
private static func measureCompletion( _ harness: inout BenchmarkHarness, record: (DisplayEventBenchmarkSample) -> Void ) async throws { let fixture = DisplayCompletionBenchmarkFixture() defer { do { try fixture.displays.stop() } catch { Issue.record(error) } } var instructions: [Double] = [] var cycles: [Double] = [] var latencies: [Double] = [] try await harness.measureAsync("DisplayChangeLatency", iterations: 1, latencySamples: 0) { let before = try BenchmarkCounters.processRusage() let start = ContinuousClock.now try await fixture.completion() let elapsed = BenchmarkHarness.nanoseconds(start.duration(to: .now)) let counts = try BenchmarkCounters.processRusage().delta(from: before) instructions.append(Double(counts.instructions)) cycles.append(Double(counts.cycles)) latencies.append(elapsed) record( DisplayEventBenchmarkSample( name: "DisplayChangeLatency", instructions: counts.instructions, cycles: counts.cycles, latencyNanoseconds: elapsed)) return BenchmarkObservation(checksum: 1) } guard let index = harness.results.indices.last else { throw BenchmarkFailure.measurement("display completion result missing") } harness.results[index].proofMetrics = [ "process-instructions-p50": BenchmarkHarness.percentile(instructions, fraction: 0.5), "process-cycles-p50": BenchmarkHarness.percentile(cycles, fraction: 0.5), "event-to-publish-p50-nanoseconds": BenchmarkHarness.percentile(latencies, fraction: 0.5), "process-counter-samples": Double(instructions.count), ] #if !PARCEL_EVENT_BASELINE #expect(completionLatencyWithinBudget(latencies, full: harness.full)) #endif } }
extension PerformanceTestSuite { @Suite("Display event benchmarks", .tags(.benchmark)) @MainActor struct DisplayEventBenchmarkTests { @Test( .enabled( if: ProcessInfo.processInfo.environment["PARCEL_DISPLAY_BENCH"] == "1" || ProcessInfo.processInfo.environment["PARCEL_BENCH_SMOKE"] == "1")) func displayChangeLatency() async throws { let environment = ProcessInfo.processInfo.environment let hosted = environment["GITHUB_ACTIONS"] == "true" || environment["PARCEL_BENCH_SMOKE"] == "1" let full = environment["PARCEL_DISPLAY_BENCH"] == "1" && !hosted if full && _isDebugAssertConfiguration() { throw BenchmarkFailure.debugBuild } let counters = BenchmarkCounters.startup(full: full, hosted: hosted) var harness = BenchmarkHarness(full: full, counters: counters) var samples: [DisplayEventBenchmarkSample] = [] try await DisplayEventBenchmarks.run(&harness) { samples.append($0) } let machine = try BenchmarkMachine.current() let report = try BenchmarkReport( version: BenchmarkReport.currentVersion, machine: machine.identity, host: machine.host(source: counters.source), results: harness.results) let names = Set(report.results.map(\.name)) #expect( names == [ "DisplayChangeScheduling/screen-only", "DisplayChangeLatency/screen-only", "DisplayChangeLatency", ]) #expect(!samples.isEmpty) let path = environment["PARCEL_DISPLAY_BENCH_OUTPUT"] ?? "build/bench-display.json" if full, let baselinePath = environment["PARCEL_DISPLAY_BENCH_BASELINE"] { let baseline = try JSONDecoder().decode( BenchmarkReport.self, from: Data(contentsOf: URL(fileURLWithPath: baselinePath))) try baseline.validate(caseNames: Set(baseline.results.map(\.name))) #expect(baseline.host.compatibilityKey == report.host.compatibilityKey) for reference in baseline.results { let result = try #require(report.results.first { $0.name == reference.name }) guard !result.reportingOnly else { continue } let before = try #require( reference.instructionsPerOp ?? reference.proofMetrics["process-instructions-p50"]) let after = try #require( result.instructionsPerOp ?? result.proofMetrics["process-instructions-p50"]) #expect(after <= before * 1.03, "\(result.name): instructions \(before) -> \(after)") } } let output = URL(fileURLWithPath: path) try BenchmarkBaselines.write(report, to: output, caseNames: names) let encoder = JSONEncoder() encoder.outputFormatting = [.prettyPrinted, .sortedKeys] try encoder.encode(samples).write( to: output.deletingPathExtension().appendingPathExtension("samples.json"), options: .atomic) for result in report.results { let instructions = result.instructionsPerOp.map(String.init(describing:)) ?? "async process counters in proof" print( "DISPLAY_BENCH \(result.name) instructions.p50=\(instructions) wall.p50.ns=\(result.wallMedianNanoseconds) proof=\(result.proofMetrics)" ) } } } }
#if !PARCEL_EVENT_BASELINE private final class DisplayBenchmarkCallback: Sendable { private struct Context: @unchecked Sendable { let callback: CGDisplayReconfigurationCallBack let pointer: UnsafeMutableRawPointer } private let context = Mutex<Context?>(nil)
var callbacks: DisplayReconfigurationCallbacks { DisplayReconfigurationCallbacks( register: { callback, pointer in self.context.withLock { $0 = Context(callback: callback, pointer: pointer) } return .success }, remove: { _, pointer in self.context.withLock { precondition($0?.pointer == pointer) $0 = nil } return .success }) }
func complete() { context.withLock { $0?.callback(1, .setModeFlag, $0?.pointer) } } }
#endif
@MainActor private final class DisplayCompletionBenchmarkFixture { #if PARCEL_EVENT_BASELINE let center = NotificationCenter() #else let source = DisplayBenchmarkCallback() #endif let displays: Displays private var iterator: AsyncThrowingStream<[Display], any Error>.Iterator private var initialized = false
init() { #if PARCEL_EVENT_BASELINE displays = Displays(snapshot: { [] }, screenCenter: center, workspaceCenter: NotificationCenter()) #else displays = Displays( snapshot: { [] }, screenCenter: NotificationCenter(), workspaceCenter: NotificationCenter(), reconfigurationCallbacks: source.callbacks) #endif iterator = displays.changes().makeAsyncIterator() }
func completion() async throws { var iterator = self.iterator defer { self.iterator = iterator } if !initialized { guard try await iterator.next(isolation: MainActor.shared) == [] else { throw BenchmarkFailure.measurement("display initial snapshot missing") } initialized = true } #if PARCEL_EVENT_BASELINE center.post(name: NSApplication.didChangeScreenParametersNotification, object: nil) #else source.complete() #endif guard try await iterator.next(isolation: MainActor.shared) == [] else { throw BenchmarkFailure.measurement("display completion snapshot missing") } } }
@MainActor private final class DisplayEventBenchmarkFixture { let center = NotificationCenter() let displays: Displays private var iterator: AsyncThrowingStream<[Display], any Error>.Iterator private var initialized = false
init() { displays = Displays(snapshot: { [] }, screenCenter: center, workspaceCenter: NotificationCenter()) iterator = displays.changes().makeAsyncIterator() }
func screenChange() async throws { var iterator = self.iterator defer { self.iterator = iterator } if !initialized { guard try await iterator.next(isolation: MainActor.shared) == [] else { throw BenchmarkFailure.measurement("display initial snapshot missing") } initialized = true } center.post(name: NSApplication.didChangeScreenParametersNotification, object: nil) guard try await iterator.next(isolation: MainActor.shared) == [] else { throw BenchmarkFailure.measurement("display change snapshot missing") } } }#endif