Repository: apple/swift-nio
File: Sources/NIOPerformanceTester/TCPThroughputBenchmark.swift (Swift)

Code before:
    private var message: ByteBuffer!
    private var isDonePromise: EventLoopPromise<Void>!

    final class ServerHandler: ChannelInboundHandler {
        public typealias InboundIn = ByteBuffer
        public typealias OutboundOut = ByteBuffer

        private var channel: Channel!

        public func channelActive(context: ChannelHandlerContext) {
            self.channel = context.channel
        }

        public func send(_ message: ByteBuffer, times count: Int) {
            for _ in 0..<count {
                _ = self.channel.writeAndFlush(message.slice())
            }
        }
    }

    final class StreamDecoder: ByteToMessageDecoder {
        public typealias InboundIn = ByteBuffer
        public typealias InboundOut = ByteBuffer

        public func decode(context: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState {
            if buffer.readableBytes >= MemoryLayout<UInt16>.size {
                let messageSize = buffer.getInteger(at: buffer.readerIndex, as: UInt16.self)!
                if buffer.readableBytes >= messageSize {
                    context.fireChannelRead(self.wrapInboundOut(buffer.readSlice(length: Int(messageSize))!))
                    return .continue
                }
            }
            return .needMoreData
        }
    }

    final class ClientHandler: ChannelInboundHandler {
        public typealias InboundIn = ByteBuffer
        public typealias OutboundOut = ByteBuffer

        private let benchmark: TCPThroughputBenchmark
        private var messagesReceived: Int

        init(_ benchmark: TCPThroughputBenchmark) {
            self.benchmark = benchmark
            self.messagesReceived = 0
        }

        public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
            self.messagesReceived += 1
            if (self.benchmark.messages == self.messagesReceived) {

Review comment (suggestion):
The idiomatic way to spell this would be to use conditional binding:

```swift
if let messageSize = buffer.getInteger(at: buffer.readerIndex, as: UInt16.self) {
```

Code after:
/// Server send 100K messages to the client,
/// measure the time from the very first message sent by the server
/// to the last message received by the client.

final class TCPThroughputBenchmark: Benchmark {

    private let messages: Int
    private let messageSize: Int

    private var group: EventLoopGroup!
    private var serverChannel: Channel!
    private var serverHandler: ServerHandler!
    private var clientChannel: Channel!

    private var message: ByteBuffer!
    private var isDonePromise: EventLoopPromise<Void>!

    final class ServerHandler: ChannelInboundHandler {
        public typealias InboundIn = ByteBuffer
        public typealias OutboundOut = ByteBuffer

        private var channel: Channel!

        public func channelActive(context: ChannelHandlerContext) {
            self.channel = context.channel
        }

        public func send(_ message: ByteBuffer, times count: Int) {
            for _ in 0..<count {
                _ = self.channel.writeAndFlush(message.slice())
            }
        }
    }

    final class StreamDecoder: ByteToMessageDecoder {
        public typealias InboundIn = ByteBuffer
        public typealias InboundOut = ByteBuffer

        public func decode(context: ChannelHandlerContext, buffer: inout ByteBuffer) throws -> DecodingState {
            if let messageSize = buffer.getInteger(at: buffer.readerIndex, as: UInt16.self) {
                if buffer.readableBytes >= messageSize {
                    context.fireChannelRead(self.wrapInboundOut(buffer.readSlice(length: Int(messageSize))!))
                    return .continue
                }
            }
            return .needMoreData
        }
    }

    final class ClientHandler: ChannelInboundHandler {
        public typealias InboundIn = ByteBuffer