Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286import Foundationimport ParcelCoreimport Testing
struct EngineCoreWorkload: Sendable, CustomStringConvertible { enum Kind: Sendable { case focus, coldFocus, coldRatio, floatingEdit, tiledEdit, edgeEdit, mixed, arrange, edges, undo, neighbor } let name: String let kind: Kind let count: Int var workspaces = 1 var layout: Layout = .grid var description: String { name }
static var all: [Self] { var workloads: [Self] = [] for count in [10, 50, 200] { for workspaces in [1, 8] { workloads.append( Self( name: "reduce.focusObservation.\(count)x\(workspaces)ws", kind: .focus, count: count, workspaces: workspaces)) workloads.append( Self( name: "reduce.focusObservation.cold.\(count)x\(workspaces)ws", kind: .coldFocus, count: count, workspaces: workspaces)) } workloads.append( Self(name: "reduce.windowFrameEdited.tick.floating.\(count)", kind: .floatingEdit, count: count)) workloads.append(Self(name: "reduce.windowFrameEdited.tick.tiled.\(count)", kind: .tiledEdit, count: count)) workloads.append( Self(name: "reduce.tileEdgeResized.tick.\(count)", kind: .edgeEdit, count: count, layout: .side)) workloads.append(Self(name: "reduce.focus.directional.\(count)", kind: .neighbor, count: count)) } for count in [50, 200] { workloads.append(Self(name: "reduce.batch.mixed8.\(count)", kind: .mixed, count: count)) workloads.append(Self(name: "layout.edges.columns.\(count)", kind: .edges, count: count, layout: .columns)) } for layout in [Layout.columns, .rows, .accordion] { workloads.append( Self(name: "layout.arrange.\(layout.rawValue).200", kind: .arrange, count: 200, layout: layout)) } workloads.append(Self(name: "undo.position.columns.200", kind: .undo, count: 200, layout: .columns)) workloads.append(Self(name: "reduce.nudgeRatio.cold.200", kind: .coldRatio, count: 200, layout: .side)) return workloads }
static var selected: [Self] { let environment = ProcessInfo.processInfo.environment if environment["PARCEL_ENGINE_BENCH"] == "1" { if let name = environment["PARCEL_ENGINE_BENCH_CASE"] { return all.filter { $0.name == name } } return all } return all.filter { $0.count == 10 && $0.workspaces == 1 && $0.kind != .coldFocus } }
func prepare() throws -> () -> Int { let fixture = EngineCoreFixture(count: count, workspaces: workspaces, layout: layout) var seed = kind == .coldFocus || kind == .coldRatio ? fixture.cold : fixture.warm let window = fixture.ids[0] let other = fixture.activeIDs.last ?? window guard let frame = seed.frames[window], let key = seed.activeWorkspace, let workspace = seed.workspaces[key], let screen = seed.displays[key.display]?.frame else { throw EngineCoreMeasurementError.invalid("fixture \(name)") } var events: [Event] switch kind { case .focus, .coldFocus: events = [.focusChanged(other)] case .coldRatio: events = [.commandIssued(.nudgeRatio(0.02))] case .neighbor: events = [.commandIssued(.focus(.right))] case .floatingEdit, .mixed: seed = reduce(seed, .commandIssued(.toggleFloat)).0 seed.undoHistory = nil seed = reduce(seed, .undoGestureBegan).0 seed = reduce(seed, .windowFrameEdited(window, frame: frame.offsetBy(dx: 1, dy: 1))).0 let edited = frame.offsetBy(dx: 2, dy: 2) if kind == .floatingEdit { events = [.windowFrameEdited(window, frame: edited)] } else { guard let otherFrame = seed.frames[other] else { throw EngineCoreMeasurementError.invalid("mixed fixture frame") } events = [ .focusChanged(window), .windowMovedByUser(window, frame: edited), .windowFrameEdited(window, frame: edited), .focusChanged(other), .windowResizedByUser(other, frame: otherFrame.offsetBy(dx: 1, dy: 0)), .commandIssued(.focus(.right)), .focusChanged(window), .windowFrameEdited(window, frame: frame.offsetBy(dx: 3, dy: 3)), ] } case .tiledEdit: seed = reduce(seed, .undoGestureBegan).0 seed = reduce(seed, .windowFrameEdited(window, frame: frame.offsetBy(dx: 1, dy: 1))).0 events = [.windowFrameEdited(window, frame: frame.offsetBy(dx: 2, dy: 2))] case .edgeEdit: guard let edge = seed.tileEdges.first else { throw EngineCoreMeasurementError.invalid("edge fixture \(name)") } seed = reduce(seed, .undoGestureBegan).0 seed = reduce(seed, .tileEdgeResized(edge, delta: 1)).0 events = [.tileEdgeResized(edge, delta: 2)] case .undo: guard let edge = seed.tileEdges.last, let target = edge.secondWindows.last else { throw EngineCoreMeasurementError.invalid("undo fixture edge") } seed = reduce(seed, .focusChanged(target)).0 events = [.tileEdgeResized(edge, delta: 1), .commandIssued(.undo)] case .arrange: return { let result = LayoutEngine.arrange(workspace, in: screen, windows: fixture.warm.windows) return consume(result.frames) &+ result.visibleWindows.count &+ result.cannotFit.count } case .edges: return { let edges = LayoutEngine.edges(workspace, in: screen, windows: fixture.warm.windows) return edges.count &+ (edges.last?.path.count ?? 0) } } let (checked, effects) = reduce(seed, events) for effect in effects.flatMap({ $0 }) { if case .report(let report) = effect { throw EngineCoreMeasurementError.invalid("\(name) rejected workload: \(report)") } } if kind == .floatingEdit { guard checked.frames[window] != seed.frames[window], checked.undoHistory?.count(for: window) == 1 else { throw EngineCoreMeasurementError.invalid("floating tick must change geometry and coalesce undo") } } if kind == .edgeEdit { guard checked.frames != seed.frames, checked.undoHistory?.count(for: window) == 1 else { throw EngineCoreMeasurementError.invalid("edge tick must change geometry and coalesce undo") } } if kind == .coldRatio { guard checked.workspaces[key]?.ratio != seed.workspaces[key]?.ratio, checked.undoHistory?.count(for: window) == 1, reduce(checked, .commandIssued(.undo)).0.frames == fixture.warm.frames else { throw EngineCoreMeasurementError.invalid("cold ratio edit must restore canonical geometry") } } if kind == .undo { guard checked.workspaces == seed.workspaces, checked.frames == seed.frames else { throw EngineCoreMeasurementError.invalid("public resize+undo must restore columns") } } let state = seed return { let (next, effects) = reduce(state, events) return consume(next.frames) &+ effects.reduce(0) { $0 &+ $1.count } &+ next.observedFrames.count &+ (next.undoHistory?.count(for: window) ?? 0) } }}
struct EngineCoreFixture { let ids: [WindowID] let activeIDs: [WindowID] let cold: State let warm: State
init(count: Int, workspaces: Int, layout: Layout) { precondition(count >= workspaces && [1, 8].contains(workspaces)) ids = (1...count).map { WindowID(rawValue: UInt32($0)) } let displays = (0..<2).map { index in Display( id: DisplayID(rawValue: "engine-benchmark-\(index)"), frame: Rect(x: Double(index) * 3840, y: 0, width: 3840, height: 2160)) } 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 model: [WorkspaceKey: Workspace] = [:] var frames: [WindowID: Rect] = [:] var active: [WindowID] = [] for index in 0..<workspaces { let display = displays[index % 2] let key = WorkspaceKey(display: display.id, space: SpaceID(rawValue: UInt64(index / 2 + 1))) let order = ids.enumerated().filter { $0.offset % workspaces == index }.map(\.element) var workspace = Workspace( key: key, layout: layout, gaps: Gaps(inner: 2), windowCap: Int.max, order: order, focus: order[0]) workspace.adapt(to: display.frame, fullFrame: display.fullFrame) let arranged = LayoutEngine.arrange(workspace, in: display.frame, windows: windows) precondition( arranged.frames.count == order.count && arranged.visibleWindows.count == order.count, "Engine benchmark fixture must place every window in a separate visible tile") model[key] = workspace frames.merge(arranged.frames.mapValues { $0.offsetBy(dx: 1, dy: 1) }) { _, new in new } if index < min(2, workspaces) { active += order } } activeIDs = active var spaces = [displays[0].id: SpaceID(rawValue: 1)] if workspaces > 1 { spaces[displays[1].id] = SpaceID(rawValue: 1) } cold = State( displays: displays, activeSpaces: spaces, workspaces: model, windows: windows, focusedWindow: ids[0], frames: frames) warm = reduce(cold, .relayout).0 }}
extension Rect { fileprivate func offsetBy(dx: Double, dy: Double) -> Rect { Rect(x: minX + dx, y: minY + dy, width: size.width, height: size.height) }}
private struct EngineCoreReport: Encodable { let version = 1 let machine: String let os: String let counter = "proc_pid_rusage/RUSAGE_INFO_V4/ri_instructions/process" let allocationCounter = "libmalloc malloc_logger/thread/requested allocation traffic/separate replay" let result: EngineCoreResult}
extension EngineCorePerformanceTests { @Suite("Engine core isolated performance", .timeLimit(.minutes(10))) struct EngineCoreBenchmarks { @Test(arguments: EngineCoreWorkload.selected) func workload(_ workload: EngineCoreWorkload) throws { let environment = ProcessInfo.processInfo.environment let full = environment["PARCEL_ENGINE_BENCH"] == "1" if full && _isDebugAssertConfiguration() { throw EngineCoreMeasurementError.invalid("release build required") } if full, environment["PARCEL_ENGINE_BENCH_OUTPUT"] != nil, environment["PARCEL_ENGINE_BENCH_CASE"] == nil { throw EngineCoreMeasurementError.invalid("select one case when writing an output artifact") } try EngineCoreAllocationCounter.validate() let operation = try workload.prepare() let result = try EngineCoreSampler.measure( name: workload.name, iterations: full ? 32 : 2, samples: full ? 7 : 1, operation: operation) if full { print( "ENGINE_BENCH \(result.name): instructions \(String(format: "%.0f", result.minimumInstructions)), " + "CPU \(String(format: "%.2f", result.minimumCPU / 1000)) us, " + "allocations \(String(format: "%.2f", result.minimumAllocations)), " + "requested bytes \(String(format: "%.0f", result.minimumBytes))") if let path = environment["PARCEL_ENGINE_BENCH_OUTPUT"] { let report = EngineCoreReport( machine: try BenchmarkMachine.current().identity, os: ProcessInfo.processInfo.operatingSystemVersionString, result: result) let encoder = JSONEncoder() encoder.outputFormatting = [.prettyPrinted, .sortedKeys] try encoder.encode(report).write(to: URL(fileURLWithPath: path), options: .atomic) } } }
@Test func fullSelectionMustNameAnExistingWorkload() throws { let environment = ProcessInfo.processInfo.environment if environment["PARCEL_ENGINE_BENCH"] == "1", let name = environment["PARCEL_ENGINE_BENCH_CASE"] { guard EngineCoreWorkload.all.contains(where: { $0.name == name }) else { throw EngineCoreMeasurementError.invalid("unknown workload \(name)") } } }
@Test func warmFixturesKeepInactiveFramesAndRemoveActiveWrites() { let fixture = EngineCoreFixture(count: 50, workspaces: 8, layout: .grid) #expect(fixture.warm.displays.count == 2) #expect(fixture.warm.workspaces.count == 8) #expect(fixture.warm.windows.count == 50) let (_, effects) = reduce(fixture.warm, .focusChanged(fixture.ids[0])) #expect(!effects.contains { if case .setFrames = $0 { true } else { false } }) for window in fixture.ids { if let key = fixture.warm.workspace(containing: window), !fixture.warm.isActive(key) { #expect(fixture.warm.frames[window] == fixture.cold.frames[window]) } } let (_, coldEffects) = reduce(fixture.cold, .focusChanged(fixture.ids[0])) #expect(coldEffects.contains { if case .setFrames = $0 { true } else { false } }) } }}