Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Time the tests' quick checks by their thread's processor time

Țugui Dragoșf47e68ab2495
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift
index 5853d0f9..5f22c3b2 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift
@@ -426,17 +426,16 @@ struct DuplicateFinderTests {
#expect(!DuplicateFinder.hasCopySuffix("Invoice (3a)"))
}
- /// `hasCopySuffix` is asked once for every file in every group, so it has to stay fast on a large scan. The
- /// time limit is generous, so that a busy Mac slows the test without failing it.
+ /// `hasCopySuffix` is asked once for every file in every group, so it has to stay fast on a large scan.
@Test func recognizesCopySuffixesQuickly() {
- let clock = ContinuousClock()
- let start = clock.now
var copies = 0
- for index in 0..<20_000 where DuplicateFinder.hasCopySuffix("small-\(index) copy") {
- copies += 1
+ let took = processorTime {
+ for index in 0..<20_000 where DuplicateFinder.hasCopySuffix("small-\(index) copy") {
+ copies += 1
+ }
}
#expect(copies == 20_000)
- #expect(clock.now - start < .seconds(1))
+ #expect(took < .seconds(1))
}
@Test func scansOnlyUserContentFolders() throws {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/InstalledAppTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/InstalledAppTests.swift
index 725194c6..bcc139db 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/InstalledAppTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/InstalledAppTests.swift
@@ -7,10 +7,7 @@ struct InstalledAppTests {
let app = InstalledApp(url: URL(filePath: "/Applications/Editor.app"), bundleIdentifier: "org.example.editor", name: "Editor")
var answers = 0
- // The time the processor spent on this thread, which other tests running beside it do not stretch.
- let started = clock_gettime_nsec_np(CLOCK_THREAD_CPUTIME_ID)
- for _ in 0..<200_000 where app.isPeelItself { answers += 1 }
- let took = Duration.nanoseconds(Int64(clock_gettime_nsec_np(CLOCK_THREAD_CPUTIME_ID) - started))
+ let took = processorTime { for _ in 0..<200_000 where app.isPeelItself { answers += 1 } }
#expect(answers == 0)
#expect(took < .milliseconds(100))
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/OutputTailTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/OutputTailTests.swift
index 776f616f..7a53fb76 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/OutputTailTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/OutputTailTests.swift
@@ -17,8 +17,7 @@ struct OutputTailTests {
var output = Data()
for _ in 0..<200_000 { output.append(line) }
- let clock = ContinuousClock()
- let took = clock.measure { _ = OutputTail.lastLines(of: output, count: 400) }
+ let took = processorTime { _ = OutputTail.lastLines(of: output, count: 400) }
#expect(took < .milliseconds(5), "\(took)")
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/Timing.swift b/Packages/PeelCore/Tests/PeelCoreTests/Timing.swift
index ba1e7d1d..7bfee08c 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/Timing.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/Timing.swift
@@ -1,3 +1,12 @@
+import Darwin
+
+/// The processor time `work` takes on this thread, which other tests taking the processor do not stretch.
+func processorTime(of work: () -> Void) -> Duration {
+ let started = clock_gettime_nsec_np(CLOCK_THREAD_CPUTIME_ID)
+ work()
+ return .nanoseconds(Int64(clock_gettime_nsec_np(CLOCK_THREAD_CPUTIME_ID) - started))
+}
+
/// Runs `work` at user-initiated priority and times it where it returns. A task at the default priority resumes only
/// once every test queued before it has had a thread, and that wait would be counted too.
func timedWhereItRuns<Value: Sendable>(
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/UpdateTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/UpdateTests.swift
index e28c1556..71411761 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/UpdateTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/UpdateTests.swift
@@ -91,10 +91,9 @@ struct AppcastTests {
</channel></rss>
""".utf8)
}
- let clock = ContinuousClock()
var reading: Appcast.Reading?
- let took = clock.measure { reading = Appcast.read(feed(using: "&a10;"), systemVersion: "26.7.0", isAppleSilicon: true) }
+ let took = processorTime { reading = Appcast.read(feed(using: "&a10;"), systemVersion: "26.7.0", isAppleSilicon: true) }
#expect(reading == .unreadable)
#expect(took < .seconds(1))