Make the backlinks timing test independent of machine load !464

merged merged by cmc on 2026-11-22 19:30 UTC · krz/orgstar:backlinks-timing-test into main

1 file changed, +16 −11

Layout: unified · split

Tests/OrgCoreTests/BacklinkTests.swift +16 −11
@@ -27,19 +27,24 @@ struct BacklinkTests {
2727 /// The patterns are compiled once, not for each link: testing the links takes less time
2828 /// than compiling them that often would.
2929 @Test func testsManyLinksWithoutCompilingPatterns() throws {
30 let links = (0..<20_000).map { ["id:abc-\($0)", "file:a.org::*Fix the sink", "https://example.org/\($0)", "Fix the sink"][$0 % 4] }
30 let links = (0..<4_000).map { ["id:abc-\($0)", "file:a.org::*Fix the sink", "https://example.org/\($0)", "Fix the sink"][$0 % 4] }
3131 let clock = ContinuousClock()
32 let compiling = try clock.measure {
33 for index in links.indices {
34 _ = try NSRegularExpression(pattern: "(\\\\+)(?=$|[\\[\\]])")
35 if index % 2 == 0 { _ = try Regex("^[a-zA-Z][-a-zA-Z0-9+]*:") }
36 }
32 // The fastest of several interleaved rounds, which other work on the machine can't slow.
33 var compiling = Duration.seconds(Int64.max)
34 var testing = Duration.seconds(Int64.max)
35 for _ in 0..<5 {
36 compiling = min(compiling, try clock.measure {
37 for index in links.indices {
38 _ = try NSRegularExpression(pattern: "(\\\\+)(?=$|[\\[\\]])")
39 if index % 2 == 0 { _ = try Regex("^[a-zA-Z][-a-zA-Z0-9+]*:") }
40 }
41 })
42 var hits = 0
43 testing = min(testing, clock.measure {
44 for link in links where Backlinks.links(link, from: "/notes/b.org", to: heading, home: "/home") { hits += 1 }
45 })
46 #expect(hits == 1_000)
3747 }
38 var hits = 0
39 let testing = clock.measure {
40 for link in links where Backlinks.links(link, from: "/notes/b.org", to: heading, home: "/home") { hits += 1 }
41 }
42 #expect(hits == 5_000)
4348 #expect(testing < compiling / 2, "\(testing) against \(compiling)")
4449 }
4550