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.Fork;
39 import org.openjdk.jmh.annotations.Level;
40 import org.openjdk.jmh.annotations.Measurement;
41 import org.openjdk.jmh.annotations.Mode;
42 import org.openjdk.jmh.annotations.OutputTimeUnit;
43 import org.openjdk.jmh.annotations.Param;
44 import org.openjdk.jmh.annotations.Scope;
45 import org.openjdk.jmh.annotations.Setup;
46 import org.openjdk.jmh.annotations.State;
47 import org.openjdk.jmh.annotations.TearDown;
48 import org.openjdk.jmh.annotations.Threads;
49 import org.openjdk.jmh.annotations.Warmup;
50 import org.openjdk.jmh.infra.Blackhole;
51
52 import java.util.Iterator;
53 import java.util.Map;
54 import java.util.concurrent.TimeUnit;
55
56 @Fork(1)
57 @Threads(1)
58 @State(Scope.Benchmark)
59 @Warmup(iterations = 5)
60 @Measurement(iterations = 5)
61 @OutputTimeUnit(TimeUnit.NANOSECONDS)
62 public class HpackEncoderBenchmark extends AbstractMicrobenchmark {
63
64 @Param
65 public HpackHeadersSize size;
66
67 @Param({ "true", "false" })
68 public boolean sensitive;
69
70 @Param({ "true", "false" })
71 public boolean duplicates;
72
73 @Param({ "true", "false" })
74 public boolean limitToAscii;
75
76 private Http2Headers http2Headers;
77 private Buffer output;
78 private Http2HeadersEncoder.SensitivityDetector sensitivityDetector;
79
80 @Setup(Level.Trial)
81 public void setup() {
82 http2Headers = HpackBenchmarkUtil.http2Headers(size, limitToAscii);
83 if (duplicates) {
84 int size = http2Headers.size();
85 if (size > 0) {
86 Iterator<Map.Entry<CharSequence, CharSequence>> itr = http2Headers.iterator();
87 Map.Entry<CharSequence, CharSequence> entry = itr.next();
88 http2Headers.clear();
89 for (int i = 0; i < size; ++i) {
90 http2Headers.add(entry.getKey(), entry.getValue());
91 }
92 }
93 }
94 output = size.newOutBuffer();
95 sensitivityDetector = sensitive ? Http2HeadersEncoder.ALWAYS_SENSITIVE : Http2HeadersEncoder.NEVER_SENSITIVE;
96 }
97
98 @TearDown(Level.Trial)
99 public void tearDown() {
100 output.close();
101 }
102
103 @Benchmark
104 @BenchmarkMode(Mode.AverageTime)
105 public void encode(Blackhole bh) throws Exception {
106 HpackEncoder hpackEncoder = HpackUtilBenchmark.newTestEncoder();
107 output.resetOffsets();
108 hpackEncoder.encodeHeaders(3 , output, http2Headers, sensitivityDetector);
109 bh.consume(output);
110 }
111 }