import AppKit import Darwin import ParcelConfig import ParcelCore import ParcelTestSupport import QuartzCore import Testing @testable import ParcelEngine @testable import ParcelUI @_silgen_name("thread_selfcounts") private func overlayThreadCounts(_ flavor: Int32, _ buffer: UnsafeMutableRawPointer, _ size: Int32) -> Int32 @MainActor enum OverlayBenchmark { static func measure(_ name: String, iterations: Int = 100, body: () throws -> Int) throws { let full = ProcessInfo.processInfo.environment["PARCEL_OVERLAY_BENCH"] == "1" if full && _isDebugAssertConfiguration() { throw BenchmarkFailure.debugBuild } let count = full ? iterations : 2 var checksum = 0 for _ in 0.. before else { throw BenchmarkFailure.measurement("instruction counter did not advance") } instructions.append(Double(after - before) / Double(count)) } #expect(checksum != 0) print( "OVERLAY_BENCH \(name): \(Int(BenchmarkHarness.percentile(instructions, fraction: 0.5))) instructions, \(String(format: "%.3f", BenchmarkHarness.percentile(times, fraction: 0.5) / 1000)) µs" ) } private static func instructionCount() throws -> UInt64 { guard dlsym(UnsafeMutableRawPointer(bitPattern: -2), "thread_selfcounts") != nil else { throw BenchmarkFailure.measurement("thread instruction counter unavailable") } var counts: (UInt64, UInt64) = (0, 0) let status = withUnsafeMutablePointer(to: &counts) { overlayThreadCounts(1, UnsafeMutableRawPointer($0), 16) } guard status == 0, counts.0 > 0 else { throw BenchmarkFailure.measurement("thread instruction counter failed: \(status)") } return counts.0 } } extension PerformanceTestSuite { @Suite("Overlay rendering benchmarks", .tags(.benchmark, .appKit)) @MainActor struct OverlayRenderingBenchmarks { @Test func renderingBenchmarks() throws { _ = NSApplication.shared let display = OverlayDisplay( id: "benchmark", frame: CGRect(x: 0, y: 0, width: 2000, height: 1200), scale: 2) let windows = (0..<10).map { index in OverlayWindow(id: UInt32(index + 1), frame: CGRect(x: 20 + index * 190, y: 40, width: 170, height: 500)) } let canvas = OverlayHighlightCanvas(parent: CALayer()) let tiles = windows.map { window in let tile = OverlayTileLayer(parent: canvas.layer) tile.move( to: display.localFrame(window.frame), radius: 10, motion: OverlayMotion(configuration: .init(enabled: false), accessibility: .init()), token: .pop) return tile } let configuration = MotionConfiguration(highlight: .glow) let style = OverlayHighlightStyle(configuration: configuration, accessibility: .init(), active: true) let paletteSource = OverlayPaletteSource() let appearance = NSApp.effectiveAppearance func update(_ frames: [OverlayWindow]) -> Int { let palette = paletteSource.resolve(for: appearance, accessibility: .init()) withoutImplicitAnimations { canvas.update( display: display, windows: [], focusedWindow: nil, cutouts: frames, configuration: configuration, accessibility: .init(), palette: palette, paletteVersion: paletteSource.version) for (tile, window) in zip(tiles, frames) { if tile.layer.frame != display.localFrame(window.frame) { tile.place(at: display.localFrame(window.frame), radius: 10) } tile.styleHighlight(style, palette: palette, version: paletteSource.version, scale: 2) } } CATransaction.flush() return tiles.count + canvas.gaps.count } _ = update(windows) let allocations = OverlayLayerAllocation.count try OverlayBenchmark.measure("Overlay/HighlightUpdate/10-unchanged") { update(windows) } #expect(OverlayLayerAllocation.count == allocations) let shifted = windows.map { OverlayWindow(id: $0.id, frame: $0.frame.offsetBy(dx: 1, dy: 1)) } var moved = false try OverlayBenchmark.measure("Overlay/HighlightUpdate/10-moved") { moved.toggle() return update(moved ? shifted : windows) } let light = try #require(NSAppearance(named: .aqua)) let dark = try #require(NSAppearance(named: .darkAqua)) var darkMode = false let themeSource = OverlayPaletteSource { _, _ in .system(activeBorder: .labelColor) } try OverlayBenchmark.measure("Overlay/ThemeChange/10-windows") { darkMode.toggle() themeSource.invalidate() let palette = themeSource.resolve(for: darkMode ? dark : light, accessibility: .init()) for tile in tiles { tile.styleHighlight(style, palette: palette, version: themeSource.version, scale: 2) } return tiles.count } let fixture = PerformanceFixture(count: 50, layout: .columns) let screen = try #require(NSScreen.screens.first) let number = try #require(screen.deviceDescription[NSDeviceDescriptionKey("NSScreenNumber")] as? NSNumber) let uuid = try #require(CGDisplayCreateUUIDFromDisplayID(number.uint32Value)?.takeRetainedValue()) let displayID = DisplayID(rawValue: CFUUIDCreateString(nil, uuid) as String) let key = WorkspaceKey(display: displayID, space: SpaceID(rawValue: 1)) let workspace = Workspace( key: key, layout: .columns, windowCap: Int.max, order: fixture.ids, focus: fixture.ids[0]) var state = State( displays: [Display(id: displayID, frame: fixture.screen)], activeSpaces: [displayID: key.space], workspaces: [key: workspace], windows: fixture.state.windows, focusedWindow: fixture.ids[0], frames: fixture.state.frames) state.focusModes = [key: FocusModes(noPeek: true)] let controller = OverlayController( configuration: .init(enabled: false), accessibility: .init(reduceTransparency: true), notifications: OverlayNotifications(application: NotificationCenter(), workspace: NotificationCenter()), windowReader: { [] }, onFailure: { Issue.record($0) }) defer { controller.stop() } let snapshot = EngineSnapshot(revision: 1, state: state) try OverlayBenchmark.measure("Overlay/Consume/50-windows", iterations: 30) { controller.consume(snapshot) return snapshot.tiles.count } var stacked = PerformanceFixture(count: 3, layout: .columns, gap: 20, windowCap: 1).state for key in stacked.workspaces.keys { stacked.workspaces[key]?.gaps = Gaps(inner: 20, outer: Insets(20)) } stacked = reduce(stacked, .relayout).0 stacked.observedFrames = stacked.frames #expect(StackChips.groups(in: stacked).count == 1) let chips = StackChipController(focus: { _ in }, close: { _ in }) defer { chips.stop() } try OverlayBenchmark.measure("Chips/Update/unchanged") { chips.update(state: stacked, configuration: .init(enabled: false)) return chips.groups.count + 1 } } } }