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 STATUS = new AsciiString(":status".getBytes(CharsetUtil.US_ASCII), false);
47      private static final CharSequence STATUS_200 = new AsciiString("200".getBytes(CharsetUtil.US_ASCII), false);
48      private static final CharSequence STATUS_500 = new AsciiString("500".getBytes(CharsetUtil.US_ASCII), false);
49      private static final CharSequence AUTHORITY =
50              new AsciiString(":authority".getBytes(CharsetUtil.US_ASCII), false);
51      private static final CharSequence AUTHORITY_NETTY =
52              new AsciiString("netty.io".getBytes(CharsetUtil.US_ASCII), false);
53      private static final CharSequence USER_AGENT =
54              new AsciiString("user-agent".getBytes(CharsetUtil.US_ASCII), false);
55      private static final CharSequence USER_AGENT_CURL =
56              new AsciiString("curl/7.64.1".getBytes(CharsetUtil.US_ASCII), false);
57  
58      @Benchmark
59      @BenchmarkMode(Mode.AverageTime)
60      public int lookupNoNameMatch() {
61          return HpackStaticTable.getIndexInsensitive(X_CONTENT_ENCODING, X_GZIP);
62      }
63  
64      @Benchmark
65      @BenchmarkMode(Mode.AverageTime)
66      public int lookupNameAndValueMatchFirst() {
67          return HpackStaticTable.getIndexInsensitive(STATUS, STATUS_200);
68      }
69  
70      @Benchmark
71      @BenchmarkMode(Mode.AverageTime)
72      public int lookupNameAndValueMatchLast() {
73          return HpackStaticTable.getIndexInsensitive(STATUS, STATUS_500);
74      }
75  
76      @Benchmark
77      @BenchmarkMode(Mode.AverageTime)
78      public int lookupNameOnlyMatchBeginTable() {
79          return HpackStaticTable.getIndexInsensitive(AUTHORITY, AUTHORITY_NETTY);
80      }
81  
82      @Benchmark
83      @BenchmarkMode(Mode.AverageTime)
84      public int lookupNameOnlyMatchEndTable() {
85          return HpackStaticTable.getIndexInsensitive(USER_AGENT, USER_AGENT_CURL);
86      }
87  }