Skip to content

Commit 3631941

Browse files
feat: Adds Benchmarks
1 parent 7cbbb33 commit 3631941

4 files changed

Lines changed: 241 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,5 @@ iOSInjectionProject/
9191

9292
# VS Code
9393
.vscode/
94+
95+
.DS_Store
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import Benchmark
2+
import NIO
3+
import AsyncDataLoader
4+
import DataLoader
5+
6+
let eventLoopGroup = MultiThreadedEventLoopGroup.singleton
7+
8+
let benchmarks: @Sendable () -> Void = {
9+
10+
// MARK: Async
11+
12+
let nonBatchingLoader = DataLoader<Int, Int>(
13+
options: .init(
14+
batchingEnabled: true,
15+
cachingEnabled: false,
16+
)
17+
) { keys in
18+
keys.map { DataLoaderValue.success($0) }
19+
}
20+
21+
let batchingLoader = DataLoader<Int, Int>(
22+
options: .init(
23+
batchingEnabled: true,
24+
cachingEnabled: false,
25+
maxBatchSize: 10
26+
)
27+
) { keys in
28+
keys.map { DataLoaderValue.success($0) }
29+
}
30+
31+
Benchmark("loadNonBatching") { _ in
32+
try await withThrowingTaskGroup { group in
33+
for i in (0..<1_000) {
34+
group.addTask {
35+
try await nonBatchingLoader.load(key: i)
36+
}
37+
}
38+
try await group.waitForAll()
39+
}
40+
}
41+
42+
Benchmark("loadBatching") { _ in
43+
try await withThrowingTaskGroup { group in
44+
for i in (0..<1_000) {
45+
group.addTask {
46+
try await batchingLoader.load(key: i)
47+
}
48+
}
49+
try await group.waitForAll()
50+
}
51+
}
52+
53+
Benchmark("loadBatchingMany") { _ in
54+
let result = try await batchingLoader.loadMany(keys: Array(0..<1_000))
55+
}
56+
57+
// MARK: NIO
58+
59+
let nioNonBatchingLoader = DataLoader<Int, Int>(
60+
options: .init(
61+
batchingEnabled: true,
62+
cachingEnabled: false,
63+
)
64+
) { keys in
65+
return eventLoopGroup.next().makeSucceededFuture(
66+
keys.map { DataLoaderFutureValue.success($0) }
67+
)
68+
}
69+
70+
let nioBatchingLoader = DataLoader<Int, Int>(
71+
options: .init(
72+
batchingEnabled: true,
73+
cachingEnabled: false,
74+
maxBatchSize: 10
75+
)
76+
) { keys in
77+
return eventLoopGroup.next().makeSucceededFuture(
78+
keys.map { DataLoaderFutureValue.success($0) }
79+
)
80+
}
81+
82+
Benchmark("nioLoadNonBatching") { _ in
83+
let futures = try (0..<1_000).map { i in
84+
try nioNonBatchingLoader.load(key: i, on: eventLoopGroup.next())
85+
}
86+
let result = try EventLoopFuture.whenAllSucceed(futures, on: eventLoopGroup.next()).wait()
87+
}
88+
89+
Benchmark("nioLoadBatching") { _ in
90+
let futures = try (0..<1_000).map { i in
91+
try nioBatchingLoader.load(key: i, on: eventLoopGroup.next())
92+
}
93+
let result = try EventLoopFuture.whenAllSucceed(futures, on: eventLoopGroup.next()).wait()
94+
}
95+
96+
Benchmark("nioLoadBatchingMany") { _ in
97+
let result = try nioBatchingLoader.loadMany(keys: Array(0..<1_000), on: eventLoopGroup.next()).wait()
98+
}
99+
}

Benchmarks/Package.resolved

Lines changed: 113 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Benchmarks/Package.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// swift-tools-version:5.10
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "Benchmarks",
6+
platforms: [.macOS(.v13)],
7+
dependencies: [
8+
.package(name: "DataLoader", path: "../"),
9+
.package(url: "https://github.com/ordo-one/package-benchmark", from: "1.4.0"),
10+
.package(url: "https://github.com/apple/swift-nio.git", from: "2.84.0"),
11+
],
12+
targets: [
13+
.executableTarget(
14+
name: "Benchmarks",
15+
dependencies: [
16+
.product(name: "Benchmark", package: "package-benchmark"),
17+
.product(name: "AsyncDataLoader", package: "DataLoader"),
18+
.product(name: "DataLoader", package: "DataLoader"),
19+
.product(name: "NIO", package: "swift-nio"),
20+
],
21+
path: "Benchmarks",
22+
plugins: [
23+
.plugin(name: "BenchmarkPlugin", package: "package-benchmark")
24+
]
25+
)
26+
]
27+
)

0 commit comments

Comments
 (0)