//===----------------------------------------------------------------------===// // // This source file is part of the SwiftNIO open source project // // Copyright (c) 2022 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 NIOCore import NIOHPACK import NIOHTTP1 final class HPACKHeadersNormalizationOfHTTPHeadersBenchmark { private let httpHeadersKind: HeadersKind private var httpHeaders: HTTPHeaders! private let iterations: Int enum HeadersKind { case manyUniqueConnectionHeaderValues(Int) case manyConnectionHeaderValuesWhichAreNotRemoved(Int) } init(headersKind: HeadersKind, iterations: Int) { self.httpHeadersKind = headersKind self.iterations = iterations } } extension HPACKHeadersNormalizationOfHTTPHeadersBenchmark: Benchmark { func setUp() throws { switch self.httpHeadersKind { case .manyUniqueConnectionHeaderValues(let count): self.httpHeaders = .manyUniqueConnectionHeaderValues(count) case .manyConnectionHeaderValuesWhichAreNotRemoved(let count): self.httpHeaders = .manyConnectionHeaderValuesWhichAreNotRemoved(count) } } func tearDown() {} func run() throws -> Int { var count = 0 for _ in 0.. HTTPHeaders { var httpHeaders: HTTPHeaders = [:] httpHeaders.reserveCapacity(count + 1) let connectionHeaderValue = (0.. HTTPHeaders { var httpHeaders: HTTPHeaders = [:] httpHeaders.reserveCapacity(count + 1) // Assuming connection header values are decomposed into an array then this is the // worst case: the whole array must be scanned for each of the `count` headers we add below. let connectionHeaderValue = (0..