oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1#if os(iOS)
2import Foundation
3import UIKit
4import XCTest
5@testable import DocsKit
6
7/// §7's iPhone row: main-thread time per one-block publication, before S1a
8/// (the whole `attributedText` assigned, as `applyRender` did) and after
9/// (`DocumentSurface`: the one block rendered and reconciled), each with
10/// the layout that follows. A probe, not a gate: skipped unless
11/// DOCS_RT_AUDIT_DOCS names a directory of generate-large-doc.mjs
12/// documents (TEST_RUNNER_DOCS_RT_AUDIT_DOCS through xcodebuild).
13@MainActor final class SurfaceTimingTests: XCTestCase {
14 func testOneBlockPublication() async throws {
15 guard let dir = ProcessInfo.processInfo.environment["DOCS_RT_AUDIT_DOCS"] else { throw XCTSkip("Set DOCS_RT_AUDIT_DOCS") }
16 let names = (ProcessInfo.processInfo.environment["DOCS_RT_AUDIT_ONLY"] ?? "p20,p200h").split(separator: ",").map(String.init)
17 DocsFonts.register()
18 for name in names {
19 let replica = CRDTReplica(guid: "timing-" + name)
20 try await replica.open(update: try Data(contentsOf: URL(fileURLWithPath: "\(dir)/\(name).bin")))
21 var projection = try await replica.projection()
22 await replica.close()
23 let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 402, height: 874))
24 window.makeKeyAndVisible()
25 let block = projection.children.count / 2
26 for paged in [true, false] {
27 // Before: the whole text assigned per publication.
28 let before = DocsTextView(frame: window.bounds)
29 before.paged = paged
30 window.addSubview(before)
31 before.attributedText = NativeTextProjection.render(projection)
32 before.layoutIfNeeded()
33 var whole: [Double] = []
34 var current = projection
35 for _ in 0..<8 {
36 current.children[block] = Self.appending("x", to: current.children[block])
37 let start = ContinuousClock.now
38 before.attributedText = NativeTextProjection.render(current)
39 before.layoutIfNeeded()
40 whole.append(Self.ms(start))
41 }
42 before.removeFromSuperview()
43 // After: the surface.
44 let after = DocsTextView(frame: window.bounds)
45 after.paged = paged
46 window.addSubview(after)
47 let surface = DocumentSurface()
48 surface.attach(after)
49 current = projection
50 var revision: UInt64 = 1
51 surface.apply(DocumentSurface.Publication(generation: 1, projection: current, revision: revision, dirty: nil))
52 after.layoutIfNeeded()
53 var perBlock: [Double] = []
54 for _ in 0..<8 {
55 current.children[block] = Self.appending("x", to: current.children[block])
56 revision += 1
57 let start = ContinuousClock.now
58 surface.apply(DocumentSurface.Publication(generation: 1, projection: current, revision: revision, dirty: [current.children[block].id]))
59 after.layoutIfNeeded()
60 perBlock.append(Self.ms(start))
61 }
62 after.removeFromSuperview()
63 print(String(format: "SURFACE TIMING %@ paged=%d: attributedText+layout median %.2f ms; surface+layout median %.2f ms (n=8; %@ / %@)", name, paged ? 1 : 0,
64 Self.median(whole), Self.median(perBlock), whole.map { String(format: "%.1f", $0) }.joined(separator: " "), perBlock.map { String(format: "%.1f", $0) }.joined(separator: " ")))
65 }
66 projection = CRDTProjection(children: [], canUndo: false, canRedo: false)
67 window.isHidden = true
68 }
69 }
70 /// The keystroke on the iPhone (S1b step 0): the keyboard's path into
71 /// the text view (the delegate asked, the storage edited, the caret
72 /// moved), then the layout UIKit runs before it draws, per key; and the
73 /// line-fragment requests our container gets for it.
74 func testKeystroke() async throws {
75 guard let dir = ProcessInfo.processInfo.environment["DOCS_RT_AUDIT_DOCS"] else { throw XCTSkip("Set DOCS_RT_AUDIT_DOCS") }
76 let names = (ProcessInfo.processInfo.environment["DOCS_RT_AUDIT_ONLY"] ?? "p1,p5,p20,p50,p200h").split(separator: ",").map(String.init)
77 DocsFonts.register()
78 for name in names {
79 let replica = CRDTReplica(guid: "keys-" + name)
80 try await replica.open(update: try Data(contentsOf: URL(fileURLWithPath: "\(dir)/\(name).bin")))
81 let projection = try await replica.projection()
82 await replica.close()
83 for paged in [true, false] {
84 let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 402, height: 874))
85 window.makeKeyAndVisible()
86 let text = DocsTextView(frame: window.bounds)
87 text.paged = paged
88 window.addSubview(text)
89 let surface = DocumentSurface()
90 var hooks = DocumentSurface.Hooks()
91 hooks.edit = { _ in true }
92 surface.hooks = hooks
93 surface.isEditable = true
94 surface.attach(text)
95 var start = ContinuousClock.now
96 surface.apply(DocumentSurface.Publication(generation: 1, projection: projection, revision: 1, dirty: nil))
97 text.layoutIfNeeded()
98 let open = Self.ms(start)
99 let content = text.textStorage.string as NSString
100 var at = content.length / 3
101 while at < content.length, content.character(at: at) == 10 { at += 1 }
102 text.selectedRange = NSRange(location: at, length: 0)
103 text.scrollRangeToVisible(text.selectedRange)
104 text.layoutIfNeeded()
105 try await Task.sleep(for: .milliseconds(200))
106 var keys: [Double] = [], lines: [Int] = []
107 for _ in 0..<40 {
108 #if DOCS_PROBES
109 let lines0 = DocsProbes.lineRequests
110 #endif
111 start = .now
112 let range = text.selectedRange
113 if text.delegate?.textView?(text, shouldChangeTextIn: range, replacementText: "x") ?? true {
114 text.textStorage.replaceCharacters(in: range, with: NSAttributedString(string: "x", attributes: text.typingAttributes))
115 text.selectedRange = NSRange(location: range.location + 1, length: 0)
116 text.delegate?.textViewDidChange?(text)
117 }
118 text.layoutIfNeeded()
119 keys.append(Self.ms(start))
120 #if DOCS_PROBES
121 lines.append(DocsProbes.lineRequests - lines0)
122 #endif
123 try await Task.sleep(for: .milliseconds(10))
124 }
125 let sorted = keys.sorted()
126 print(String(format: "KEYSTROKE TIMING %@ paged=%d: open %.1f ms; key + layout median %.2f ms p95 %.2f ms max %.2f ms; line requests per key median %d max %d",
127 name, paged ? 1 : 0, open, Self.median(keys), sorted[Int(Double(sorted.count - 1) * 0.95)], sorted.last ?? 0,
128 lines.sorted()[lines.count / 2], lines.max() ?? 0))
129 text.removeFromSuperview()
130 window.isHidden = true
131 }
132 }
133 }
134 static func ms(_ start: ContinuousClock.Instant) -> Double {
135 let d = start.duration(to: .now); return Double(d.components.seconds) * 1000 + Double(d.components.attoseconds) / 1e15
136 }
137 static func median(_ values: [Double]) -> Double { let s = values.sorted(); return s.isEmpty ? .nan : s[s.count / 2] }
138 static func appending(_ text: String, to node: CRDTNode) -> CRDTNode {
139 var node = node
140 if var runs = node.runs, !runs.isEmpty { runs[0].insert += text; node.runs = runs; return node }
141 if var children = node.children {
142 for index in children.indices {
143 let changed = appending(text, to: children[index])
144 if changed != children[index] { children[index] = changed; node.children = children; return node }
145 }
146 }
147 return node
148 }
149}
150#endif
151