| 1 | import Foundation |
| 2 | import XCTest |
| 3 | @testable import DocsKit |
| 4 | |
| 5 | final class NativePerformanceProbeTests: XCTestCase { |
| 6 | func testTypingProbe() async throws { |
| 7 | let mode = ProcessInfo.processInfo.environment["DOCS_PROBE_MODE"] ?? "fresh" |
| 8 | let limit = Int(ProcessInfo.processInfo.environment["DOCS_PROBE_COUNT"] ?? "100")! |
| 9 | let replica = CRDTReplica(guid: "performance-probe") |
| 10 | _ = try await replica.open(update: Data()) |
| 11 | _ = try await replica.command(["op": .string("insert_element"), "at": .number(0), "type": .string("paragraph")]) |
| 12 | let first = try await replica.presentation(source: 0, selection: NSRange(location: 0, length: 0)) |
| 13 | var generation = try XCTUnwrap(first?.0) |
| 14 | let clock = ContinuousClock() |
| 15 | let begin = clock.now |
| 16 | var totalBytes = 0, maximumBytes = 0 |
| 17 | let batchSize = mode == "batch" ? 100 : 1 |
| 18 | var inserted = 0 |
| 19 | while inserted < limit { |
| 20 | let count = min(batchSize, limit - inserted) |
| 21 | let (update, _) = try await replica.command(["op": .string("display_edit"), "generation": .number(Double(generation)), "at": .number(Double(inserted)), "length": .number(0), "text": .string(String(repeating: "a", count: count))]) |
| 22 | totalBytes += update.count; maximumBytes = max(maximumBytes, update.count) |
| 23 | inserted += count |
| 24 | await replica.retainGenerations([generation]) |
| 25 | if mode != "retained" { |
| 26 | let next = try await replica.presentation(source: generation, selection: NSRange(location: inserted, length: 0), allowReuse: mode == "reuse") |
| 27 | generation = try XCTUnwrap(next?.0) |
| 28 | } |
| 29 | if [100, 1000, 5000].contains(inserted) { |
| 30 | let elapsed = begin.duration(to: clock.now) |
| 31 | let (snapshot, projection) = try await replica.command(["op": .string("snapshot")]) |
| 32 | let (clientsData, _) = try await replica.command(["op": .string("clients")]) |
| 33 | let clients = try JSONDecoder().decode([UInt64].self, from: clientsData) |
| 34 | XCTAssertEqual(projection.children.map(\.plainText).joined().count, inserted) |
| 35 | print("PROBE mode=\(mode) count=\(inserted) elapsed=\(elapsed) snapshot=\(snapshot.count) updateTotal=\(totalBytes) updateMax=\(maximumBytes) clients=\(clients.count)") |
| 36 | fflush(stdout) |
| 37 | } |
| 38 | } |
| 39 | await replica.close() |
| 40 | } |
| 41 | } |
| 42 | |