View Javadoc
1   /*
2    * Copyright 2020 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.mqtt;
17  
18  import io.netty.handler.codec.mqtt.MqttProperties.MqttPropertyType;
19  import io.netty.microbench.util.AbstractMicrobenchmark;
20  import org.openjdk.jmh.annotations.Benchmark;
21  import org.openjdk.jmh.annotations.BenchmarkMode;
22  import org.openjdk.jmh.annotations.Measurement;
23  import org.openjdk.jmh.annotations.Mode;
24  import org.openjdk.jmh.annotations.OutputTimeUnit;
25  import org.openjdk.jmh.annotations.Setup;
26  import org.openjdk.jmh.annotations.Warmup;
27  
28  import java.util.concurrent.TimeUnit;
29  
30  @BenchmarkMode(Mode.AverageTime)
31  @Warmup(iterations = 5, time = 5)
32  @Measurement(iterations = 5, time = 5)
33  @OutputTimeUnit(TimeUnit.NANOSECONDS)
34  public class MqttPropertyTypeValueOfBench extends AbstractMicrobenchmark {
35  
36      private static final int[] DATASET = new int[] {
37              28, 28, 40, 28, 36, 22, 23, 22, 11, 40, 18, 33, 8, 37, 23, 23, 33, 2, 2, 33, 18,
38              31, 39, 41, 39, 25, 2, 28, 26, 38, 42, 9, 40, 3, 2, 19, 1, 8, 18, 38, 28, 9, 18,
39              22, 42, 22, 37, 42, 23, 38, 22, 40, 2, 8, 40, 26, 1, 8, 17, 2, 37, 21, 41, 8, 37, 26, 1, 17,
40              39, 40, 41, 41, 9, 28, 38, 38, 36, 23, 19, 9, 9, 11, 25, 11, 9, 35, 31, 25, 3, 39,
41              17, 2, 35, 33, 21, 26, 34, 1, 34, 22, 33, 19, 17, 24, 9, 17, 40, 40, 11, 3, 19, 34,
42              42, 42, 11, 22, 39, 1, 25, 37, 41, 21, 42, 39, 1, 22, 21, 8, 28, 21, 17, 3, 39, 24,
43              33, 25, 9, 19, 42, 11, 42, 19, 2, 19, 35, 1, 11, 35, 42, 39, 9, 36, 36, 1, 23, 26,
44              41, 2, 41, 38, 9, 37, 9, 33, 2, 26, 18, 39, 21, 21, 8, 24, 17, 40, 1, 35, 36, 37,
45              21, 25, 1, 2, 24, 37, 3, 9, 42, 24, 28, 37, 24, 3, 35, 31, 11, 17, 42, 21, 23, 11,
46              41, 22, 38, 23, 11, 42, 38, 39, 34, 28, 21, 24, 9, 2, 35, 36, 21, 9, 21, 1, 8, 18,
47              3, 17, 24, 28, 40, 36, 21, 21, 3, 41, 41, 41, 22, 24, 19, 38, 39, 26, 3, 2, 41, 11,
48              25, 34, 42, 38, 31, 21, 23, 34, 8, 36, 19, 19, 42, 39, 3, 42, 35, 11, 33, 9, 17, 21,
49              18, 9, 42, 38, 24, 17, 34, 24, 3, 21, 23, 40, 25, 33, 11, 19, 31, 40, 24, 25, 23, 36,
50              23, 17, 39, 17, 22, 34, 28, 18, 25, 42, 31, 24, 19, 40, 21, 38, 22, 42, 35, 37, 41, 1,
51              17, 33, 3, 21, 42, 33, 2, 36, 36, 35, 42, 24, 37, 41, 8, 22, 36, 26, 42, 11, 41, 26,
52              24, 34, 11, 9, 34, 19, 23, 41, 22, 3, 35, 24, 1, 36, 24, 3, 18, 33, 2, 42, 42, 18,
53              19, 41, 38, 21, 34, 19, 40, 38, 19, 39, 21, 39, 42, 3, 36, 18, 22, 3, 25, 22, 28, 31,
54              31, 23, 24, 19, 34, 26, 33, 34, 42, 18, 3, 42, 19, 24, 21, 31, 8, 42, 25, 24, 39, 35,
55              3, 42, 31, 3, 18, 19, 24, 28, 3, 25, 39, 40, 40, 34, 33, 1, 41, 21, 17, 34, 31, 34,
56              34, 8, 17, 17, 19, 21, 21, 9, 21, 39, 24, 1, 23, 8, 37, 37, 23, 21, 34, 42, 23, 18,
57              42, 9, 34, 23, 24, 22, 11, 18, 18, 35, 24, 42, 23, 1, 31, 2, 9, 11, 24, 22, 34, 28,
58              11, 23, 26, 25, 31, 19, 39, 11, 40, 24, 41, 2, 11, 23, 33, 42, 34, 9, 17, 28, 33, 28,
59              2, 2, 21, 41, 42, 33, 33, 2, 8, 28, 19, 24, 36, 21, 36, 1, 19, 8, 1, 23, 21, 3,
60              40, 28, 38, 22, 21, 19, 37, 2, 23, 8, 33, 8, 31, 25, 17, 40, 36, 22, 3, 41, 21, 22,
61              41, 23, 3, 33, 26, 11, 33, 1, 9, 33, 40, 24, 11, 34, 8, 34, 19, 21, 34, 41, 19, 34,
62              42, 26, 41, 37, 28, 24, 42, 11, 38, 35, 33, 2, 26, 21, 9, 25, 9, 18, 33, 24, 19, 2,
63              11, 40, 37, 36, 19, 28, 40, 26, 41, 35, 21, 23, 28, 22, 19, 34, 3, 31, 36, 38, 25, 34,
64              31, 40, 38, 3, 22, 9, 8, 40, 26, 9, 17, 11, 11, 31, 19, 3, 24, 23, 3, 2, 19, 9,
65              28, 19, 28, 37, 18, 42, 38, 26, 37, 26, 39, 3, 33, 28, 17, 11, 25, 38, 34, 22, 34, 17,
66              3, 1, 34, 38, 8, 2, 37, 25, 9, 11, 36, 23, 19, 8, 35, 24, 11, 11, 11, 25, 11, 11,
67              11, 28, 17, 42, 19, 41, 40, 34, 38, 24, 28, 23, 39, 28, 41, 40, 3, 39, 34, 11, 25, 33,
68              2, 1, 3, 26, 28, 2, 17, 18, 2, 41, 42, 37, 36, 33, 38, 33, 18, 3, 34, 37, 21, 37,
69              23, 35, 21, 3, 9, 21, 34, 38, 22, 37, 28, 38, 8, 2, 31, 1, 38, 25, 40, 35, 37, 41,
70              36, 31, 23, 21, 37, 3, 24, 17, 17, 8, 22, 8, 2, 23, 1, 17, 31, 38, 9, 23, 42, 41,
71              2, 33, 11, 23, 33, 38, 17, 25, 1, 33, 37, 19, 8, 23, 41, 26, 39, 18, 18, 31, 17, 18,
72              34, 3, 41, 34, 40, 9, 23, 33, 11, 40, 39, 34, 19, 40, 3, 2, 19, 17, 23, 33, 2, 19,
73              26, 25, 36, 37, 34, 17, 39, 42, 22, 22, 19, 35, 22, 18, 18, 41, 40, 40, 26, 3, 19, 40,
74              9, 1, 19, 41, 24, 9, 18, 1, 28, 31, 18, 3, 21, 11, 24, 3, 22, 11, 11, 37, 22, 8,
75              2, 38, 3, 2, 37, 28, 11, 35, 18, 36, 9, 35, 21, 19, 42, 35, 24, 2, 2, 17, 18, 33,
76              33, 34, 8, 37, 24, 42, 17, 37, 21, 1, 36, 38, 25, 40, 1, 22, 26, 28, 22, 33, 28, 1,
77              33, 33, 33, 19, 40, 2, 36, 38, 33, 41, 2, 3, 31, 22, 1, 24, 18, 36, 28, 39, 28, 3,
78              8, 35, 17, 18, 18, 8, 18, 22, 2, 25, 18, 41, 37, 21, 17, 28, 34, 1, 35, 25, 22, 38,
79              17, 28, 19, 25, 35, 36, 39, 9, 21, 36, 39, 41, 22, 38, 39, 19, 34, 22, 40, 8, 1, 11,
80              1, 31, 1, 17, 24, 23, 28, 21, 8, 37, 42, 33, 17, 24, 19, 18, 2, 42, 39, 36, 19, 2,
81              34, 35, 36, 11, 9, 35, 2, 21, 3, 42, 28, 37, 24, 1, 38, 2, 11, 41, 33, 39, 25, 17,
82              26, 39, 36, 37, 11, 25, 42, 17, 8, 31, 41, 21, 22, 2, 2, 24, 19, 21, 31, 34, 2, 39,
83              39, 18, 1, 33, 28, 11, 34, 40, 17, 42,
84      };
85  
86      int[] types;
87      long next;
88      long mask;
89  
90      @Setup
91      public void initDataSet() {
92          types = DATASET;
93          next = 0;
94          mask = types.length - 1;
95          if (Integer.bitCount(types.length) != 1) {
96              throw new AssertionError("The data set should contains power of 2 items");
97          }
98      }
99  
100     @Benchmark
101     public MqttPropertyType getViaArray() {
102         long next = this.next;
103         int nextIndex = (int) (next & mask);
104         MqttPropertyType type = MqttPropertyType.valueOf(types[nextIndex]);
105         this.next = next + 1;
106         return type;
107     }
108 
109     @Benchmark
110     public MqttPropertyType getViaSwitch() {
111         long next = this.next;
112         int nextIndex = (int) (next & mask);
113         MqttPropertyType type = switchValueOf(types[nextIndex]);
114         this.next = next + 1;
115         return type;
116     }
117 
118     private static MqttPropertyType switchValueOf(int type) {
119         switch (type) {
120         case 1:
121             return MqttPropertyType.PAYLOAD_FORMAT_INDICATOR;
122         case 2:
123             return MqttPropertyType.PUBLICATION_EXPIRY_INTERVAL;
124         case 3:
125             return MqttPropertyType.CONTENT_TYPE;
126         case 8:
127             return MqttPropertyType.RESPONSE_TOPIC;
128         case 9:
129             return MqttPropertyType.CORRELATION_DATA;
130         case 11:
131             return MqttPropertyType.SUBSCRIPTION_IDENTIFIER;
132         case 17:
133             return MqttPropertyType.SESSION_EXPIRY_INTERVAL;
134         case 18:
135             return MqttPropertyType.ASSIGNED_CLIENT_IDENTIFIER;
136         case 19:
137             return MqttPropertyType.SERVER_KEEP_ALIVE;
138         case 21:
139             return MqttPropertyType.AUTHENTICATION_METHOD;
140         case 22:
141             return MqttPropertyType.AUTHENTICATION_DATA;
142         case 23:
143             return MqttPropertyType.REQUEST_PROBLEM_INFORMATION;
144         case 24:
145             return MqttPropertyType.WILL_DELAY_INTERVAL;
146         case 25:
147             return MqttPropertyType.REQUEST_RESPONSE_INFORMATION;
148         case 26:
149             return MqttPropertyType.RESPONSE_INFORMATION;
150         case 28:
151             return MqttPropertyType.SERVER_REFERENCE;
152         case 31:
153             return MqttPropertyType.REASON_STRING;
154         case 33:
155             return MqttPropertyType.RECEIVE_MAXIMUM;
156         case 34:
157             return MqttPropertyType.TOPIC_ALIAS_MAXIMUM;
158         case 35:
159             return MqttPropertyType.TOPIC_ALIAS;
160         case 36:
161             return MqttPropertyType.MAXIMUM_QOS;
162         case 37:
163             return MqttPropertyType.RETAIN_AVAILABLE;
164         case 38:
165             return MqttPropertyType.USER_PROPERTY;
166         case 39:
167             return MqttPropertyType.MAXIMUM_PACKET_SIZE;
168         case 40:
169             return MqttPropertyType.WILDCARD_SUBSCRIPTION_AVAILABLE;
170         case 41:
171             return MqttPropertyType.SUBSCRIPTION_IDENTIFIER_AVAILABLE;
172         case 42:
173             return MqttPropertyType.SHARED_SUBSCRIPTION_AVAILABLE;
174         default:
175             throw new IllegalArgumentException("unknown message type: " + type);
176         }
177     }
178 }