Skip to content

Commit

Permalink
Add loro to benchmarks (automerge#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
jessegrosjean authored Aug 16, 2024
1 parent 9548e39 commit 4159e71
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CollectionBenchmarks/Library.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,31 @@
"title": "Text - append",
"tasks": [
"Text - append",
"Text - append (Loro)",
]
},
{
"kind": "chart",
"title": "Text - append and read",
"tasks": [
"Text - append and read",
"Text - append and read (Loro)",
]
},
{
"kind": "chart",
"title": "List - Integer append",
"tasks": [
"List - Integer append",
"List - Integer append (Loro)",
]
},
{
"kind": "chart",
"title": "Map - Integer append",
"tasks": [
"Map - Integer append",
"Map - Integer append (Loro)",
]
},
]
Expand Down
4 changes: 3 additions & 1 deletion CollectionBenchmarks/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import PackageDescription

let package = Package(
name: "AutomergeCollectionBenchmarks",
platforms: [.macOS(.v10_15)],
platforms: [.macOS(.v12)],
dependencies: [
.package(url: "https://github.com/apple/swift-collections-benchmark", from: "0.0.1"),
.package(url: "https://github.com/loro-dev/loro-swift.git", from: "0.16.2-alpha.3"),
.package(path: "../"),
],
targets: [
Expand All @@ -16,6 +17,7 @@ let package = Package(
name: "CollectionBenchmarks",
dependencies: [
.product(name: "Automerge", package: "automerge-swift"),
.product(name: "Loro", package: "loro-swift"),
.product(
name: "CollectionsBenchmark",
package: "swift-collections-benchmark"
Expand Down
70 changes: 69 additions & 1 deletion CollectionBenchmarks/Sources/CollectionBenchmarks/main.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Automerge
import CollectionsBenchmark
import Foundation
import Automerge
import Loro

// NOTE(heckj): collections-benchmark implementations can be a bit hard to understand
// from the opaque inputs and structure of the code.
Expand Down Expand Up @@ -76,6 +77,23 @@ benchmark.addSimple(
blackHole(doc)
}

benchmark.addSimple(
title: "Text - append (Loro)",
input: [String].self
) { input in
let doc = LoroDoc()
let text = doc.getText(id: "text")

var stringLength = text.lenUnicode()
for strChar in input {
_ = try! text.splice(pos: stringLength, len: 0, s: strChar)
stringLength = text.lenUnicode()
}
// precondition(stringLength == input.count) // NOT VALID - difference in UTF-8 codepoints and how strings represent
// lengths
blackHole(doc)
}

benchmark.addSimple(
title: "Text - append and read",
input: [String].self
Expand All @@ -94,6 +112,25 @@ benchmark.addSimple(
blackHole(resultingString)
}

benchmark.addSimple(
title: "Text - append and read (Loro)",
input: [String].self
) { input in
let doc = LoroDoc()
let text = doc.getText(id: "text")

var stringLength = text.lenUnicode()
for strChar in input {
_ = try! text.splice(pos: stringLength, len: 0, s: strChar)
stringLength = text.lenUnicode()
}

let resultingString = text.toString()
// precondition(stringLength == input.count) // NOT VALID - difference in UTF-8 codepoints and how strings represent
// lengths
blackHole(resultingString)
}

benchmark.addSimple(
title: "List - Integer append",
input: [Int].self
Expand All @@ -110,6 +147,22 @@ benchmark.addSimple(
blackHole(doc)
}

benchmark.addSimple(
title: "List - Integer append (Loro)",
input: [Int].self
) { integerInput in
let doc = LoroDoc()
let numList = doc.getList(id: "list")

for intValue in integerInput {
let listLength = numList.len()
try! numList.insert(pos: listLength, v: intValue)
}
// precondition(stringLength == input.count) // NOT VALID - difference in UTF-8 codepoints and how strings represent
// lengths
blackHole(doc)
}

benchmark.addSimple(
title: "Map - Integer append",
input: [Int].self
Expand All @@ -125,5 +178,20 @@ benchmark.addSimple(
blackHole(doc)
}

benchmark.addSimple(
title: "Map - Integer append (Loro)",
input: [Int].self
) { integerInput in
let doc = LoroDoc()
let numberMap = doc.getMap(id: "map")

for intValue in integerInput {
try! numberMap.insert(key: String(intValue), v: intValue)
}
// precondition(stringLength == input.count) // NOT VALID - difference in UTF-8 codepoints and how strings represent
// lengths
blackHole(doc)
}

// Execute the benchmark tool with the above definitions.
benchmark.main()

0 comments on commit 4159e71

Please sign in to comment.