Commit 415778652e
Verified · cmc
Layout: unified · split
Tests/OrgCoreTests/BacklinkTests.swift +16 −11
| @@ -27,19 +27,24 @@ struct BacklinkTests { | |||
| 27 | /// The patterns are compiled once, not for each link: testing the links takes less time | 27 | /// The patterns are compiled once, not for each link: testing the links takes less time |
| 28 | /// than compiling them that often would. | 28 | /// than compiling them that often would. |
| 29 | @Test func testsManyLinksWithoutCompilingPatterns() throws { | 29 | @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] } |
| 31 | let clock = ContinuousClock() | 31 | let clock = ContinuousClock() |
| 32 | let compiling = try clock.measure { | 32 | // The fastest of several interleaved rounds, which other work on the machine can't slow. |
| 33 | for index in links.indices { | 33 | var compiling = Duration.seconds(Int64.max) |
| 34 | _ = try NSRegularExpression(pattern: "(\\\\+)(?=$|[\\[\\]])") | 34 | var testing = Duration.seconds(Int64.max) |
| 35 | if index % 2 == 0 { _ = try Regex("^[a-zA-Z][-a-zA-Z0-9+]*:") } | 35 | for _ in 0..<5 { |
| 36 | } | 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) | ||
| 37 | } | 47 | } |
| 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) | ||
| 43 | #expect(testing < compiling / 2, "\(testing) against \(compiling)") | 48 | #expect(testing < compiling / 2, "\(testing) against \(compiling)") |
| 44 | } | 49 | } |
| 45 | 50 | ||