1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package io.netty.handler.codec.http2;
33
34 import io.netty.buffer.ByteBuf;
35 import io.netty.microbench.util.AbstractMicrobenchmark;
36 import org.openjdk.jmh.annotations.Benchmark;
37 import org.openjdk.jmh.annotations.BenchmarkMode;
38 import org.openjdk.jmh.annotations.Level;
39 import org.openjdk.jmh.annotations.Mode;
40 import org.openjdk.jmh.annotations.Param;
41 import org.openjdk.jmh.annotations.Setup;
42 import org.openjdk.jmh.annotations.TearDown;
43 import org.openjdk.jmh.infra.Blackhole;
44
45 import static io.netty.buffer.Unpooled.wrappedBuffer;
46 import static io.netty.handler.codec.http2.HpackBenchmarkUtil.http2Headers;
47
48 public class HpackDecoderBenchmark extends AbstractMicrobenchmark {
49
50 @Param
51 public HpackHeadersSize size;
52
53 @Param({ "true", "false" })
54 public boolean sensitive;
55
56 @Param({ "true", "false" })
57 public boolean limitToAscii;
58
59 private ByteBuf input;
60
61 @Setup(Level.Trial)
62 public void setup() throws Http2Exception {
63 input = wrappedBuffer(getSerializedHeaders(http2Headers(size, limitToAscii), sensitive));
64 }
65
66 @TearDown(Level.Trial)
67 public void teardown() {
68 input.release();
69 }
70
71 @Benchmark
72 @BenchmarkMode(Mode.Throughput)
73 public void decode(final Blackhole bh) throws Http2Exception {
74 HpackDecoder hpackDecoder = new HpackDecoder(Integer.MAX_VALUE);
75 @SuppressWarnings("unchecked")
76 Http2Headers headers =
77 new DefaultHttp2Headers() {
78 @Override
79 public Http2Headers add(CharSequence name, CharSequence value) {
80 bh.consume(sensitive);
81 return this;
82 }
83 };
84 hpackDecoder.decode(0, input.duplicate(), headers, true);
85 }
86
87 private byte[] getSerializedHeaders(Http2Headers headers, boolean sensitive) throws Http2Exception {
88 HpackEncoder hpackEncoder = HpackUtilBenchmark.newTestEncoder();
89 ByteBuf out = size.newOutBuffer();
90 try {
91 hpackEncoder.encodeHeaders(3 , out, headers,
92 sensitive ? Http2HeadersEncoder.ALWAYS_SENSITIVE
93 : Http2HeadersEncoder.NEVER_SENSITIVE);
94 byte[] bytes = new byte[out.readableBytes()];
95 out.readBytes(bytes);
96 return bytes;
97 } finally {
98 out.release();
99 }
100 }
101 }