Skip to content

Commit 1d1ce62

Browse files
fabianfettLukasa
andauthored
Add perf test for EL as SerialExecutor (#3178)
This PR adds two benchmarks: - Jumping 1k times from the global executor to a NIO EL and back using `el.execute {}` and an `UncheckedContinuation` - Jumping 1k times from the global executor to a NIO EL and back using an actor that has a custom executor Co-authored-by: Cory Benfield <lukasa@apple.com>
1 parent 3fd5e74 commit 1d1ce62

2 files changed

Lines changed: 45 additions & 6 deletions

File tree

Benchmarks/Benchmarks/NIOPosixBenchmarks/Benchmarks.swift

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ private let eventLoop = MultiThreadedEventLoopGroup.singleton.next()
2020

2121
let benchmarks = {
2222
let defaultMetrics: [BenchmarkMetric] = [
23-
.mallocCountTotal
23+
.mallocCountTotal,
24+
.contextSwitches,
25+
.wallClock,
2426
]
2527

2628
Benchmark(
@@ -38,9 +40,6 @@ let benchmarks = {
3840
)
3941
}
4042

41-
// This benchmark is only available above 5.9 since our EL conformance
42-
// to serial executor is also gated behind 5.9.
43-
#if compiler(>=5.9)
4443
Benchmark(
4544
"TCPEchoAsyncChannel",
4645
configuration: .init(
@@ -66,7 +65,6 @@ let benchmarks = {
6665
eventLoop: eventLoop
6766
)
6867
}
69-
#endif
7068

7169
Benchmark(
7270
"MTELG.scheduleTask(in:_:)",
@@ -101,4 +99,45 @@ let benchmarks = {
10199
let handle = try! eventLoop.scheduleCallback(in: .hours(1), handler: timer)
102100
}
103101
}
102+
103+
Benchmark(
104+
"Jump to EL and back using execute and unsafecontinuation",
105+
configuration: .init(
106+
metrics: defaultMetrics,
107+
scalingFactor: .kilo
108+
)
109+
) { benchmark in
110+
for _ in benchmark.scaledIterations {
111+
await withUnsafeContinuation { (continuation: UnsafeContinuation<Void, Never>) in
112+
eventLoop.execute {
113+
continuation.resume()
114+
}
115+
}
116+
}
117+
}
118+
119+
final actor Foo {
120+
nonisolated public let unownedExecutor: UnownedSerialExecutor
121+
122+
init(eventLoop: any EventLoop) {
123+
self.unownedExecutor = eventLoop.executor.asUnownedSerialExecutor()
124+
}
125+
126+
func foo() {
127+
blackHole(Void())
128+
}
129+
}
130+
131+
Benchmark(
132+
"Jump to EL and back using actor with EL executor",
133+
configuration: .init(
134+
metrics: defaultMetrics,
135+
scalingFactor: .kilo
136+
)
137+
) { benchmark in
138+
let actor = Foo(eventLoop: eventLoop)
139+
for _ in benchmark.scaledIterations {
140+
await actor.foo()
141+
}
142+
}
104143
}

Benchmarks/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version: 5.7
1+
// swift-tools-version:5.9
22

33
import PackageDescription
44

0 commit comments

Comments
 (0)