View Javadoc
1   /*
2    * Copyright 2016 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.netty5.handler.codec.http2;
17  
18  import io.netty5.buffer.api.Buffer;
19  import io.netty5.microbench.util.AbstractMicrobenchmark;
20  import io.netty5.util.AsciiString;
21  import io.netty5.util.internal.ConstantTimeUtils;
22  import io.netty5.util.internal.PlatformDependent;
23  import org.openjdk.jmh.annotations.Benchmark;
24  import org.openjdk.jmh.annotations.Level;
25  import org.openjdk.jmh.annotations.Measurement;
26  import org.openjdk.jmh.annotations.Param;
27  import org.openjdk.jmh.annotations.Setup;
28  import org.openjdk.jmh.annotations.Threads;
29  import org.openjdk.jmh.annotations.Warmup;
30  
31  import java.util.List;
32  
33  import static io.netty5.buffer.api.DefaultBufferAllocators.onHeapAllocator;
34  import static io.netty5.handler.codec.http2.Http2CodecUtil.MAX_HEADER_LIST_SIZE;
35  import static io.netty5.handler.codec.http2.Http2CodecUtil.MAX_HEADER_TABLE_SIZE;
36  
37  @Threads(1)
38  @Warmup(iterations = 5)
39  @Measurement(iterations = 5)
40  public class HpackUtilBenchmark extends AbstractMicrobenchmark {
41      @Param
42      public HpackHeadersSize size;
43  
44      private List<HpackHeader> hpackHeaders;
45  
46      @Setup(Level.Trial)
47      public void setup() {
48          hpackHeaders = HpackBenchmarkUtil.headers(size, false);
49      }
50  
51      @Benchmark
52      public int oldEquals() {
53          int count = 0;
54          for (int i = 0; i < hpackHeaders.size(); ++i) {
55              HpackHeader hpackHeader = hpackHeaders.get(i);
56              if (oldEquals(hpackHeader.name, hpackHeader.name)) {
57                  ++count;
58              }
59          }
60          return count;
61      }
62  
63      @Benchmark
64      public int newEquals() {
65          int count = 0;
66          for (int i = 0; i < hpackHeaders.size(); ++i) {
67              HpackHeader hpackHeader = hpackHeaders.get(i);
68              if (newEquals(hpackHeader.name, hpackHeader.name)) {
69                  ++count;
70              }
71          }
72          return count;
73      }
74  
75      private static boolean oldEquals(CharSequence s1, CharSequence s2) {
76          if (s1.length() != s2.length()) {
77              return false;
78          }
79          char c = 0;
80          for (int i = 0; i < s1.length(); i++) {
81              c |= s1.charAt(i) ^ s2.charAt(i);
82          }
83          return c == 0;
84      }
85  
86      private static boolean newEquals(CharSequence s1, CharSequence s2) {
87          if (s1 instanceof AsciiString && s2 instanceof AsciiString) {
88              if (s1.length() != s2.length()) {
89                  return false;
90              }
91              AsciiString s1Ascii = (AsciiString) s1;
92              AsciiString s2Ascii = (AsciiString) s2;
93              return PlatformDependent.equalsConstantTime(s1Ascii.array(), s1Ascii.arrayOffset(),
94                                                          s2Ascii.array(), s2Ascii.arrayOffset(), s1.length()) != 0;
95          }
96  
97          return ConstantTimeUtils.equalsConstantTime(s1, s2) != 0;
98      }
99  
100     static HpackEncoder newTestEncoder() {
101         HpackEncoder hpackEncoder = new HpackEncoder();
102         try (Buffer buf = onHeapAllocator().allocate(256)) {
103             hpackEncoder.setMaxHeaderTableSize(buf, MAX_HEADER_TABLE_SIZE);
104             hpackEncoder.setMaxHeaderListSize(MAX_HEADER_LIST_SIZE);
105         } catch (Http2Exception e) {
106             throw new Error("max size not allowed?", e);
107         }
108         return hpackEncoder;
109     }
110 }