1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.http;
17
18 import io.netty.microbench.util.AbstractMicrobenchmark;
19 import org.openjdk.jmh.annotations.Benchmark;
20 import org.openjdk.jmh.annotations.Measurement;
21 import org.openjdk.jmh.annotations.OutputTimeUnit;
22 import org.openjdk.jmh.annotations.Setup;
23 import org.openjdk.jmh.annotations.Threads;
24 import org.openjdk.jmh.annotations.Warmup;
25
26 import java.util.concurrent.TimeUnit;
27
28 @Threads(1)
29 @Warmup(iterations = 3)
30 @Measurement(iterations = 3)
31 @OutputTimeUnit(TimeUnit.MICROSECONDS)
32 public class QueryStringEncoderBenchmark extends AbstractMicrobenchmark {
33 private String shortAscii;
34 private String shortUtf8;
35 private String shortAsciiFirst;
36
37 private String longAscii;
38 private String longUtf8;
39 private String longAsciiFirst;
40
41 @Setup
42 public void setUp() {
43
44 shortAscii = new String("foo".toCharArray());
45 shortUtf8 = new String("ほげほげ".toCharArray());
46 shortAsciiFirst = shortAscii + shortUtf8;
47 longAscii = repeat(shortAscii, 100);
48 longUtf8 = repeat(shortUtf8, 100);
49 longAsciiFirst = longAscii + longUtf8;
50 }
51
52 @Benchmark
53 public String shortAscii() {
54 return encode(shortAscii);
55 }
56
57 @Benchmark
58 public String shortUtf8() {
59 return encode(shortUtf8);
60 }
61
62 @Benchmark
63 public String shortAsciiFirst() {
64 return encode(shortAsciiFirst);
65 }
66
67 @Benchmark
68 public String longAscii() {
69 return encode(longAscii);
70 }
71
72 @Benchmark
73 public String longUtf8() {
74 return encode(longUtf8);
75 }
76
77 @Benchmark
78 public String longAsciiFirst() {
79 return encode(longAsciiFirst);
80 }
81
82 private static String encode(String s) {
83 QueryStringEncoder encoder = new QueryStringEncoder("");
84 encoder.addParam(s, s);
85 return encoder.toString();
86 }
87
88 private static String repeat(String s, int num) {
89 StringBuilder sb = new StringBuilder(num * s.length());
90 for (int i = 0; i < num; i++) {
91 sb.append(s);
92 }
93 return sb.toString();
94 }
95 }