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.netty5.handler.codec.http2;
33
34 import io.netty5.buffer.api.Buffer;
35 import io.netty5.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.netty5.buffer.api.DefaultBufferAllocators.onHeapAllocator;
46 import static io.netty5.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 Buffer input;
60
61 @Setup(Level.Trial)
62 public void setup() throws Http2Exception {
63 input = onHeapAllocator().copyOf(getSerializedHeaders(http2Headers(size, limitToAscii), sensitive))
64 .makeReadOnly();
65 }
66
67 @TearDown(Level.Trial)
68 public void teardown() {
69 input.close();
70 }
71
72 @Benchmark
73 @BenchmarkMode(Mode.Throughput)
74 public void decode(final Blackhole bh) throws Http2Exception {
75 HpackDecoder hpackDecoder = new HpackDecoder(Integer.MAX_VALUE);
76 Http2Headers headers = new DefaultHttp2Headers() {
77 @Override
78 public Http2Headers add(CharSequence name, CharSequence value) {
79 bh.consume(sensitive);
80 return this;
81 }
82 };
83 hpackDecoder.decode(0, input.copy(true), headers, true);
84 }
85
86 private byte[] getSerializedHeaders(Http2Headers headers, boolean sensitive) throws Http2Exception {
87 HpackEncoder hpackEncoder = HpackUtilBenchmark.newTestEncoder();
88 try (Buffer out = size.newOutBuffer()) {
89 hpackEncoder.encodeHeaders(3 , out, headers,
90 sensitive ? Http2HeadersEncoder.ALWAYS_SENSITIVE
91 : Http2HeadersEncoder.NEVER_SENSITIVE);
92 byte[] bytes = new byte[out.readableBytes()];
93 out.readBytes(bytes, 0, bytes.length);
94 return bytes;
95 }
96 }
97 }