Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128import Foundation
struct MeasuredFrame: Codable, Equatable { enum Kind: String, Codable { case write, vsync, mark } let timestampNanoseconds: UInt64 let x: Double let y: Double let w: Double let h: Double let kind: Kind var label: String? var timestamp: Double? var targetTimestamp: Double?
private enum CodingKeys: String, CodingKey { case timestampNanoseconds = "t_ns" case x, y, w, h, kind, label, timestamp, targetTimestamp }
var components: [Double] { [x, y, w, h] } func distance(to other: Self) -> Double { zip(components, other.components).reduce(0) { $0 + pow($1.0 - $1.1, 2) }.squareRoot() }}
struct MeasuredRecording: Codable { let window: Int let records: [MeasuredFrame] let refreshIntervalNanoseconds: UInt64 let overflow: Bool let clockSource: String
private enum CodingKeys: String, CodingKey { case window, records, overflow, clockSource case refreshIntervalNanoseconds = "refreshInterval_ns" }}
struct SmoothnessMetrics: Codable { let refreshIntervalNanoseconds: UInt64 let refreshes: Int let starvedRefreshes: Int let dropRate: Double let maxGapRefreshes: Double let jank: Double let inputToFirstMoveNanoseconds: UInt64 let settle90Nanoseconds: UInt64 let settleFinalNanoseconds: UInt64 let overshootPoints: Double let writesPerRefresh: Double let maximumWritesPerRefresh: Int
static func measure(_ recording: MeasuredRecording, stimulus: UInt64) throws -> Self { guard !recording.overflow else { throw E2EError(description: "Fixture frame ring overflowed") } guard recording.clockSource == "CLOCK_UPTIME_RAW" else { throw E2EError(description: "Unsupported fixture timestamp source: \(recording.clockSource)") } let records = recording.records guard records.allSatisfy({ $0.components.allSatisfy(\.isFinite) }), zip(records, records.dropFirst()).allSatisfy({ $0.timestampNanoseconds <= $1.timestampNanoseconds }) else { throw E2EError(description: "Nonfinite geometry or nonmonotonic recording") } let interval = recording.refreshIntervalNanoseconds guard interval > 0 else { throw E2EError(description: "No display refresh interval was recorded") } let writes = records.filter { $0.kind == .write && $0.timestampNanoseconds >= stimulus } guard let firstWrite = writes.first, let last = writes.last else { throw E2EError(description: "No fixture frame write followed the stimulus") } let start = records.last { $0.kind == .mark && $0.timestampNanoseconds <= stimulus } ?? firstWrite let moving = writes.filter { $0.distance(to: start) > 0.01 } let first = moving.first ?? firstWrite guard first.timestampNanoseconds >= stimulus else { throw E2EError(description: "Stimulus follows first move") } let ticks = records.filter { $0.kind == .vsync } guard ticks.count >= 2 else { throw E2EError(description: "Recording needs at least two display refreshes") } let motionTicks = ticks.filter { $0.timestampNanoseconds >= first.timestampNanoseconds && $0.timestampNanoseconds <= last.timestampNanoseconds } var starved = 0 var maximumWrites = 0 var totalWrites = 0 for tick in motionTicks { let previous = ticks.last { $0.timestampNanoseconds < tick.timestampNanoseconds }?.timestampNanoseconds ?? first.timestampNanoseconds let count = writes.filter { $0.timestampNanoseconds > previous && $0.timestampNanoseconds <= tick.timestampNanoseconds }.count if count == 0 { starved += 1 } maximumWrites = max(maximumWrites, count) totalWrites += count } let gaps = zip(writes, writes.dropFirst()).map { Double($1.timestampNanoseconds - $0.timestampNanoseconds) / Double(interval) } let delta = zip(start.components, last.components).map { $1 - $0 } let squaredDistance = delta.reduce(0) { $0 + $1 * $1 } func progress(_ frame: MeasuredFrame) -> Double { guard squaredDistance > 0 else { return 1 } return zip(zip(frame.components, start.components), delta).reduce(0) { $0 + ($1.0.0 - $1.0.1) * $1.1 } / squaredDistance } let ninety = writes.first { progress($0) >= 0.9 } ?? last let overshoot = writes.map { max(0, progress($0) - 1) * squaredDistance.squareRoot() }.max() ?? 0 return Self( refreshIntervalNanoseconds: interval, refreshes: motionTicks.count, starvedRefreshes: starved, dropRate: motionTicks.isEmpty ? 0 : Double(starved) / Double(motionTicks.count), maxGapRefreshes: gaps.max() ?? 0, jank: gaps.isEmpty ? 0 : Double(gaps.filter { $0 > 1.5 }.count) / Double(gaps.count), inputToFirstMoveNanoseconds: first.timestampNanoseconds - stimulus, settle90Nanoseconds: ninety.timestampNanoseconds - stimulus, settleFinalNanoseconds: last.timestampNanoseconds - stimulus, overshootPoints: overshoot, writesPerRefresh: motionTicks.isEmpty ? 0 : Double(totalWrites) / Double(motionTicks.count), maximumWritesPerRefresh: maximumWrites) }}
func measurementPercentile(_ values: [Double], _ percentile: Double) throws -> Double { guard !values.isEmpty, values.allSatisfy(\.isFinite), (0...1).contains(percentile) else { throw E2EError(description: "Percentile needs finite samples and a probability") } let sorted = values.sorted() let rank = Double(sorted.count - 1) * percentile let lower = Int(rank.rounded(.down)) let upper = Int(rank.rounded(.up)) return sorted[lower] + (sorted[upper] - sorted[lower]) * (rank - Double(lower))}