Skip to content
This repository was archived by the owner on Oct 17, 2026. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions Tests/CommandKitTests/PerformanceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@ import Testing
/// measures, so they catch an accidental quadratic loop without flaking on a busy CI runner.
@Suite(.serialized)
struct PerformanceTests {
/// The mean of the fastest of five batches. Other work on the machine can only slow a batch down, never
/// speed it up, so the fastest batch is the closest to the code's own cost. An average over everything
/// let one busy moment fail the gate on a loaded Mac.
func averageMicroseconds(_ iterations: Int, _ body: () -> Void) -> Double {
body()
let start = DispatchTime.now().uptimeNanoseconds
for _ in 0..<iterations { body() }
return Double(DispatchTime.now().uptimeNanoseconds - start) / Double(iterations) / 1000
var fastest = UInt64.max
for _ in 0..<5 {
let start = DispatchTime.now().uptimeNanoseconds
for _ in 0..<iterations { body() }
fastest = min(fastest, DispatchTime.now().uptimeNanoseconds - start)
}
return Double(fastest) / Double(iterations) / 1000
}

/// Two thousand plausible names: far more apps and panes than any Mac has.
Expand All @@ -32,7 +39,7 @@ struct PerformanceTests {
func rankingTheWholeCatalogTakesWellUnderAFrame(query: String) {
let catalog = Self.catalog
let now = Date()
let us = averageMicroseconds(20) {
let us = averageMicroseconds(4) {
_ = Ranker.rank(catalog, query: query, usage: Usage(), now: now, limit: 8)
}
print("rank 2,000 for \"\(query)\": \(String(format: "%.0f", us)) µs")
Expand Down
Loading