View Javadoc
1   /*
2    * Copyright 2015 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License, version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * 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 distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  package io.netty5.handler.codec.http2;
16  
17  import io.netty5.buffer.api.Buffer;
18  import io.netty5.buffer.api.BufferAllocator;
19  import io.netty5.channel.ChannelHandler;
20  import io.netty5.channel.ChannelHandlerContext;
21  import io.netty5.microbench.channel.EmbeddedChannelWriteReleaseHandlerContext;
22  import io.netty5.microbench.util.AbstractMicrobenchmark;
23  import org.openjdk.jmh.annotations.Benchmark;
24  import org.openjdk.jmh.annotations.BenchmarkMode;
25  import org.openjdk.jmh.annotations.Fork;
26  import org.openjdk.jmh.annotations.Level;
27  import org.openjdk.jmh.annotations.Measurement;
28  import org.openjdk.jmh.annotations.Mode;
29  import org.openjdk.jmh.annotations.OutputTimeUnit;
30  import org.openjdk.jmh.annotations.Param;
31  import org.openjdk.jmh.annotations.Scope;
32  import org.openjdk.jmh.annotations.Setup;
33  import org.openjdk.jmh.annotations.State;
34  import org.openjdk.jmh.annotations.TearDown;
35  import org.openjdk.jmh.annotations.Warmup;
36  
37  import java.util.concurrent.TimeUnit;
38  
39  import static io.netty5.buffer.api.BufferAllocator.onHeapPooled;
40  import static io.netty5.buffer.api.BufferAllocator.onHeapUnpooled;
41  
42  @Fork(1)
43  @Warmup(iterations = 5)
44  @Measurement(iterations = 5)
45  @State(Scope.Benchmark)
46  @OutputTimeUnit(TimeUnit.NANOSECONDS)
47  public class Http2FrameWriterDataBenchmark extends AbstractMicrobenchmark {
48      @Param({ "64", "1024", "4096", "16384", "1048576", "4194304" })
49      public int payloadSize;
50  
51      @Param({ "0", "100", "255" })
52      public int padding;
53  
54      @Param({ "true", "false" })
55      public boolean pooled;
56  
57      private Buffer payload;
58      private ChannelHandlerContext ctx;
59      private Http2DataWriter writer;
60      private Http2DataWriter oldWriter;
61      private BufferAllocator allocator;
62  
63      @Setup(Level.Trial)
64      public void setup() {
65          writer = new DefaultHttp2FrameWriter();
66          allocator = pooled ? onHeapPooled() : onHeapUnpooled();
67          payload = allocator.allocate(payloadSize);
68          for (int i = 0; i < payloadSize; i++) {
69              payload.writeByte((byte) 0);
70          }
71          payload.makeReadOnly();
72          ctx = new EmbeddedChannelWriteReleaseHandlerContext(allocator, new ChannelHandler() { }) {
73              @Override
74              protected void handleException(Throwable t) {
75                  handleUnexpectedException(t);
76              }
77          };
78      }
79  
80      @TearDown(Level.Trial)
81      public void teardown() throws Exception {
82          if (payload != null) {
83              payload.close();
84          }
85          if (ctx != null) {
86              ctx.close();
87          }
88          allocator.close();
89      }
90  
91      @Benchmark
92      @BenchmarkMode(Mode.AverageTime)
93      public void newWriter() {
94          writer.writeData(ctx, 3, payload.copy(true), padding, true);
95          ctx.flush();
96      }
97  }