Cotton code
58 lines · 2.5 KB · SwiftRaw
// The page walk (lane threadload, 16 Oct 2026): launched with// `-page-walk /messages/<id>,/people/person/<id>,…` the app opens each route// in turn, back to Home between, `-page-walk-rounds` times (default 5), so// `PageClock` logs each open from the push — the timing a tap gives, with no// system "Open in…?" between, which a deep link from outside raises.// `scripts/perf/pages.py run` launches it on a simulator over a synthetic// household and reads the clock's lines.import CottonKitimport Foundationimport OSLog@MainActorenum PageWalk {    private static var arguments: [String] { ProcessInfo.processInfo.arguments }    private static let log = Logger(subsystem: "com.cottondb.page", category: "open")    /// The routes asked for, or none.    static var routes: [Route] {        guard let at = arguments.firstIndex(of: "-page-walk"), at + 1 < arguments.count else { return [] }        return arguments[at + 1].split(separator: ",").compactMap { Route.parse(String($0)) }    }    static var rounds: Int {        guard let at = arguments.firstIndex(of: "-page-walk-rounds"), at + 1 < arguments.count else { return 5 }        return Int(arguments[at + 1]) ?? 5    }    /// Opens each route, `rounds` times, from Home; logs `walk: done` at the end.    static func run(_ navigator: Navigator, settle: Duration = .seconds(3)) async {        let routes = routes        guard !routes.isEmpty else { return }        try? await Task.sleep(for: .seconds(8))  // the first Home, arranged        for _ in 0..<rounds {            for route in routes {                let pushed = ContinuousClock.now                navigator.push(route)                try? await Task.sleep(for: settle)                calls(since: pushed)                navigator.popToRoot()                try? await Task.sleep(for: .milliseconds(1500))            }        }        log.notice("walk: done")    }    /// Every core call the open made, from the push: when it started and how    /// long it took (a development build's `DevReads`).    private static func calls(since pushed: ContinuousClock.Instant) {        #if COTTON_DEV        for call in DevReads.since(pushed) {            let offset = call.started - pushed            let at = Double(offset.components.seconds) * 1000 + Double(offset.components.attoseconds) / 1e15            log.notice("call: \(call.method, privacy: .public) at \(Int(at)) ms took \(Int(call.millis.rounded())) ms")        }        #endif    }}