Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272import Foundationimport ParcelAnimationimport ParcelConfigimport ParcelCoreimport ParcelIPCimport ParcelTestSupportimport ParcelUIimport Testing
@testable import ParcelEngine
extension PerformanceTestSuite { @Suite("Headless performance", .serialized, .tags(.benchmark, .appKit), .timeLimit(.minutes(10))) @MainActor struct PerformanceBenchmarks { @Test func headlessPaths() async throws { let environment = ProcessInfo.processInfo.environment let hosted = environment["GITHUB_ACTIONS"] == "true" || environment["PARCEL_BENCH_SMOKE"] == "1" let full = environment["PARCEL_BENCH"] == "1" && !hosted let counters = BenchmarkCounters.startup(full: full, hosted: hosted) if hosted { print("Hosted CI: benchmark smoke mode only (no performance gate).") } if full && _isDebugAssertConfiguration() { throw BenchmarkFailure.debugBuild } let machine = try BenchmarkMachine.current() let host = try machine.host( source: counters.source, quietURL: environment["PARCEL_BENCH_QUIET"].map { URL(fileURLWithPath: $0) }) let baselineURL = environment["PARCEL_BENCH_BASELINE"].map { URL(fileURLWithPath: $0) } ?? BenchmarkBaselines.url(for: machine) let recording = environment["PARCEL_BENCH_RECORD"] == "1" let comparing = full && !recording && environment["PARCEL_BENCH_NO_COMPARE"] != "1" let baseline: BenchmarkReport? if comparing { baseline = try BenchmarkBaselines.read(baselineURL, caseNames: BenchmarkCases.names) guard baseline?.machine == machine.identity else { throw BenchmarkFailure.machine(baseline?.machine ?? "missing", machine.identity) } } else { baseline = nil } let previousPriority = full ? setPriority(.userInteractive) : nil defer { if let previousPriority { _ = setPriority(previousPriority) } } var harness = BenchmarkHarness(full: full, counters: counters) for count in [50, 200] { for layout in Layout.allCases { let fixture = PerformanceFixture(count: count, layout: layout) let iterations = count == 50 ? 40 : 10 let name = "event-layout/\(layout.rawValue)/\(count)" try harness.measure(name, iterations: iterations) { try PerformanceWorkload.eventLayout( state: fixture.state, event: .focusChanged(fixture.ids[count - 1]), name: name, counters: counters) } harness.measure("layout-diff/\(layout.rawValue)/\(count)", iterations: iterations) { let (next, effects) = reduce(fixture.state, .relayout) return BenchmarkObservation(checksum: consume(next.frames) &+ effects.count) } } let fixture = PerformanceFixture(count: count, layout: .grid) let burst = (0..<16).map { Event.focusChanged(fixture.ids[$0 % count]) } harness.measure("reducer-burst/\(count)", iterations: 20) { let (next, effects) = reduce(fixture.state, burst) return BenchmarkObservation(checksum: consume(next.frames) &+ effects.count) } harness.measure("snapshot-menu/\(count)", iterations: 100) { snapshotModel(fixture) } let requests = fixture.ids.enumerated().map { index, id in MotionRequest( windowID: MotionWindowID(rawValue: UInt64(id.rawValue)), appID: MotionAppID(rawValue: Int32(index % 8 + 1)), start: MotionRect(originX: 0, originY: 0, width: 100, height: 100), target: MotionRect(originX: Double(index) * 5, originY: 200, width: 120, height: 100), screen: MotionRect(originX: 0, originY: 0, width: 3840, height: 2160)) } try harness.measure("animation-plan-sample/\(count)", iterations: 30) { var checksum = 0 for request in requests { let plan = try FramePlanner.plan(request, at: 10) let frame = try plan.sample(at: 10 + 1.0 / 120).frame checksum &+= Int(frame.rect.originX) &+ Int(frame.rect.width) } return BenchmarkObservation(checksum: checksum) } } for layout in Layout.allCases { let fixture = PerformanceFixture( count: 200, layout: layout, screen: Rect(x: 0, y: 0, width: 320, height: 200), gap: 1_000) let workspace = try #require(fixture.state.workspaces.values.first) harness.measure("compressed-gap/\(layout.rawValue)/200", iterations: 10) { let result = LayoutEngine.arrange( workspace, in: fixture.screen, windows: fixture.state.windows) return BenchmarkObservation(checksum: consume(result.frames) &+ result.cannotFit.count) } } harness.measure("snapshot-menu/6", iterations: 200) { snapshotModel(PerformanceFixture.sixWindows) } let source = (0..<500).map { index in """ [[rules]] bundle-id = "org.example.app\(index)" title-contains = "Document \(index)" action = "float" """ }.joined(separator: "\n") try harness.measure("config-parse-validate/500-rules", iterations: 3) { let result = ConfigValidator.validate(try TOMLDocument(parsing: source)) guard let configuration = result.configuration, result.diagnostics.isEmpty else { throw BenchmarkFailure.measurement("large config") } return BenchmarkObservation(checksum: configuration.rules.count) } try await socketRoundTrip(&harness) #if !PARCEL_BENCH_CORE_ONLY try await HotPathBenchmarks.run(&harness) #endif #expect(harness.results.count == BenchmarkCases.names.count) #expect(Set(harness.results.map(\.name)) == BenchmarkCases.names) let report = BenchmarkReport( version: BenchmarkReport.currentVersion, machine: machine.identity, host: host, calibrationNanoseconds: BenchmarkCalibration.measure(), results: harness.results) try report.validate(caseNames: BenchmarkCases.names) if let output = environment["PARCEL_BENCH_OUTPUT"] { try BenchmarkBaselines.write(report, to: URL(fileURLWithPath: output), caseNames: BenchmarkCases.names) } #if !PARCEL_BENCH_CORE_ONLY try HotPathBenchmarks.writeCatalog() #endif guard full else { return } for result in report.results { print( "BENCH \(result.name): \(result.metric.rawValue) minimum \(String(format: "%.2f", result.minimumNanoseconds / 1000)) µs, " + "median \(String(format: "%.2f", result.medianNanoseconds / 1000)) µs, " + "wall median \(String(format: "%.2f", result.wallMedianNanoseconds / 1000)) µs, " + "instructions \(result.instructionsPerOp.map { String(format: "%.0f", $0) } ?? "not measured") " + "[\(result.reportingOnly ? "reporting-only" : "gating")] wall p99 " + "\(result.wallP99Nanoseconds.map { String(format: "%.2f", $0 / 1000) } ?? "not sampled") µs") } if recording { guard host.quiet?.verified == true else { throw BenchmarkFailure.measurement("baseline recording requires verified quiet-check readings") } try BenchmarkBaselines.write(report, to: baselineURL, caseNames: BenchmarkCases.names) print("Recorded baseline: \(baselineURL.path)") return } guard comparing else { return } guard let baseline else { throw BenchmarkFailure.measurement("baseline not loaded") } let regressions = try report.regressions( against: baseline, scaleCalibration: environment["PARCEL_BENCH_CALIBRATE"] == "1") if !regressions.isEmpty { throw BenchmarkFailure.regression(regressions) } }
private func snapshotModel(_ fixture: PerformanceFixture) -> BenchmarkObservation { let snapshot = EngineSnapshot(revision: 1, state: fixture.state) let tiles = snapshot.tiles.map { tile in LayoutTile( id: UInt64(tile.window.rawValue), frame: CGRect( x: tile.frame.minX, y: tile.frame.minY, width: tile.frame.size.width, height: tile.frame.size.height ), isFocused: tile.window == snapshot.focusedWindow) } let model = LayoutSnapshot( layout: .grid, bounds: CGRect(x: 0, y: 0, width: 3840, height: 2160), tiles: tiles) let icon = MenuIcon(snapshot: model, style: .minimap) let count: Int switch icon { case .tiles(let miniTiles): count = miniTiles.count case .logo(let badge): count = badge ?? 0 } return BenchmarkObservation(checksum: count &+ snapshot.layouts.count &+ tiles.count) }
private func socketRoundTrip(_ harness: inout BenchmarkHarness) async throws { let directory = URL(fileURLWithPath: "/tmp/parcel-bench-" + UUID().uuidString.prefix(8)) try FileManager.default.createDirectory( at: directory, withIntermediateDirectories: false, attributes: [.posixPermissions: 0o700]) defer { do { try FileManager.default.removeItem(at: directory) } catch { Issue.record(error) } } let path = directory.appendingPathComponent("ipc.sock").path let server = try IPCServer(handler: BenchmarkIPCHandler(), socketPath: path, onError: { Issue.record($0) }) let client = try IPCClient(socketPath: path) try await server.start() do { try await harness.measureAsync("ipc-request-round-trip", iterations: 20) { let response = try await client.request(IPCRequest(id: .integer(1), command: "ping")) guard response.result == .string("pong"), response.ok else { throw BenchmarkFailure.measurement("IPC ping") } return BenchmarkObservation(checksum: 1) } await client.close() try await server.stop() } catch { await client.close() do { try await server.stop() } catch { Issue.record(error) } throw error } }
private func setPriority(_ priority: QualityOfService) -> QualityOfService { let thread = Thread.current let previous = thread.qualityOfService thread.qualityOfService = priority return previous }
}}
struct PerformanceFixture { static let sixWindows = PerformanceFixture(count: 6, layout: .grid) let ids: [WindowID] let state: State let screen: Rect
init( count: Int, layout: Layout, screen: Rect = Rect(x: 0, y: 0, width: 3840, height: 2160), gap: Double = 2, fullFrame: Rect? = nil, windowCap: Int = Int.max ) { self.screen = screen let display = Display(id: DisplayID(rawValue: "benchmark"), frame: screen, fullFrame: fullFrame) let space = SpaceID(rawValue: 1) let key = WorkspaceKey(display: display.id, space: space) ids = (1...count).map { WindowID(rawValue: UInt32($0)) } let windows = Dictionary( uniqueKeysWithValues: ids.map { id in ( id, ManagedWindow( id: id, app: AppID(pid: Int32(id.rawValue % 8 + 1), bundleID: "org.example.fixture"), title: "Document \(id.rawValue)", minimumSize: Size(width: 4, height: 4)) ) }) var workspace = Workspace( key: key, layout: layout, gaps: Gaps(inner: gap), windowCap: windowCap, order: ids, focus: ids[0]) workspace.adapt(to: display.frame, fullFrame: display.fullFrame) #expect(workspace.displaySize == display.frame.size) #expect(workspace.capabilitySize == display.fullFrame.size) let layoutResult = LayoutEngine.arrange(workspace, in: display.frame, windows: windows) #expect(layoutResult.frames.count == count) #expect(layoutResult.visibleWindows.count == min(count, workspace.effectiveWindowCap)) let frames = layoutResult.frames state = State( displays: [display], activeSpaces: [display.id: space], workspaces: [key: workspace], windows: windows, focusedWindow: ids[0], frames: frames.mapValues { Rect(x: $0.minX + 1, y: $0.minY + 1, width: $0.size.width, height: $0.size.height) }) }}
@inline(never)func consume(_ frames: [WindowID: Rect]) -> Int { frames.reduce(frames.count) { checksum, element in let frame = element.value return checksum &+ Int(element.key.rawValue) &* 31 &+ Int(frame.minX * 1000) &* 37 &+ Int(frame.minY * 1000) &* 41 &+ Int(frame.size.width * 1000) &* 43 &+ Int(frame.size.height * 1000) &* 47 }}
private actor BenchmarkIPCHandler: IPCHandler { func query(_ query: IPCQuery) async throws -> JSONValue { .array([]) } func run(command: String, args: [String]) async throws -> JSONValue { .bool(true) } func reloadConfig() async throws -> JSONValue { .bool(true) }}