mirror of
https://github.com/apple/swift-nio-http2
synced 2026-11-29 05:24:22 +00:00
HPACKHeaders performance test for HTTPHeader normalisation (#346)
Motivation: `HPACKHeaders` has an `init` which will normalize `HTTPHeaders`, this includes removing any headers which are illegal in HTTP2 and any headers which present as a 'connection' header value. As written this is expensive in some edges cases. With perf tests we can validate that implementation changes improve performance. Modifications: Add a performance test to normalize `HTTPHeaders`. This is run on two configurations. Result: More insight into header normalisation perf.
This commit is contained in:
1 parent
c13f959b10
commit
ed25fecd40
2 files changed
+92
No files matched your search
+90
@@ -0,0 +1,90 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 ..< self.iterations {
|
||||
let normalize = HPACKHeaders(httpHeaders: self.httpHeaders, normalizeHTTPHeaders: true)
|
||||
count &+= normalize.count
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
}
|
||||
|
||||
extension HTTPHeaders {
|
||||
static func manyUniqueConnectionHeaderValues(_ count: Int) -> HTTPHeaders {
|
||||
var httpHeaders: HTTPHeaders = [:]
|
||||
httpHeaders.reserveCapacity(count + 1)
|
||||
|
||||
let connectionHeaderValue = (0 ..< count).map(String.init).joined(separator: ",")
|
||||
httpHeaders.add(name: "connection", value: connectionHeaderValue)
|
||||
|
||||
for i in 0 ..< count {
|
||||
let header = String(describing: i)
|
||||
httpHeaders.add(name: header, value: header)
|
||||
}
|
||||
|
||||
return httpHeaders
|
||||
}
|
||||
|
||||
static func manyConnectionHeaderValuesWhichAreNotRemoved(_ count: Int) -> 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 ..< count).map { _ in "a" }.joined(separator: ",")
|
||||
httpHeaders.add(name: "connection", value: connectionHeaderValue)
|
||||
|
||||
for _ in 0 ..< count {
|
||||
httpHeaders.add(name: "b", value: "")
|
||||
}
|
||||
|
||||
return httpHeaders
|
||||
}
|
||||
}
|
||||
@@ -92,6 +92,8 @@ try measureAndPrint(desc: "hpackheaders_canonical_form", benchmark: HPACKHeaderC
|
||||
try measureAndPrint(desc: "hpackheaders_canonical_form_trimming_whitespace", benchmark: HPACKHeaderCanonicalFormBenchmark(.trimmingWhitespace))
|
||||
try measureAndPrint(desc: "hpackheaders_canonical_form_trimming_whitespace_short_strings", benchmark: HPACKHeaderCanonicalFormBenchmark(.trimmingWhitespaceFromShortStrings))
|
||||
try measureAndPrint(desc: "hpackheaders_canonical_form_trimming_whitespace_long_strings", benchmark: HPACKHeaderCanonicalFormBenchmark(.trimmingWhitespaceFromLongStrings))
|
||||
try measureAndPrint(desc: "hpackheaders_normalize_httpheaders_removing_10k_conn_headers", benchmark: HPACKHeadersNormalizationOfHTTPHeadersBenchmark(headersKind: .manyUniqueConnectionHeaderValues(10_000), iterations: 10))
|
||||
try measureAndPrint(desc: "hpackheaders_normalize_httpheaders_keeping_10k_conn_headers", benchmark: HPACKHeadersNormalizationOfHTTPHeadersBenchmark(headersKind: .manyConnectionHeaderValuesWhichAreNotRemoved(10_000), iterations: 10))
|
||||
try measureAndPrint(desc: "huffman_encode_basic", benchmark: HuffmanEncodingBenchmark(huffmanString: .basicHuffmanString, loopCount: 100))
|
||||
try measureAndPrint(desc: "huffman_encode_complex", benchmark: HuffmanEncodingBenchmark(huffmanString: .complexHuffmanString, loopCount: 100))
|
||||
try measureAndPrint(desc: "huffman_decode_basic", benchmark: HuffmanDecodingBenchmark(huffmanBytes: .basicHuffmanBytes, loopCount: 25))
|
||||
|
||||
Reference in new issue
Block a user