View Javadoc
1   /*
2    * Copyright 2020 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.http2;
17  
18  import io.netty.microbench.util.AbstractMicrobenchmark;
19  import io.netty.util.CharsetUtil;
20  import org.openjdk.jmh.annotations.Benchmark;
21  import org.openjdk.jmh.annotations.BenchmarkMode;
22  import org.openjdk.jmh.annotations.Fork;
23  import org.openjdk.jmh.annotations.Measurement;
24  import org.openjdk.jmh.annotations.Mode;
25  import org.openjdk.jmh.annotations.OutputTimeUnit;
26  import org.openjdk.jmh.annotations.Scope;
27  import org.openjdk.jmh.annotations.State;
28  import org.openjdk.jmh.annotations.Threads;
29  import org.openjdk.jmh.annotations.Warmup;
30  
31  import java.util.concurrent.TimeUnit;
32  
33  import io.netty.util.AsciiString;
34  
35  @Fork(1)
36  @Threads(1)
37  @State(Scope.Benchmark)
38  @Warmup(iterations = 5)
39  @Measurement(iterations = 5)
40  @OutputTimeUnit(TimeUnit.NANOSECONDS)
41  public class HpackStaticTableBenchmark extends AbstractMicrobenchmark {
42  
43      private static final CharSequence X_CONTENT_ENCODING =
44              new AsciiString("x-content-encoding".getBytes(CharsetUtil.US_ASCII), false);
45      private static final CharSequence X_GZIP = new AsciiString("x-gzip".getBytes(CharsetUtil.US_ASCII), false);
46      private static final CharSequence SCHEME = new AsciiString(":scheme".getBytes(CharsetUtil.US_ASCII), false);
47      private static final CharSequence HTTP = new AsciiString("http".getBytes(CharsetUtil.US_ASCII), false);
48      private static final CharSequence HTTPS = new AsciiString("https".getBytes(CharsetUtil.US_ASCII), false);
49      private static final CharSequence STATUS = new AsciiString(":status".getBytes(CharsetUtil.US_ASCII), false);
50      private static final CharSequence STATUS_200 = new AsciiString("200".getBytes(CharsetUtil.US_ASCII), false);
51      private static final CharSequence STATUS_500 = new AsciiString("500".getBytes(CharsetUtil.US_ASCII), false);
52      private static final CharSequence AUTHORITY =
53              new AsciiString(":authority".getBytes(CharsetUtil.US_ASCII), false);
54      private static final CharSequence AUTHORITY_NETTY =
55              new AsciiString("netty.io".getBytes(CharsetUtil.US_ASCII), false);
56      private static final CharSequence USER_AGENT =
57              new AsciiString("user-agent".getBytes(CharsetUtil.US_ASCII), false);
58      private static final CharSequence USER_AGENT_CURL =
59              new AsciiString("curl/7.64.1".getBytes(CharsetUtil.US_ASCII), false);
60  
61      @Benchmark
62      @BenchmarkMode(Mode.AverageTime)
63      public int lookupNoNameMatch() {
64          return HpackStaticTable.getIndexInsensitive(X_CONTENT_ENCODING, X_GZIP);
65      }
66  
67      @Benchmark
68      @BenchmarkMode(Mode.AverageTime)
69      public int lookupNameAndValueMatchFirst() {
70          return HpackStaticTable.getIndexInsensitive(STATUS, STATUS_200);
71      }
72  
73      @Benchmark
74      @BenchmarkMode(Mode.AverageTime)
75      public int lookupNameAndValueMatchLast() {
76          return HpackStaticTable.getIndexInsensitive(STATUS, STATUS_500);
77      }
78  
79      @Benchmark
80      @BenchmarkMode(Mode.AverageTime)
81      public int lookupNameOnlyMatchBeginTable() {
82          return HpackStaticTable.getIndexInsensitive(AUTHORITY, AUTHORITY_NETTY);
83      }
84  
85      @Benchmark
86      @BenchmarkMode(Mode.AverageTime)
87      public int lookupHttp() {
88          return HpackStaticTable.getIndexInsensitive(SCHEME, HTTP);
89      }
90  
91      @Benchmark
92      @BenchmarkMode(Mode.AverageTime)
93      public int lookupHttps() {
94          return HpackStaticTable.getIndexInsensitive(SCHEME, HTTPS);
95      }
96  
97      @Benchmark
98      @BenchmarkMode(Mode.AverageTime)
99      public int lookupNameOnlyMatchEndTable() {
100         return HpackStaticTable.getIndexInsensitive(USER_AGENT, USER_AGENT_CURL);
101     }
102 }