Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,5 @@ iOSInjectionProject/

# VS Code
.vscode/

.DS_Store
99 changes: 99 additions & 0 deletions Benchmarks/Benchmarks/DataLoaderBenchmarks.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import Benchmark
import NIO
import AsyncDataLoader
import DataLoader

let eventLoopGroup = MultiThreadedEventLoopGroup.singleton

let benchmarks: @Sendable () -> Void = {

// MARK: Async

let nonBatchingLoader = DataLoader<Int, Int>(
options: .init(
batchingEnabled: true,
cachingEnabled: false,
)
) { keys in
keys.map { DataLoaderValue.success($0) }
}

let batchingLoader = DataLoader<Int, Int>(
options: .init(
batchingEnabled: true,
cachingEnabled: false,
maxBatchSize: 10
)
) { keys in
keys.map { DataLoaderValue.success($0) }
}

Benchmark("loadNonBatching") { _ in
try await withThrowingTaskGroup { group in
for i in (0..<1_000) {
group.addTask {
try await nonBatchingLoader.load(key: i)
}
}
try await group.waitForAll()
}
}

Benchmark("loadBatching") { _ in
try await withThrowingTaskGroup { group in
for i in (0..<1_000) {
group.addTask {
try await batchingLoader.load(key: i)
}
}
try await group.waitForAll()
}
}

Benchmark("loadBatchingMany") { _ in
let result = try await batchingLoader.loadMany(keys: Array(0..<1_000))
}

// MARK: NIO

let nioNonBatchingLoader = DataLoader<Int, Int>(
options: .init(
batchingEnabled: true,
cachingEnabled: false,
)
) { keys in
return eventLoopGroup.next().makeSucceededFuture(
keys.map { DataLoaderFutureValue.success($0) }
)
}

let nioBatchingLoader = DataLoader<Int, Int>(
options: .init(
batchingEnabled: true,
cachingEnabled: false,
maxBatchSize: 10
)
) { keys in
return eventLoopGroup.next().makeSucceededFuture(
keys.map { DataLoaderFutureValue.success($0) }
)
}

Benchmark("nioLoadNonBatching") { _ in
let futures = try (0..<1_000).map { i in
try nioNonBatchingLoader.load(key: i, on: eventLoopGroup.next())
}
let result = try EventLoopFuture.whenAllSucceed(futures, on: eventLoopGroup.next()).wait()
}

Benchmark("nioLoadBatching") { _ in
let futures = try (0..<1_000).map { i in
try nioBatchingLoader.load(key: i, on: eventLoopGroup.next())
}
let result = try EventLoopFuture.whenAllSucceed(futures, on: eventLoopGroup.next()).wait()
}

Benchmark("nioLoadBatchingMany") { _ in
let result = try nioBatchingLoader.loadMany(keys: Array(0..<1_000), on: eventLoopGroup.next()).wait()
}
}
113 changes: 113 additions & 0 deletions Benchmarks/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions Benchmarks/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// swift-tools-version:5.10
import PackageDescription

let package = Package(
name: "Benchmarks",
platforms: [.macOS(.v13)],
dependencies: [
.package(name: "DataLoader", path: "../"),
.package(url: "https://github.com/ordo-one/package-benchmark", from: "1.4.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.84.0"),
],
targets: [
.executableTarget(
name: "Benchmarks",
dependencies: [
.product(name: "Benchmark", package: "package-benchmark"),
.product(name: "AsyncDataLoader", package: "DataLoader"),
.product(name: "DataLoader", package: "DataLoader"),
.product(name: "NIO", package: "swift-nio"),
],
path: "Benchmarks",
plugins: [
.plugin(name: "BenchmarkPlugin", package: "package-benchmark")
]
)
]
)
Loading