View Javadoc
1   /*
2    * Copyright 2017 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.netty.handler.codec.http;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.ByteBufUtil;
20  import io.netty.buffer.Unpooled;
21  import io.netty.microbench.util.AbstractMicrobenchmark;
22  import io.netty.util.AsciiString;
23  import io.netty.util.CharsetUtil;
24  import org.openjdk.jmh.annotations.Benchmark;
25  import org.openjdk.jmh.annotations.Measurement;
26  import org.openjdk.jmh.annotations.Scope;
27  import org.openjdk.jmh.annotations.State;
28  import org.openjdk.jmh.annotations.Warmup;
29  
30  import static io.netty.handler.codec.http.HttpConstants.*;
31  
32  @State(Scope.Benchmark)
33  @Warmup(iterations = 10)
34  @Measurement(iterations = 20)
35  public class HttpRequestEncoderInsertBenchmark extends AbstractMicrobenchmark {
36  
37      private final String uri = "http://localhost?eventType=CRITICAL&from=0&to=1497437160327&limit=10&offset=0";
38      private final OldHttpRequestEncoder encoderOld = new OldHttpRequestEncoder();
39      private final HttpRequestEncoder encoderNew = new HttpRequestEncoder();
40  
41      @Benchmark
42      public ByteBuf oldEncoder() throws Exception {
43          ByteBuf buffer = Unpooled.buffer(100);
44          try {
45              encoderOld.encodeInitialLine(buffer, new DefaultHttpRequest(HttpVersion.HTTP_1_1,
46                      HttpMethod.GET, uri));
47              return buffer;
48          } finally {
49              buffer.release();
50          }
51      }
52  
53      @Benchmark
54      public ByteBuf newEncoder() throws Exception {
55          ByteBuf buffer = Unpooled.buffer(100);
56          try {
57              encoderNew.encodeInitialLine(buffer, new DefaultHttpRequest(HttpVersion.HTTP_1_1,
58                      HttpMethod.GET, uri));
59              return buffer;
60          } finally {
61              buffer.release();
62          }
63      }
64  
65      private static class OldHttpRequestEncoder extends HttpObjectEncoder<HttpRequest> {
66          private static final byte[] CRLF = {CR, LF};
67          private static final char SLASH = '/';
68          private static final char QUESTION_MARK = '?';
69  
70          @Override
71          public boolean acceptOutboundMessage(Object msg) throws Exception {
72              return super.acceptOutboundMessage(msg) && !(msg instanceof HttpResponse);
73          }
74  
75          @Override
76          protected void encodeInitialLine(ByteBuf buf, HttpRequest request) throws Exception {
77              AsciiString method = request.method().asciiName();
78              ByteBufUtil.copy(method, method.arrayOffset(), buf, method.length());
79              buf.writeByte(SP);
80  
81              // Add / as absolute path if no is present.
82              // See https://tools.ietf.org/html/rfc2616#section-5.1.2
83              String uri = request.uri();
84  
85              if (uri.isEmpty()) {
86                  uri += SLASH;
87              } else {
88                  int start = uri.indexOf("://");
89                  if (start != -1 && uri.charAt(0) != SLASH) {
90                      int startIndex = start + 3;
91                      // Correctly handle query params.
92                      // See https://github.com/netty/netty/issues/2732
93                      int index = uri.indexOf(QUESTION_MARK, startIndex);
94                      if (index == -1) {
95                          if (uri.lastIndexOf(SLASH) <= startIndex) {
96                              uri += SLASH;
97                          }
98                      } else {
99                          if (uri.lastIndexOf(SLASH, index) <= startIndex) {
100                             int len = uri.length();
101                             StringBuilder sb = new StringBuilder(len + 1);
102                             sb.append(uri, 0, index)
103                                     .append(SLASH)
104                                     .append(uri, index, len);
105                             uri = sb.toString();
106                         }
107                     }
108                 }
109             }
110 
111             buf.writeBytes(uri.getBytes(CharsetUtil.UTF_8));
112 
113             buf.writeByte(SP);
114             request.protocolVersion().encode(buf);
115             buf.writeBytes(CRLF);
116         }
117     }
118 }