| 1 | #if os(iOS) |
| 2 | import Foundation |
| 3 | import UIKit |
| 4 | import XCTest |
| 5 | @testable import DocsKit |
| 6 | |
| 7 | /// iPhone text-view cost per publication (realtime probes). The iOS |
| 8 | /// NativeDocumentTextView replaces `attributedText` wholesale for every |
| 9 | /// published projection; this prices that against reconciling the same |
| 10 | /// change into the view's own text storage, plus the per-keystroke getters. |
| 11 | @MainActor final class RealtimeAuditIOSTests: XCTestCase { |
| 12 | func testTextViewPublicationCost() async throws { |
| 13 | guard let dir = ProcessInfo.processInfo.environment["DOCS_RT_AUDIT_DOCS"] else { throw XCTSkip("Set DOCS_RT_AUDIT_DOCS") } |
| 14 | let names = (ProcessInfo.processInfo.environment["DOCS_RT_AUDIT_ONLY"] ?? "p1,p10,p20,p50").split(separator: ",").map(String.init) |
| 15 | for name in names { |
| 16 | let bytes = try Data(contentsOf: URL(fileURLWithPath: "\(dir)/\(name).bin")) |
| 17 | let replica = CRDTReplica(guid: "ios-" + name) |
| 18 | try await replica.open(update: bytes) |
| 19 | let projection = try await replica.projection() |
| 20 | var content = NativeTextProjection.render(projection) |
| 21 | let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 402, height: 874)) |
| 22 | // The production view: DocsTextView (TextKit 1, ensureLayout in layoutSubviews). |
| 23 | let text = DocsTextView(frame: window.bounds) |
| 24 | text.paged = ProcessInfo.processInfo.environment["DOCS_RT_AUDIT_PAGED"] != "0" |
| 25 | window.addSubview(text); window.makeKeyAndVisible() |
| 26 | var t = ContinuousClock.now |
| 27 | text.attributedText = content; text.layoutIfNeeded() |
| 28 | RealtimeAuditTests.record(name, "ios: initial attributedText + layout", [RealtimeAuditTests.ms(t)], note: "utf16=\(content.length)") |
| 29 | let middle = RealtimeAuditTests.middle(content) |
| 30 | text.selectedRange = NSRange(location: middle, length: 0) |
| 31 | text.scrollRangeToVisible(text.selectedRange) |
| 32 | var replace: [Double] = [], reconcile: [Double] = [], getAttributed: [Double] = [], getText: [Double] = [] |
| 33 | for i in 0..<8 { |
| 34 | let next = NSMutableAttributedString(attributedString: content) |
| 35 | next.replaceCharacters(in: NSRange(location: middle + i, length: 0), with: NSAttributedString(string: "x", attributes: content.attributes(at: middle + i, effectiveRange: nil))) |
| 36 | // Production path (NativeDocumentTextView.swift, iOS applyRender). |
| 37 | t = .now |
| 38 | text.attributedText = next |
| 39 | text.selectedRange = NSRange(location: middle + i + 1, length: 0) |
| 40 | text.layoutIfNeeded() |
| 41 | replace.append(RealtimeAuditTests.ms(t)) |
| 42 | // Alternative: the Mac's reconciliation into the live storage. |
| 43 | let again = NSMutableAttributedString(attributedString: next) |
| 44 | again.replaceCharacters(in: NSRange(location: middle + i + 1, length: 0), with: NSAttributedString(string: "y", attributes: next.attributes(at: middle + i, effectiveRange: nil))) |
| 45 | t = .now |
| 46 | DocumentRenderReconciliation.apply(again, to: text.textStorage) |
| 47 | text.layoutIfNeeded() |
| 48 | reconcile.append(RealtimeAuditTests.ms(t)) |
| 49 | content = again |
| 50 | // Per keystroke: shouldChangeTextIn reads attributedText (a copy); |
| 51 | // textViewDidChangeSelection reads text twice. |
| 52 | t = .now; _ = text.attributedText; getAttributed.append(RealtimeAuditTests.ms(t)) |
| 53 | t = .now; _ = text.text.utf16.count; _ = text.text.hasSuffix("\n"); getText.append(RealtimeAuditTests.ms(t)) |
| 54 | } |
| 55 | RealtimeAuditTests.record(name, "ios: attributedText = (production) + layout", replace) |
| 56 | RealtimeAuditTests.record(name, "ios: reconcile into textStorage + layout", reconcile) |
| 57 | RealtimeAuditTests.record(name, "ios: textView.attributedText getter", getAttributed) |
| 58 | RealtimeAuditTests.record(name, "ios: textView.text x2 (selection change)", getText) |
| 59 | window.isHidden = true |
| 60 | await replica.close() |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | #endif |
| 65 | |