View Javadoc
1   /*
2    * Copyright 2024 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.handler.codec.http.multipart.HttpPostRequestDecoder;
22  import io.netty.handler.codec.http.multipart.InterfaceHttpData;
23  import io.netty.microbench.util.AbstractMicrobenchmark;
24  import io.netty.util.internal.PlatformDependent;
25  import org.openjdk.jmh.annotations.Benchmark;
26  import org.openjdk.jmh.annotations.Measurement;
27  import org.openjdk.jmh.annotations.OutputTimeUnit;
28  import org.openjdk.jmh.annotations.Param;
29  import org.openjdk.jmh.annotations.Scope;
30  import org.openjdk.jmh.annotations.Setup;
31  import org.openjdk.jmh.annotations.State;
32  import org.openjdk.jmh.annotations.Threads;
33  import org.openjdk.jmh.annotations.Warmup;
34  
35  import java.util.List;
36  import java.util.Random;
37  import java.util.concurrent.TimeUnit;
38  
39  @Threads(1)
40  @Warmup(iterations = 3)
41  @Measurement(iterations = 3)
42  @OutputTimeUnit(TimeUnit.MILLISECONDS)
43  @State(Scope.Benchmark)
44  public class HttpPostDecoderBenchmark extends AbstractMicrobenchmark {
45      @Param({ "false", "true" })
46      public boolean direct;
47  
48      private ByteBuf buf;
49      private HttpRequest request;
50  
51      @Setup()
52      public void setUp() {
53          request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/post");
54          request.headers().add(HttpHeaderNames.CONTENT_TYPE, "application/x-www-form-urlencoded");
55          buf = direct ? Unpooled.directBuffer() : Unpooled.buffer();
56          for (int i = 0; i < 100; i++) {
57              if (i != 0) {
58                  buf.writeByte('&');
59              }
60              ByteBufUtil.writeAscii(buf, "form-field-" + i);
61              buf.writeByte('=');
62  
63              ByteBufUtil.writeAscii(buf, randomString());
64          }
65      }
66  
67      private static CharSequence randomString() {
68          Random rng = PlatformDependent.threadLocalRandom();
69          int len = 4 + rng.nextInt(110);
70          StringBuilder sb = new StringBuilder(len);
71          for (int i = 0; i < len; i++) {
72              String chars = "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJLKMNOPQRSTUVWXYZ";
73              sb.append(chars.charAt(rng.nextInt(chars.length())));
74          }
75          return sb;
76      }
77  
78      @Benchmark
79      public List<InterfaceHttpData> decode() {
80          HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(request);
81          DefaultLastHttpContent content = new DefaultLastHttpContent(buf.duplicate());
82          decoder.offer(content);
83          return decoder.getBodyHttpDatas();
84      }
85  }