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.Fork;
39  import org.openjdk.jmh.annotations.Level;
40  import org.openjdk.jmh.annotations.Measurement;
41  import org.openjdk.jmh.annotations.Mode;
42  import org.openjdk.jmh.annotations.OutputTimeUnit;
43  import org.openjdk.jmh.annotations.Param;
44  import org.openjdk.jmh.annotations.Scope;
45  import org.openjdk.jmh.annotations.Setup;
46  import org.openjdk.jmh.annotations.State;
47  import org.openjdk.jmh.annotations.TearDown;
48  import org.openjdk.jmh.annotations.Threads;
49  import org.openjdk.jmh.annotations.Warmup;
50  import org.openjdk.jmh.infra.Blackhole;
51  
52  import java.util.Iterator;
53  import java.util.Map;
54  import java.util.concurrent.TimeUnit;
55  
56  @Fork(1)
57  @Threads(1)
58  @State(Scope.Benchmark)
59  @Warmup(iterations = 5)
60  @Measurement(iterations = 5)
61  @OutputTimeUnit(TimeUnit.NANOSECONDS)
62  public class HpackEncoderBenchmark extends AbstractMicrobenchmark {
63  
64      @Param
65      public HpackHeadersSize size;
66  
67      @Param({ "true", "false" })
68      public boolean sensitive;
69  
70      @Param({ "true", "false" })
71      public boolean duplicates;
72  
73      @Param({ "true", "false" })
74      public boolean limitToAscii;
75  
76      private Http2Headers http2Headers;
77      private Buffer output;
78      private Http2HeadersEncoder.SensitivityDetector sensitivityDetector;
79  
80      @Setup(Level.Trial)
81      public void setup() {
82          http2Headers = HpackBenchmarkUtil.http2Headers(size, limitToAscii);
83          if (duplicates) {
84              int size = http2Headers.size();
85              if (size > 0) {
86                  Iterator<Map.Entry<CharSequence, CharSequence>> itr = http2Headers.iterator();
87                  Map.Entry<CharSequence, CharSequence> entry = itr.next();
88                  http2Headers.clear();
89                  for (int i = 0; i < size; ++i) {
90                      http2Headers.add(entry.getKey(), entry.getValue());
91                  }
92              }
93          }
94          output = size.newOutBuffer();
95          sensitivityDetector = sensitive ? Http2HeadersEncoder.ALWAYS_SENSITIVE : Http2HeadersEncoder.NEVER_SENSITIVE;
96      }
97  
98      @TearDown(Level.Trial)
99      public void tearDown() {
100         output.close();
101     }
102 
103     @Benchmark
104     @BenchmarkMode(Mode.AverageTime)
105     public void encode(Blackhole bh) throws Exception {
106         HpackEncoder hpackEncoder = HpackUtilBenchmark.newTestEncoder();
107         output.resetOffsets();
108         hpackEncoder.encodeHeaders(3 /*randomly chosen*/, output, http2Headers, sensitivityDetector);
109         bh.consume(output);
110     }
111 }