Skip to content

Commit

Permalink
Add future wait benchmark to catch memory leaks (#2931)
Browse files Browse the repository at this point in the history
### 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.
  • Loading branch information
rnro authored Oct 17, 2024
1 parent 290b349 commit 06c16b1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Benchmarks/Benchmarks/NIOCoreBenchmarks/Benchmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ let benchmarks = {
.mallocCountTotal
]

let leakMetrics: [BenchmarkMetric] = [
.mallocCountTotal,
.memoryLeaked,
]

Benchmark(
"NIOAsyncChannel.init",
configuration: .init(
Expand All @@ -46,4 +51,28 @@ let benchmarks = {
blackHole(asyncChanel)
}
}

Benchmark(
"WaitOnPromise",
configuration: .init(
metrics: leakMetrics,
scalingFactor: .kilo,
maxDuration: .seconds(10_000_000),
maxIterations: 10_000 // need 10k to get a signal
)
) { benchmark in
// Elide the cost of the 'EmbeddedEventLoop'.
let el = EmbeddedEventLoop()

benchmark.startMeasurement()
defer {
benchmark.stopMeasurement()
}

for _ in 0..<benchmark.scaledIterations.count {
let p = el.makePromise(of: Int.self)
p.succeed(0)
do { _ = try! p.futureResult.wait() }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"mallocCountTotal" : 6000,
"memoryLeaked" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"mallocCountTotal" : 6000,
"memoryLeaked" : 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"mallocCountTotal" : 6000,
"memoryLeaked" : 0
}

0 comments on commit 06c16b1

Please sign in to comment.