Tests/OrgCoreTests/BacklinkGateTests.swift
34 lines · 1750 bytes
2 symbols in this file
1import Foundation
2import Testing
3@testable import OrgCore
4
5/// Timing checks for backlinks, run with the other gates (`ORGSTAR_GATES=1 swift test -c release
6/// --filter GateTests`) rather than in the parallel suite, where other tests' load decides them.
7@Suite(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_GATES"] != nil), .serialized)
8struct BacklinkGateTests {
9 let heading = Backlinks.Target(path: "/notes/a.org", title: "Fix the sink [1/2]", id: "abc-1", customID: "sink")
10
11 /// The patterns are compiled once, not for each link: testing the links takes less time
12 /// than compiling them that often would.
13 @Test func testsManyLinksWithoutCompilingPatterns() throws {
14 let links = (0..<4_000).map { ["id:abc-\($0)", "file:a.org::*Fix the sink", "https://example.org/\($0)", "Fix the sink"][$0 % 4] }
15 let clock = ContinuousClock()
16 // The fastest of several interleaved rounds, which other work on the machine can't slow.
17 var compiling = Duration.seconds(Int64.max)
18 var testing = Duration.seconds(Int64.max)
19 for _ in 0..<5 {
20 compiling = min(compiling, try clock.measure {
21 for index in links.indices {
22 _ = try NSRegularExpression(pattern: "(\\\\+)(?=$|[\\[\\]])")
23 if index % 2 == 0 { _ = try Regex("^[a-zA-Z][-a-zA-Z0-9+]*:") }
24 }
25 })
26 var hits = 0
27 testing = min(testing, clock.measure {
28 for link in links where Backlinks.links(link, from: "/notes/b.org", to: heading, home: "/home") { hits += 1 }
29 })
30 #expect(hits == 1_000)
31 }
32 #expect(testing < compiling / 2, "\(testing) against \(compiling)")
33 }
34}