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

adding external benchmarks using package-benchmark #34

Merged
merged 7 commits into from
Feb 27, 2023
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
6 changes: 4 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "Swift",
"name": "Swift",
"image": "swift:5.7",
"features": {
"ghcr.io/devcontainers/features/common-utils:2": {
"installZsh": "false",
"installZsh": "true",
"username": "vscode",
"userUid": "1000",
"userGid": "1000",
Expand All @@ -13,6 +13,8 @@
"version": "os-provided",
"ppa": "false"
}
"ghcr.io/heckj/jemalloc/jemalloc:1": {
}
},
"runArgs": [
"--cap-add=SYS_PTRACE",
Expand Down
10 changes: 10 additions & 0 deletions ExternalBenchmarks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/config/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
.vscode
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import BenchmarkSupport // imports supporting infrastructure for running the benchmarks
import Foundation
import ExtrasJSON
import CRDT

@main extension BenchmarkRunner {} // Required for the main() definition to no get linker errors

enum TextOp {
case insert(cursor: UInt32, value: String)
case delete(cursor: UInt32, count: UInt32)
}

func loadEditingTrace () async -> Data {
guard let traceURL = Bundle.module.url(forResource: "editing-trace", withExtension: "json") else {
fatalError("Unable to find editing-trace.json in bundle")
}
let data: Data
do {
data = try Data(contentsOf: traceURL, options: .mappedIfSafe)
} catch {
fatalError("failed load JSON data from editing-trace.json URL")
}
// print("[TESTING DEBUG OUTPUT] bytes loaded: \(data.count)")
return data
}

func parseDataIntoJSON(data: Data) async -> JSONValue {
do {
return try ExtrasJSON.JSONParser().parse(bytes: data)
} catch {
fatalError("failed parse JSON")
}
}

func parseJSONIntoTrace(topOfTrace: JSONValue) async -> [TextOp] {
// var insert = 0
// var delete = 0
var trace:[TextOp] = []

switch topOfTrace {
case let .array(opsJSONValues):
// print("Identified \(opsJSONValues.count)")
for opsJSONValue in opsJSONValues {
if case let .array(internalOpValues) = opsJSONValue {
let cursorPos: UInt32
if case let .number(cursorString) = internalOpValues[0] {
cursorPos = UInt32(cursorString)!
} else {
fatalError("Invalid cursor position")
}
if case let .number(opString) = internalOpValues[1] {
if opString == "0" {
// insert += 1
if case let .string(foundInsertString) = internalOpValues[2] {
trace.append(TextOp.insert(cursor: cursorPos, value: foundInsertString))
}
}
if opString == "1" {
// delete += 1
trace.append(TextOp.delete(cursor: cursorPos, count: 1))
}
}
}
}
default:
// print("WTF? Incorrect thing found.")
fatalError("Top value of JSON wasn't an array.")
}
// print("[TESTING DEBUG OUTPUT] - \(insert) inserts, \(delete) deletes")
// print("Created trace with \(trace.count) elements")
return trace
}

@_dynamicReplacement(for: registerBenchmarks) // And this is how we register our benchmarks
func benchmarks() {
// Benchmark.defaultConfiguration.timeUnits = .microseconds
Benchmark.defaultConfiguration.desiredIterations = .count(1)
Benchmark.defaultConfiguration.desiredDuration = .seconds(3)

Benchmark("Loading JSON trace data",
configuration: .init(metrics: [.throughput, .wallClock], desiredIterations: 20)) { benchmark in
for _ in benchmark.throughputIterations {
blackHole(await loadEditingTrace())
}
}

Benchmark("parse JSON with Swift Extras parser",
configuration: .init(metrics: [.throughput, .wallClock], desiredIterations: 50)) { benchmark in
for _ in benchmark.throughputIterations {
let data = await loadEditingTrace()
benchmark.startMeasurement()
blackHole(await parseDataIntoJSON(data: data))
benchmark.stopMeasurement()
}
}

Benchmark("process JSON into trace data",
configuration: .init(metrics: [.throughput, .wallClock], desiredIterations: 30)) { benchmark in
for _ in benchmark.throughputIterations {
let data = await loadEditingTrace()
let jsonValue = await parseDataIntoJSON(data: data)
benchmark.startMeasurement()
blackHole(await parseJSONIntoTrace(topOfTrace: jsonValue))
benchmark.stopMeasurement()
}
}

Benchmark("Create single-character List CRDT",
configuration: .init(metrics: BenchmarkMetric.all, throughputScalingFactor: .kilo)) { benchmark in
for _ in benchmark.throughputIterations {
blackHole(blackHole(List(actorId: "a", ["a"])))
}
}

Benchmark("List six-character append",
configuration: .init(metrics: [.throughput, .wallClock], throughputScalingFactor: .kilo)) { benchmark in
for _ in benchmark.throughputIterations {
var mylist = List(actorId: "a", ["a"])
benchmark.startMeasurement()
mylist.append(" hello")
benchmark.stopMeasurement()
}
}

Benchmark("List six-character append, individual characters",
configuration: .init(metrics: [.throughput, .wallClock], throughputScalingFactor: .kilo)) { benchmark in
for _ in benchmark.throughputIterations {
var mylist = List(actorId: "a", ["a"])
let appendList = [" ", "h", "e", "l", "l", "o"]
benchmark.startMeasurement()
for val in appendList {
mylist.append(val)
}
benchmark.stopMeasurement()
}
}

}
11 changes: 11 additions & 0 deletions ExternalBenchmarks/Benchmarks/ExternalBenchmarks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Replica of the edit-by-index sequence provided by Martin Kleppmann
in [Automerge-perf](https://github.com/automerge/automerge-perf.git)

General format of the JSON:
2nd Int = op
- 0 => Insert [Column (pos0), String(pos2)]
- 1 => Delete [Column (pos0)

ref article for decoding in an enum w/ associated value in swift:
https://www.donnywals.com/splitting-a-json-object-into-an-enum-and-an-associated-object-with-codable/

Loading