View Javadoc
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;
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.Param;
22  import org.openjdk.jmh.annotations.Threads;
23  import org.openjdk.jmh.annotations.Warmup;
24  
25  import java.util.Date;
26  
27  @Threads(1)
28  @Warmup(iterations = 5)
29  @Measurement(iterations = 5)
30  public class DateFormatter2Benchmark extends AbstractMicrobenchmark {
31  
32      @Param({"Sun, 27 Jan 2016 19:18:46 GMT", "Sun, 27 Dec 2016 19:18:46 GMT"})
33      String DATE_STRING;
34  
35      @Benchmark
36      public Date parseHttpHeaderDateFormatterNew() {
37          return DateFormatter.parseHttpDate(DATE_STRING);
38      }
39  
40      /*
41      @Benchmark
42      public Date parseHttpHeaderDateFormatter() {
43          return DateFormatterOld.parseHttpDate(DATE_STRING);
44      }
45      */
46  
47      /*
48       * Benchmark                        (DATE_STRING)   Mode  Cnt        Score       Error  Units
49       * parseHttpHeaderDateFormatter     Sun, 27 Jan 2016 19:18:46 GMT  thrpt    6  4142781.221 ± 82155.002  ops/s
50       * parseHttpHeaderDateFormatter     Sun, 27 Dec 2016 19:18:46 GMT  thrpt    6  3781810.558 ± 38679.061  ops/s
51       * parseHttpHeaderDateFormatterNew  Sun, 27 Jan 2016 19:18:46 GMT  thrpt    6  4372569.705 ± 30257.537  ops/s
52       * parseHttpHeaderDateFormatterNew  Sun, 27 Dec 2016 19:18:46 GMT  thrpt    6  4339785.100 ± 57542.660  ops/s
53       */
54  
55      /*Old DateFormatter.tryParseMonth method:
56      private boolean tryParseMonth(CharSequence txt, int tokenStart, int tokenEnd) {
57          int len = tokenEnd - tokenStart;
58  
59          if (len != 3) {
60              return false;
61          }
62  
63          if (matchMonth("Jan", txt, tokenStart)) {
64              month = Calendar.JANUARY;
65          } else if (matchMonth("Feb", txt, tokenStart)) {
66              month = Calendar.FEBRUARY;
67          } else if (matchMonth("Mar", txt, tokenStart)) {
68              month = Calendar.MARCH;
69          } else if (matchMonth("Apr", txt, tokenStart)) {
70              month = Calendar.APRIL;
71          } else if (matchMonth("May", txt, tokenStart)) {
72              month = Calendar.MAY;
73          } else if (matchMonth("Jun", txt, tokenStart)) {
74              month = Calendar.JUNE;
75          } else if (matchMonth("Jul", txt, tokenStart)) {
76              month = Calendar.JULY;
77          } else if (matchMonth("Aug", txt, tokenStart)) {
78              month = Calendar.AUGUST;
79          } else if (matchMonth("Sep", txt, tokenStart)) {
80              month = Calendar.SEPTEMBER;
81          } else if (matchMonth("Oct", txt, tokenStart)) {
82              month = Calendar.OCTOBER;
83          } else if (matchMonth("Nov", txt, tokenStart)) {
84              month = Calendar.NOVEMBER;
85          } else if (matchMonth("Dec", txt, tokenStart)) {
86              month = Calendar.DECEMBER;
87          } else {
88              return false;
89          }
90  
91          return true;
92      }
93      */
94  
95  }