Peter Adams 0f878f95f0
Performance testing for NIO PCAP logging (#98)
Motivation:

It's useful to know the overhead we could be adding by including
the PCAP handler.

Modifications:

Add a new executable based on the NIO performance testing executable.

Result:

There is a new executable which runs a short test of sending and receiving
data through the HTTP/1 handler using multiple eventloops and showing
three options.
1) Vanilla
2) With in memory PCAP never written to disk
3) With a disk based PCAP.
2020-08-03 12:13:24 +01:00

65 lines
1.8 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2020 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 NIO
import NIOExtras
import Foundation
class PCAPPerformanceTest: Benchmark {
let numberOfRepeats: Int
let byteBuffer = ByteBuffer(repeating: 0x65, count: 1000)
init(numberOfRepeats: Int) {
self.numberOfRepeats = numberOfRepeats
}
var outputFile: String!
func setUp() throws {
self.outputFile = NSTemporaryDirectory() + "/" + UUID().uuidString
}
func tearDown() {
try! FileManager.default.removeItem(atPath: self.outputFile)
}
func run() throws -> Int {
let fileSink = try NIOWritePCAPHandler.SynchronizedFileSink.fileSinkWritingToFile(path: self.outputFile) {
error in
print("ERROR: \(error)")
exit(1)
}
defer {
try! fileSink.syncClose() // We want this to be included in the timing.
}
let channel = EmbeddedChannel()
defer {
_ = try! channel.finish()
}
let pcapHandler = NIOWritePCAPHandler(mode: .client,
fileSink: fileSink.write)
try channel.pipeline.addHandler(pcapHandler, position: .first).wait()
for _ in 0 ..< self.numberOfRepeats {
channel.writeAndFlush(self.byteBuffer, promise: nil)
_ = try channel.readOutbound(as: ByteBuffer.self)
}
return self.numberOfRepeats
}
}