Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply updates from yorkie-js-sdk v0.4.28 #193

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Sources/API/Converter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -983,18 +983,18 @@ extension Converter {

let nodes = pbTreeNodes.compactMap { fromTreeNode($0) }

let root = nodes[nodes.count - 1]
let rootIndex = nodes.count - 1
let root = nodes[rootIndex]

var depthTable: [Int32: CRDTTreeNode] = [
pbTreeNodes[rootIndex].depth: root
]

for index in stride(from: nodes.count - 2, to: -1, by: -1) {
var parent: CRDTTreeNode?
for index2 in index + 1 ..< nodes.count {
if pbTreeNodes[index].depth - 1 == pbTreeNodes[index2].depth {
parent = nodes[index2]
break
}
}

try parent?.prepend(contentsOf: [nodes[index]])
for index in stride(from: rootIndex - 1, to: -1, by: -1) {
let node = nodes[index]
let parent = depthTable[pbTreeNodes[index].depth - 1]
try parent?.prepend(contentsOf: [node])
depthTable[pbTreeNodes[index].depth] = node
}

root.updateDescendantsSize()
Expand Down
74 changes: 69 additions & 5 deletions Tests/Benchmark/DocumentBenchmarkTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import XCTest
@testable import Yorkie

final class DocumentBenchmarkTests: XCTestCase {
func benchmarkTree(_ size: Int) async {
func benchmarkTreeEdit(_ size: Int) async {
let doc = Document(key: "test-doc")

do {
Expand Down Expand Up @@ -130,27 +130,52 @@ final class DocumentBenchmarkTests: XCTestCase {
}
}

func testDocumentTree100() throws {
func benchmarkTreeConvert(_ size: Int) async {
let doc = Document(key: "test-doc")

do {
try await doc.update { root, _ in
var children: [JSONTreeTextNode] = []
for _ in 1 ... size {
children.append(JSONTreeTextNode(value: "a"))
}

root.tree = JSONTree(initialRoot:
JSONTreeElementNode(type: "doc",
children: [JSONTreeElementNode(type: "p", children: children)])
)
}

let root = try await(doc.getRoot().tree as? JSONTree)?.getIndexTree().root
let pbTreeNodes = Converter.toTreeNodes(root)
let convertedTreeNodes = try Converter.fromTreeNodes(pbTreeNodes)
XCTAssertNotNil(convertedTreeNodes, "Tree conversion failed")
} catch {
XCTFail("Benchmark failed with error: \(error)")
}
}

func testDocumentTreeEdit100() throws {
self.measure {
let exp = expectation(description: "measure")

// Put the code you want to measure the time of here.
Task { @MainActor in
await self.benchmarkTree(100)
await self.benchmarkTreeEdit(100)
exp.fulfill()
}

wait(for: [exp], timeout: 1)
}
}

func testDocumentTree1000() throws {
func testDocumentTreeEdit1000() throws {
self.measure {
let exp = expectation(description: "measure")

// Put the code you want to measure the time of here.
Task { @MainActor in
await self.benchmarkTree(1000)
await self.benchmarkTreeEdit(1000)
exp.fulfill()
}

Expand Down Expand Up @@ -227,6 +252,45 @@ final class DocumentBenchmarkTests: XCTestCase {
wait(for: [exp], timeout: 20)
}
}

func testDocumentTreeConvert10000() throws {
self.measure {
let exp = expectation(description: "measure")

Task { @MainActor in
await self.benchmarkTreeConvert(10000)
exp.fulfill()
}

wait(for: [exp], timeout: 20)
}
}

func testDocumentTreeConvert20000() throws {
self.measure {
let exp = expectation(description: "measure")

Task { @MainActor in
await self.benchmarkTreeConvert(20000)
exp.fulfill()
}

wait(for: [exp], timeout: 20)
}
}

func testDocumentTreeConvert30000() throws {
self.measure {
let exp = expectation(description: "measure")

Task { @MainActor in
await self.benchmarkTreeConvert(30000)
exp.fulfill()
}

wait(for: [exp], timeout: 20)
}
}
}

extension String {
Expand Down
Loading