View Javadoc
1   /*
2    * Copyright 2015 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  
17  /*
18   * Copyright 2015 Twitter, Inc.
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *     https://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package io.netty5.handler.codec.http2;
33  
34  import io.netty5.buffer.api.Buffer;
35  import io.netty5.microbench.util.AbstractMicrobenchmark;
36  import org.openjdk.jmh.annotations.Benchmark;
37  import org.openjdk.jmh.annotations.BenchmarkMode;
38  import org.openjdk.jmh.annotations.Level;
39  import org.openjdk.jmh.annotations.Mode;
40  import org.openjdk.jmh.annotations.Param;
41  import org.openjdk.jmh.annotations.Setup;
42  import org.openjdk.jmh.annotations.TearDown;
43  import org.openjdk.jmh.infra.Blackhole;
44  
45  import static io.netty5.buffer.api.DefaultBufferAllocators.onHeapAllocator;
46  import static io.netty5.handler.codec.http2.HpackBenchmarkUtil.http2Headers;
47  
48  public class HpackDecoderBenchmark extends AbstractMicrobenchmark {
49  
50      @Param
51      public HpackHeadersSize size;
52  
53      @Param({ "true", "false" })
54      public boolean sensitive;
55  
56      @Param({ "true", "false" })
57      public boolean limitToAscii;
58  
59      private Buffer input;
60  
61      @Setup(Level.Trial)
62      public void setup() throws Http2Exception {
63          input = onHeapAllocator().copyOf(getSerializedHeaders(http2Headers(size, limitToAscii), sensitive))
64                                   .makeReadOnly();
65      }
66  
67      @TearDown(Level.Trial)
68      public void teardown() {
69          input.close();
70      }
71  
72      @Benchmark
73      @BenchmarkMode(Mode.Throughput)
74      public void decode(final Blackhole bh) throws Http2Exception {
75          HpackDecoder hpackDecoder = new HpackDecoder(Integer.MAX_VALUE);
76          Http2Headers headers = new DefaultHttp2Headers() {
77              @Override
78              public Http2Headers add(CharSequence name, CharSequence value) {
79                  bh.consume(sensitive);
80                  return this;
81              }
82          };
83          hpackDecoder.decode(0, input.copy(true), headers, true);
84      }
85  
86      private byte[] getSerializedHeaders(Http2Headers headers, boolean sensitive) throws Http2Exception {
87          HpackEncoder hpackEncoder = HpackUtilBenchmark.newTestEncoder();
88          try (Buffer out = size.newOutBuffer()) {
89              hpackEncoder.encodeHeaders(3 /* randomly chosen */, out, headers,
90                                    sensitive ? Http2HeadersEncoder.ALWAYS_SENSITIVE
91                                              : Http2HeadersEncoder.NEVER_SENSITIVE);
92              byte[] bytes = new byte[out.readableBytes()];
93              out.readBytes(bytes, 0, bytes.length);
94              return bytes;
95          }
96      }
97  }