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 java.util.HashMap;
35  import java.util.List;
36  import java.util.Map;
37  
38  /**
39   * Utility methods for hpack tests.
40   */
41  public final class HpackBenchmarkUtil {
42      private HpackBenchmarkUtil() {
43      }
44  
45      /**
46       * Internal key used to index a particular set of headers in the map.
47       */
48      private static class HeadersKey {
49          final HpackHeadersSize size;
50          final boolean limitToAscii;
51  
52          HeadersKey(HpackHeadersSize size, boolean limitToAscii) {
53              this.size = size;
54              this.limitToAscii = limitToAscii;
55          }
56  
57          List<HpackHeader> newHeaders() {
58              return size.newHeaders(limitToAscii);
59          }
60  
61          @Override
62          public boolean equals(Object o) {
63              if (this == o) {
64                  return true;
65              }
66              if (o == null || getClass() != o.getClass()) {
67                  return false;
68              }
69  
70              HeadersKey that = (HeadersKey) o;
71  
72              if (limitToAscii != that.limitToAscii) {
73                  return false;
74              }
75              return size == that.size;
76          }
77  
78          @Override
79          public int hashCode() {
80              int result = size.hashCode();
81              result = 31 * result + (limitToAscii ? 1 : 0);
82              return result;
83          }
84      }
85  
86      private static final Map<HeadersKey, List<HpackHeader>> headersMap;
87  
88      static {
89          HpackHeadersSize[] sizes = HpackHeadersSize.values();
90          headersMap = new HashMap<>(sizes.length * 2);
91          for (HpackHeadersSize size : sizes) {
92              HeadersKey key = new HeadersKey(size, true);
93              headersMap.put(key, key.newHeaders());
94  
95              key = new HeadersKey(size, false);
96              headersMap.put(key, key.newHeaders());
97          }
98      }
99  
100     /**
101      * Gets headers for the given size and whether the key/values should be limited to ASCII.
102      */
103     static List<HpackHeader> headers(HpackHeadersSize size, boolean limitToAscii) {
104         return headersMap.get(new HeadersKey(size, limitToAscii));
105     }
106 
107     static Http2Headers http2Headers(HpackHeadersSize size, boolean limitToAscii) {
108         List<HpackHeader> hpackHeaders = headersMap.get(new HeadersKey(size, limitToAscii));
109         Http2Headers http2Headers = new DefaultHttp2Headers(false);
110         for (int i = 0; i < hpackHeaders.size(); ++i) {
111             HpackHeader hpackHeader = hpackHeaders.get(i);
112             http2Headers.add(hpackHeader.name, hpackHeader.value);
113         }
114         return http2Headers;
115     }
116 }