查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2019 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a 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
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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          // Avoid constant pool for strings since it's common for at least values to not be constant.
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  }