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.netty.handler.codec.http2;
33  
34  import io.netty.buffer.ByteBuf;
35  import io.netty.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.netty.buffer.Unpooled.wrappedBuffer;
46  import static io.netty.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 ByteBuf input;
60  
61      @Setup(Level.Trial)
62      public void setup() throws Http2Exception {
63          input = wrappedBuffer(getSerializedHeaders(http2Headers(size, limitToAscii), sensitive));
64      }
65  
66      @TearDown(Level.Trial)
67      public void teardown() {
68          input.release();
69      }
70  
71      @Benchmark
72      @BenchmarkMode(Mode.Throughput)
73      public void decode(final Blackhole bh) throws Http2Exception {
74          HpackDecoder hpackDecoder = new HpackDecoder(Integer.MAX_VALUE);
75          @SuppressWarnings("unchecked")
76          Http2Headers headers =
77                  new DefaultHttp2Headers() {
78              @Override
79              public Http2Headers add(CharSequence name, CharSequence value) {
80                  bh.consume(sensitive);
81                  return this;
82              }
83          };
84          hpackDecoder.decode(0, input.duplicate(), headers, true);
85      }
86  
87      private byte[] getSerializedHeaders(Http2Headers headers, boolean sensitive) throws Http2Exception {
88          HpackEncoder hpackEncoder = HpackUtilBenchmark.newTestEncoder();
89          ByteBuf out = size.newOutBuffer();
90          try {
91              hpackEncoder.encodeHeaders(3 /* randomly chosen */, out, headers,
92                                    sensitive ? Http2HeadersEncoder.ALWAYS_SENSITIVE
93                                              : Http2HeadersEncoder.NEVER_SENSITIVE);
94              byte[] bytes = new byte[out.readableBytes()];
95              out.readBytes(bytes);
96              return bytes;
97          } finally {
98              out.release();
99          }
100     }
101 }