|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftNIO open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2026 Apple Inc. and the SwiftNIO project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftNIO project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import Benchmark |
| 16 | +import HTTPTypes |
| 17 | +@_spi(Benchmarks) import NIOHTTP3 |
| 18 | +import NIOQUICHelpers |
| 19 | + |
| 20 | +/// A realistic request header set for a browser GET, exercising a mix of exact |
| 21 | +/// static-table matches (`:method GET`), name-only matches (`:authority`) and |
| 22 | +/// headers absent from the table (`x-custom-header`). |
| 23 | +private let requestHeaders: [HTTPField] = [ |
| 24 | + HTTPField(name: .init(parsed: ":method")!, value: "GET"), |
| 25 | + HTTPField(name: .init(parsed: ":scheme")!, value: "https"), |
| 26 | + HTTPField(name: .init(parsed: ":authority")!, value: "www.example.com"), |
| 27 | + HTTPField(name: .init(parsed: ":path")!, value: "/index.html"), |
| 28 | + HTTPField(name: .init(parsed: "user-agent")!, value: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)"), |
| 29 | + HTTPField(name: .init(parsed: "accept")!, value: "*/*"), |
| 30 | + HTTPField(name: .init(parsed: "accept-encoding")!, value: "gzip, deflate, br"), |
| 31 | + HTTPField(name: .init(parsed: "accept-language")!, value: "en-US,en;q=0.9"), |
| 32 | + HTTPField(name: .init(parsed: "cache-control")!, value: "no-cache"), |
| 33 | + HTTPField(name: .init(parsed: "cookie")!, value: "session=abc123def456; theme=dark"), |
| 34 | + HTTPField(name: .init(parsed: "referer")!, value: "https://www.example.com/"), |
| 35 | + HTTPField(name: .init(parsed: "x-custom-header")!, value: "some-custom-value"), |
| 36 | +] |
| 37 | + |
| 38 | +let benchmarks: @Sendable () -> Void = { |
| 39 | + // Exercises StaticHeaderTable.find on every header, with no dynamic table. |
| 40 | + Benchmark( |
| 41 | + "QPACKStaticEncode_BrowserGET", |
| 42 | + configuration: .init( |
| 43 | + metrics: [.mallocCountTotal, .instructions, .wallClock], |
| 44 | + scalingFactor: .kilo |
| 45 | + ) |
| 46 | + ) { benchmark in |
| 47 | + for _ in benchmark.scaledIterations { |
| 48 | + blackHole(QPACKBenchmarks.staticEncode(headers: requestHeaders)) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // Exercises the static-table lookup plus the dynamic table on every header. |
| 53 | + Benchmark( |
| 54 | + "QPACKDynamicEncode_BrowserGET", |
| 55 | + configuration: .init( |
| 56 | + metrics: [.mallocCountTotal, .instructions, .wallClock], |
| 57 | + scalingFactor: .kilo |
| 58 | + ) |
| 59 | + ) { benchmark in |
| 60 | + for _ in benchmark.scaledIterations { |
| 61 | + blackHole( |
| 62 | + QPACKBenchmarks.dynamicEncode( |
| 63 | + headers: requestHeaders, |
| 64 | + streamID: QUICStreamID(rawValue: 0), |
| 65 | + dynamicTableMaxCapacity: 4096, |
| 66 | + dynamicTableInitialCapacity: 4096, |
| 67 | + maxBlockedStreams: 100, |
| 68 | + targetEvictableFraction: 0.5 |
| 69 | + ) |
| 70 | + ) |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments