SPB Git

Repository Public

PerformanceTests.swift 6.9 KB 130 lines swift

1import AppKit2import TerminalCore3import WorkspaceKit4import XCTest5@testable import SPTerm67/// Measurements printed as `BENCH key=value` lines and collected by Scripts/bench.sh.8/// They exercise the real PTY + emulation engine inside the app process; the windows are not9/// on screen, so these numbers exclude drawing (see BENCHMARKS.md for what is and is not measured).10@MainActor11final class PerformanceTests: AppTestCase {12    func testEchoLatency() async {13        let model = makeModel()14        let window = model.openWindow(tabs: [TabModel(single: PaneDescriptor(kind: .command(line: "exec /bin/cat")))])15        let pane = window.focusedPane!16        await start(window, in: model)17        let session = model.session(for: pane)!18        try? await Task.sleep(for: .milliseconds(500))19        var samples: [Double] = []20        for index in 0..<200 {21            let token = "k\(index)z"22            let begin = DispatchTime.now().uptimeNanoseconds23            session.view.typeText(token)24            // Key press → PTY → line discipline echo → PTY → parser → screen buffer.25            while !session.bufferText().contains(token) {26                await Task.yield()27                if DispatchTime.now().uptimeNanoseconds - begin > 2_000_000_000 { break }28            }29            samples.append(Double(DispatchTime.now().uptimeNanoseconds - begin) / 1_000_000)30            if index % 20 == 19 { session.send(text: "\r") }31        }32        samples.sort()33        print(String(format: "BENCH echo_latency_ms_median=%.2f p95=%.2f max=%.2f samples=%d", samples[samples.count / 2],34                     samples[Int(Double(samples.count) * 0.95)], samples.last ?? 0, samples.count))35        XCTAssertLessThan(samples[samples.count / 2], 50)36    }3738    func testOutputThroughputAndBoundedScrollback() async throws {39        let file = FileManager.default.temporaryDirectory.appendingPathComponent("spterm-bench-\(UUID().uuidString).txt")40        var line = ""41        for column in 0..<9 { line += "col\(column)-abcdefgh " }42        var data = Data()43        for index in 0..<400_000 { data.append(contentsOf: "\(index) \(line)\n".utf8) }44        try data.write(to: file)45        defer { try? FileManager.default.removeItem(at: file) }46        let megabytes = Double(data.count) / 1_048_5764748        let model = makeModel()49        model.settings.scrollbackLines = 200_00050        let window = model.openWindow()51        let pane = window.focusedPane!52        await start(window, in: model)53        let session = model.session(for: pane)!54        session.view.setFrameSize(NSSize(width: 1100, height: 700))55        let before = PerformanceTests.residentMegabytes()56        // Completion is observed through a sentinel file and the session's byte counter. Reading57        // the whole 200k-line buffer in a polling loop would itself stall the parser and distort58        // the measurement (an earlier version of this test did exactly that).59        let sentinel = file.path + ".done"60        defer { try? FileManager.default.removeItem(atPath: sentinel) }61        let begin = Date()62        session.send(text: "cat \(file.path); touch \(sentinel); echo BENCH-DONE-$((1+1))\r")63        let catFinished = await wait(120) { FileManager.default.fileExists(atPath: sentinel) }64        let ptySeconds = Date().timeIntervalSince(begin)65        XCTAssertTrue(catFinished, "the stream did not complete")66        // `cat` returned: every byte crossed the PTY. Now wait until the engine has parsed it all.67        var done = false68        for _ in 0..<600 {69            if session.view.visibleRowsText(0..<session.rows).joined(separator: "\n").contains("BENCH-DONE-2") { done = true; break }70            try? await Task.sleep(for: .milliseconds(20))71        }72        let seconds = Date().timeIntervalSince(begin)73        XCTAssertTrue(done, "the engine did not finish parsing")74        print(String(format: "BENCH pty_drain_seconds=%.2f parsed_seconds=%.2f", ptySeconds, seconds))75        let lines = session.bufferText().split(separator: "\n", omittingEmptySubsequences: false).count76        let after = PerformanceTests.residentMegabytes()77        print(String(format: "BENCH stdout_mb=%.1f seconds=%.2f mb_per_s=%.1f lines_fed=400000 lines_kept=%d rss_before_mb=%.0f rss_after_mb=%.0f",78                     megabytes, seconds, megabytes / seconds, lines, before, after))79        // Scrollback is bounded by the setting, not by what was printed.80        XCTAssertLessThanOrEqual(lines, 200_000 + session.rows + 2)81        XCTAssertGreaterThan(lines, 150_000)82        // The session is still responsive afterwards.83        session.send(text: "echo alive-$((2+2))\r")84        let alive = await wait { session.view.visibleRowsText(0..<session.rows).joined(separator: "\n").contains("alive-4") }85        XCTAssertTrue(alive)8687        // Search across a 200k-line scrollback.88        window.searchQuery = "399999 col0"89        let searchBegin = Date()90        model.search(in: window, forward: false)91        print(String(format: "BENCH search_200k_lines_ms=%.0f result=%@", Date().timeIntervalSince(searchBegin) * 1000, window.searchSummary))92        XCTAssertNotEqual(window.searchSummary, "No results")93    }9495    func testRepeatedResizeUnderOutput() async {96        let model = makeModel()97        let window = model.openWindow()98        let pane = window.focusedPane!99        await start(window, in: model)100        let session = model.session(for: pane)!101        session.send(text: "while :; do echo resize-stress-line-$RANDOM; done\r")102        let begin = Date()103        var sizes = 0104        for step in 0..<300 {105            session.view.setFrameSize(NSSize(width: 300 + (step * 37) % 1500, height: 120 + (step * 53) % 900))106            sizes += 1107            if step % 10 == 0 { await Task.yield() }108        }109        let seconds = Date().timeIntervalSince(begin)110        session.send(bytes: [0x03])111        session.view.setFrameSize(NSSize(width: 1000, height: 600))112        let expected = "\(session.rows) \(session.columns)"113        try? await Task.sleep(for: .milliseconds(300))114        session.send(text: "stty size\r")115        let consistent = await wait { session.bufferText().suffix(300).contains(expected) }116        print(String(format: "BENCH resizes=%d seconds=%.2f final=%@ tty_matches=%@", sizes, seconds, expected, consistent ? "yes" : "no"))117        XCTAssertTrue(consistent, "after 300 resizes the kernel TTY size must match the view (\(expected))")118        XCTAssertTrue(session.status.isRunning)119    }120121    static func residentMegabytes() -> Double {122        var info = mach_task_basic_info()123        var count = mach_msg_type_number_t(MemoryLayout<mach_task_basic_info>.size / MemoryLayout<natural_t>.size)124        let result = withUnsafeMutablePointer(to: &info) {125            $0.withMemoryRebound(to: integer_t.self, capacity: Int(count)) { task_info(mach_task_self_, task_flavor_t(MACH_TASK_BASIC_INFO), $0, &count) }126        }127        return result == KERN_SUCCESS ? Double(info.resident_size) / 1_048_576 : 0128    }129}130
Files main
Menu

Theme