Skip to content

Commit 06c16b1

Browse files
authored
Add future wait benchmark to catch memory leaks (#2931)
### Motivation: In the past we introduced a memory leak around the creation of and waiting on futures - we should protect against leaks in this fundamental operation. ### Modifications: Add a new benchmark which would have failed with the previous bug ### Result: Regression protection for this type of memory leak.
1 parent 290b349 commit 06c16b1

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

Benchmarks/Benchmarks/NIOCoreBenchmarks/Benchmarks.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ let benchmarks = {
2121
.mallocCountTotal
2222
]
2323

24+
let leakMetrics: [BenchmarkMetric] = [
25+
.mallocCountTotal,
26+
.memoryLeaked,
27+
]
28+
2429
Benchmark(
2530
"NIOAsyncChannel.init",
2631
configuration: .init(
@@ -46,4 +51,28 @@ let benchmarks = {
4651
blackHole(asyncChanel)
4752
}
4853
}
54+
55+
Benchmark(
56+
"WaitOnPromise",
57+
configuration: .init(
58+
metrics: leakMetrics,
59+
scalingFactor: .kilo,
60+
maxDuration: .seconds(10_000_000),
61+
maxIterations: 10_000 // need 10k to get a signal
62+
)
63+
) { benchmark in
64+
// Elide the cost of the 'EmbeddedEventLoop'.
65+
let el = EmbeddedEventLoop()
66+
67+
benchmark.startMeasurement()
68+
defer {
69+
benchmark.stopMeasurement()
70+
}
71+
72+
for _ in 0..<benchmark.scaledIterations.count {
73+
let p = el.makePromise(of: Int.self)
74+
p.succeed(0)
75+
do { _ = try! p.futureResult.wait() }
76+
}
77+
}
4978
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"mallocCountTotal" : 6000,
3+
"memoryLeaked" : 0
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"mallocCountTotal" : 6000,
3+
"memoryLeaked" : 0
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"mallocCountTotal" : 6000,
3+
"memoryLeaked" : 0
4+
}

0 commit comments

Comments
 (0)