Skip to content

Commit

Permalink
Add a benchmark for SSH throughput. (apple#83)
Browse files Browse the repository at this point in the history
Motivation:

We should have more benchmarks.

Modifications:

Add a throughput benchmark that just sends a bunch of data through.

Result:

We'll have more data.
  • Loading branch information
Lukasa committed May 11, 2021
1 parent f4fdb9e commit aab4ce5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
63 changes: 63 additions & 0 deletions Sources/NIOSSHPerformanceTester/BenchmarkLinearThroughput.swift
@@ -0,0 +1,63 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2021 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 Crypto
import NIO
import NIOSSH

final class BenchmarkLinearThroughput: Benchmark {
let serverRole = SSHConnectionRole.server(.init(hostKeys: [.init(ed25519Key: .init())], userAuthDelegate: ExpectPasswordDelegate("password")))
let clientRole = SSHConnectionRole.client(.init(userAuthDelegate: RepeatingPasswordDelegate("password"), serverAuthDelegate: ClientAlwaysAcceptHostKeyDelegate()))
let b2b = BackToBackEmbeddedChannel()
let messageCount: Int
let messageSize: Int
private var message: ByteBuffer?
private var channel: Channel?

init(messageCount: Int, messageSize: Int) {
self.messageCount = messageCount
self.messageSize = messageSize
}

func setUp() throws {
self.b2b.client.connect(to: try .init(unixDomainSocketPath: "/foo"), promise: nil)
self.b2b.server.connect(to: try .init(unixDomainSocketPath: "/foo"), promise: nil)

let clientHandler = NIOSSHHandler(role: self.clientRole, allocator: self.b2b.client.allocator, inboundChildChannelInitializer: nil)

try self.b2b.client.pipeline.addHandler(clientHandler).wait()
try self.b2b.server.pipeline.addHandler(NIOSSHHandler(role: self.serverRole, allocator: self.b2b.server.allocator, inboundChildChannelInitializer: nil)).wait()
try self.b2b.interactInMemory()

let clientChannelPromise = self.b2b.client.eventLoop.makePromise(of: Channel.self)
clientHandler.createChannel(clientChannelPromise, channelType: .session) { channel, _ in channel.eventLoop.makeSucceededVoidFuture() }
try self.b2b.interactInMemory()

self.channel = try clientChannelPromise.futureResult.wait()
self.message = ByteBuffer(repeating: 0xFF, count: self.messageSize)
}

func tearDown() {}

func run() throws -> Int {
let channel = self.channel!
let message = SSHChannelData(type: .channel, data: .byteBuffer(self.message!))

for _ in 0 ..< self.messageCount {
channel.writeAndFlush(message, promise: nil)
try self.b2b.interactInMemory()
}

return self.messageCount
}
}
4 changes: 3 additions & 1 deletion Sources/NIOSSHPerformanceTester/main.swift
Expand Up @@ -60,4 +60,6 @@ public func measureAndPrint(desc: String, fn: () throws -> Int) rethrows {

// MARK: Utilities

try measureAndPrint(desc: "10000_handshakes", benchmark: BenchmarkHandshake(loopCount: 10000))
try measureAndPrint(desc: "10_handshakes", benchmark: BenchmarkHandshake(loopCount: 10))
try measureAndPrint(desc: "10000_messages_10b_throughput", benchmark: BenchmarkLinearThroughput(messageCount: 10000, messageSize: 10))
try measureAndPrint(desc: "10000_messages_1k_throughput", benchmark: BenchmarkLinearThroughput(messageCount: 10000, messageSize: 1000))
2 changes: 1 addition & 1 deletion scripts/soundness.sh
Expand Up @@ -18,7 +18,7 @@ here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

function replace_acceptable_years() {
# this needs to replace all acceptable forms with 'YEARS'
sed -e 's/2017-20[12][890]/YEARS/' -e 's/2019-2020/YEARS/' -e 's/2019/YEARS/' -e 's/2020/YEARS/'
sed -e 's/2017-20[12][890]/YEARS/' -e 's/2019-2020/YEARS/' -e 's/2019/YEARS/' -e 's/2020/YEARS/' -e 's/2021/YEARS/'
}

command -v swiftformat >/dev/null 2>&1 || { echo >&2 "swiftformat needs to be installed but is not available in the path."; exit 1; }
Expand Down

0 comments on commit aab4ce5

Please sign in to comment.