Skip to content

Commit

Permalink
Add benchmark for creating NIOAsyncChannel
Browse files Browse the repository at this point in the history
Motivation:

NIOAsyncChannel has a number of allocs associated with it. We should
have a benchmark which tracks the number of allocs.

Modifications:

- Add NIOCoreBenchmarks with a single benchmark

Result:

Better insight
  • Loading branch information
glbrntt committed Jul 9, 2024
1 parent abbb144 commit 4f99773
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Benchmarks/Benchmarks/NIOCoreBenchmarks/Benchmarks.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2024 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Benchmark
import NIOCore
import NIOEmbedded

let benchmarks = {
let defaultMetrics: [BenchmarkMetric] = [
.mallocCountTotal,
]

Benchmark(
"NIOAsyncChannel.init",
configuration: Benchmark.Configuration(metrics: defaultMetrics)
) { benchmark in
// Elide the cost of the 'EmbeddedChannel'. It's only used for its pipeline.
var channels: [EmbeddedChannel] = []
channels.reserveCapacity(benchmark.scaledIterations.count)
for _ in 0 ..< benchmark.scaledIterations.count {
channels.append(EmbeddedChannel())
}

benchmark.startMeasurement()
defer {
benchmark.stopMeasurement()
}
for channel in channels {
let asyncChanel = try NIOAsyncChannel<ByteBuffer, ByteBuffer>(wrappingChannelSynchronously: channel)
blackHole(asyncChanel)
}
}
}
12 changes: 12 additions & 0 deletions Benchmarks/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,17 @@ let package = Package(
.plugin(name: "BenchmarkPlugin", package: "package-benchmark")
]
),
.executableTarget(
name: "NIOCoreBenchmarks",
dependencies: [
.product(name: "Benchmark", package: "package-benchmark"),
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "NIOEmbedded", package: "swift-nio"),
],
path: "Benchmarks/NIOCoreBenchmarks",
plugins: [
.plugin(name: "BenchmarkPlugin", package: "package-benchmark")
]
),
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 12
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 12
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 12
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 12
}

0 comments on commit 4f99773

Please sign in to comment.