|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 AsyncHttpClient Project. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.asynchttpclient.bench; |
| 17 | + |
| 18 | +import io.netty.handler.codec.http.DefaultHttpHeaders; |
| 19 | +import io.netty.handler.codec.http.HttpHeaderNames; |
| 20 | +import io.netty.handler.codec.http.HttpHeaders; |
| 21 | +import io.netty.handler.codec.http2.DefaultHttp2Headers; |
| 22 | +import io.netty.handler.codec.http2.Http2Headers; |
| 23 | +import io.netty.util.AsciiString; |
| 24 | +import org.openjdk.jmh.annotations.Benchmark; |
| 25 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 26 | +import org.openjdk.jmh.annotations.Mode; |
| 27 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 28 | +import org.openjdk.jmh.annotations.Scope; |
| 29 | +import org.openjdk.jmh.annotations.Setup; |
| 30 | +import org.openjdk.jmh.annotations.State; |
| 31 | + |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.HashSet; |
| 34 | +import java.util.Iterator; |
| 35 | +import java.util.Locale; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.Set; |
| 38 | +import java.util.concurrent.TimeUnit; |
| 39 | + |
| 40 | +/** |
| 41 | + * Measures the per-request HTTP/2 header copy in {@code NettyRequestSender.sendHttp2Frames}: the |
| 42 | + * baseline (String-typed {@code forEach} + {@code toLowerCase()} + {@code HashSet} skip-set) versus |
| 43 | + * {@link HttpHeaders#iteratorCharSequence()} + {@link AsciiString#contentEqualsIgnoreCase}. Both still |
| 44 | + * lowercase forwarded names (Netty's validating {@link DefaultHttp2Headers} rejects uppercase), so the |
| 45 | + * header set includes mixed-case names to exercise that path. |
| 46 | + */ |
| 47 | +@State(Scope.Thread) |
| 48 | +@BenchmarkMode(Mode.AverageTime) |
| 49 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 50 | +public class Http2HeaderConversionBenchmark { |
| 51 | + |
| 52 | + private static final Set<String> EXCLUDED_STRING = new HashSet<>(Arrays.asList( |
| 53 | + "connection", "keep-alive", "proxy-connection", "transfer-encoding", "upgrade", "host")); |
| 54 | + |
| 55 | + private HttpHeaders headers; |
| 56 | + |
| 57 | + @Setup |
| 58 | + public void setup() { |
| 59 | + // Representative request header set built the way NettyRequestFactory builds it: |
| 60 | + // names are AsciiString constants from HttpHeaderNames. |
| 61 | + headers = new DefaultHttpHeaders(false); |
| 62 | + headers.set(HttpHeaderNames.HOST, "www.example.com"); |
| 63 | + headers.set(HttpHeaderNames.USER_AGENT, "AHC/3.0"); |
| 64 | + headers.set(HttpHeaderNames.ACCEPT, "*/*"); |
| 65 | + headers.set(HttpHeaderNames.ACCEPT_ENCODING, "gzip, deflate"); |
| 66 | + headers.set(HttpHeaderNames.CONTENT_TYPE, "application/json; charset=utf-8"); |
| 67 | + headers.set(HttpHeaderNames.CONTENT_LENGTH, "256"); |
| 68 | + headers.set(HttpHeaderNames.AUTHORIZATION, "Bearer abcdef0123456789"); |
| 69 | + headers.set(HttpHeaderNames.COOKIE, "session=deadbeef; theme=dark"); |
| 70 | + headers.set(HttpHeaderNames.CONNECTION, "keep-alive"); |
| 71 | + headers.set(HttpHeaderNames.CACHE_CONTROL, "no-cache"); |
| 72 | + // Mixed-case user-supplied names (stored as String, original casing preserved) — these are the |
| 73 | + // names the proposed path must lowercase, since validating DefaultHttp2Headers rejects uppercase. |
| 74 | + headers.add("X-Request-ID", "abc-123-def-456"); |
| 75 | + headers.add("X-Custom-Header", "some-custom-value"); |
| 76 | + } |
| 77 | + |
| 78 | + /** Exact reproduction of production NettyRequestSender.sendHttp2Frames header loop. */ |
| 79 | + @Benchmark |
| 80 | + public Http2Headers baseline_forEach_toLowerCase() { |
| 81 | + Http2Headers h2 = new DefaultHttp2Headers() |
| 82 | + .method("GET").path("/path?q=1").scheme("https").authority("www.example.com"); |
| 83 | + for (Map.Entry<String, String> entry : headers) { |
| 84 | + String name = entry.getKey().toLowerCase(); |
| 85 | + if (!EXCLUDED_STRING.contains(name)) { |
| 86 | + h2.add(name, entry.getValue()); |
| 87 | + } |
| 88 | + } |
| 89 | + return h2; |
| 90 | + } |
| 91 | + |
| 92 | + /** Proposed: iteratorCharSequence + AsciiString-keyed case-insensitive skip set. */ |
| 93 | + @Benchmark |
| 94 | + public Http2Headers proposed_charSequence_ascii() { |
| 95 | + Http2Headers h2 = new DefaultHttp2Headers() |
| 96 | + .method("GET").path("/path?q=1").scheme("https").authority("www.example.com"); |
| 97 | + Iterator<Map.Entry<CharSequence, CharSequence>> it = headers.iteratorCharSequence(); |
| 98 | + while (it.hasNext()) { |
| 99 | + Map.Entry<CharSequence, CharSequence> entry = it.next(); |
| 100 | + CharSequence name = entry.getKey(); |
| 101 | + if (!containsIgnoreCase(name)) { |
| 102 | + h2.add(toLowerCaseName(name), entry.getValue()); |
| 103 | + } |
| 104 | + } |
| 105 | + return h2; |
| 106 | + } |
| 107 | + |
| 108 | + /** Mirrors production NettyRequestSender.toLowerCaseHeaderName — allocation-free when already lowercase. */ |
| 109 | + private static CharSequence toLowerCaseName(CharSequence name) { |
| 110 | + if (name instanceof AsciiString) { |
| 111 | + return ((AsciiString) name).toLowerCase(); |
| 112 | + } |
| 113 | + return name.toString().toLowerCase(Locale.ROOT); |
| 114 | + } |
| 115 | + |
| 116 | + private static boolean containsIgnoreCase(CharSequence name) { |
| 117 | + // Direct constant comparison: HTTP/2 forbids exactly these 6 connection-specific names. |
| 118 | + // AsciiString.contentEqualsIgnoreCase short-circuits on length mismatch (cheap). |
| 119 | + return HttpHeaderNames.CONNECTION.contentEqualsIgnoreCase(name) |
| 120 | + || HttpHeaderNames.HOST.contentEqualsIgnoreCase(name) |
| 121 | + || HttpHeaderNames.TRANSFER_ENCODING.contentEqualsIgnoreCase(name) |
| 122 | + || HttpHeaderNames.UPGRADE.contentEqualsIgnoreCase(name) |
| 123 | + || HttpHeaderNames.KEEP_ALIVE.contentEqualsIgnoreCase(name) |
| 124 | + || HttpHeaderNames.PROXY_CONNECTION.contentEqualsIgnoreCase(name); |
| 125 | + } |
| 126 | +} |
0 commit comments