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.microbench.util.AbstractMicrobenchmark;
19  import org.openjdk.jmh.annotations.Benchmark;
20  import org.openjdk.jmh.annotations.BenchmarkMode;
21  import org.openjdk.jmh.annotations.Measurement;
22  import org.openjdk.jmh.annotations.Mode;
23  import org.openjdk.jmh.annotations.OutputTimeUnit;
24  import org.openjdk.jmh.annotations.Setup;
25  import org.openjdk.jmh.annotations.Warmup;
26  
27  import java.util.concurrent.TimeUnit;
28  
29  @BenchmarkMode(Mode.AverageTime)
30  @Warmup(iterations = 5, time = 5)
31  @Measurement(iterations = 5, time = 5)
32  @OutputTimeUnit(TimeUnit.NANOSECONDS)
33  public class MqttConnectReturnCodeBench extends AbstractMicrobenchmark {
34  
35      private static final byte[] DATASET = new byte[] {
36              -103, -105, -100, -116, 0, 2, -100, 5, -125, 4, -118, -103, -123, 3, 0, -128, -119, 3, -99, 5, 0, -118,
37              -100, 5, -102, -127, -116,
38              -103, 4, -128, 0, -122, -101, -116, -128, -112, -124, -127, -118, -116, -107, -126, -119, -100, -121, -103,
39              -103, -103, 3,
40              -105, -126, 3, -105, -120, -127, -116, -100, -99, -122, -123, -101, -112, -101, 1, -100, 2, -116, 2, -101,
41              -97, -100, -124, -123,
42              -97, -112, -105, -118, -122, -120, 3, -102, -120, -103, 1, -119, -126, 3, -102, -125, -128, -125, -100,
43              -123, 0, -99, -118, -101,
44              -118, -112, -126, -128, 4, -121, -101, -124, 1, -118, -127, 1, -119, -120, -127, -100, -112, -124, 2, 5,
45              -103, -126, -101, -119,
46              -128, -122, -101, -119, -121, 4, -105, -103, 3, -122, 4, -119, -107, 4, -118, -121, -112, -118, 0, -100,
47              -116, -124, -107, 3,
48              -101, -125, -112, -102, -127, -105, -101, 5, -116, 4, -102, 0, -116, -101, -103, -99, -122, 2, -112, -118,
49              -103, -118, 3, -105,
50              -120, -99, -103, -116, -124, -124, -123, -128, -116, -101, -124, -122, -101, -122, -116, -118, 5, -97, 1,
51              -122, -127, -124, 1, 5,
52              4, -116, -112, -123, -123, -99, 4, -124, -126, -124, -103, 2, -119, -97, -116, -99, -101, -124, -97, -99, 0,
53              -126, -105, -120,
54              -123, -121, -100, -102, 3, -103, 0, -124, -123, -120, 0, -112, -118, -103, 1, -101, 0, -102, -99, 5, -125,
55              -100, -116, -105,
56              -122, -122, -99, 0, -121, -102, -102, 3, -121, -124, -116, 1, -122, -121, 2, 0, -101, -101, 5, -112, -101,
57              -128, 4, -99,
58              -105, -121, -118, 4, -122, -99, -103, 1, -102, -120, -99, -101, -103, 5, -120, -112, -118, -123, -128, -103,
59              -120, 4, -100, -101,
60              -107, -120, -99, -125, -119, -101, 5, 5, -107, -101, 3, -112, -122, -119, -122, -119, 0, -107, -127, 0,
61              -122, 4, 0, -119,
62              -97, 5, -119, -124, -102, -126, -107, -120, -100, -125, 4, -121, 1, -107, -100, -103, -105, -121, -123,
63              -120, -100, 0, 5, -127,
64              -128, -103, 3, -99, -121, -105, -101, -116, -127, -116, 1, 2, -105, -116, -126, 4, -123, -112, -105, -127,
65              -128, -123, -119, -121,
66              0, -119, 4, -119, -99, -97, 4, -112, -127, -103, -107, -124, -119, -102, -122, -122, -116, 1, -102, -107,
67              -119, -102, -128, 3,
68              -107, -99, -101, -126, -128, 4, -128, -103, 2, -127, -101, 4, -102, 1, -128, -97, -99, -112, -128, -107, 4,
69              -118, -100, -125,
70              -120, -116, 4, 3, 4, 2, -99, 1, -123, -120, -122, 5, -116, 2, -128, -101, -125, 3, 3, 3, -127, 5, 2, 4,
71              -122, -118, 0, -128, -121, -125, -97, -127, -124, 2, -127, -97, -97, -112, 0, -128, -105, -116, -118, -126,
72              -116, -119, -126, -127,
73              -116, -97, -105, -102, -107, -101, -126, -123, -107, -125, -107, -125, -120, -116, -123, -128, -119, -103,
74              -121, -126, -127,
75              -112, -121, 5, -127, 4, -124, -99, 4, -127, -118, -107, -99, 4, -119, -116, -127, 4, 4, -112, -124, -102,
76              -112, -99, -112, -121,
77              -128, 5, -101, -121, -116, -125, -112, -121, -107, -116, -123, 1, -105, -97, -101, -107, -100, -107, -125,
78              -121, -118, -123,
79              -99, -105, 5, 0, -123, -116, -127, -97, -124, -105, -124, -124, -118, -112, -103, -116, -125, -127, 1, -124,
80              5, -103, -128, 3, 2,
81              -103, -127, -103, -112, 0, 5, -120, -101, 4, 3, 1, 4, 4, -99, 0, -121, -124, -103, 3, -118, -127, 2, 2, 2,
82              -128, -119, 0,
83              -122, -112, -124, -101, 3, -102, 3, 3, -118, -99, -120, -125, 2, -100, -120, 0, 3, -120, -127, -122, -122,
84              -126, -127, -97,
85              -120, -99, -122, -102, -123, -116, -118, -119, -112, 2, 5, -100, -101, 4, -120, -107, 5, 1, -119, 0, -125,
86              -105, -101, -100,
87              -125, 0, -119, -123, 3, -118, 2, -127, -122, -100, 4, -126, 4, -125, -121, -100, 0, -112, -123, -125, 0,
88              -122, -101, -112,
89              -119, -118, -127, -124, -128, -127, -102, -125, -103, -103, -126, -116, -107, -125, 0, 3, -123, -100, 3,
90              -97, 2, -123, -121, -101,
91              -100, -119, -101, -123, -118, 4, 5, -125, -107, -107, 1, -105, 3, -99, -126, -99, 0, -118, 5, -122, -116,
92              -105, 2, -124,
93              -116, -126, 2, -121, -118, -100, 3, 0, -124, 2, -128, -126, -100, -99, -101, 1, -120, 2, -112, -102, -101,
94              5, -107, 1,
95              4, 2, -124, -100, -123, 4, -122, -118, -107, -103, -121, -101, -128, -112, -127, -105, -118, 2, -125, -121,
96              -101, 4, -126, -123,
97              0, -102, -128, -119, -99, 3, 4, -97, -128, -119, -99, -107, -116, -99, -127, -100, -119, -127, -122, -102,
98              -119, -118, -119, -103,
99              -123, -100, 5, -127, -112, 1, -125, -103, 4, 4, -99, 2, -116, -118, -105, 5, -123, -101, -123, -97, 4, -116,
100             2, -124,
101             -116, -125, 1, -118, -118, -124, -120, -118, -120, 4, 5, -118, -97, 0, -127, -100, -121, -97, -97, -125,
102             -120, -122, -126, -125,
103             -100, -97, 1, -97, -116, -126, -97, 2, 4, 2, -101, -103, -124, 2, 4, -123, -124, -107, -120, -122, 1, -123,
104             -97, -112,
105             -126, 0, -97, 0, -102, -99, -125, -101, -103, 1, -118, 5, -120, -102, -101, -116, -125, -125, -116, -102,
106             -120, -121, 3, -118,
107             -118, -123, -128, -126, 4, -101, -100, -103, -100, -105, -126, -121, -118, 4, -126, -123, -120, -99, -105,
108             -116, -127, -128,
109             -119, -124, -100, -120, -101, -100, 5, -116, -119, -105, -99, -119, -103, -103, -101, -107, -102, 5, -107,
110             -99, -102, -122, 2,
111             -125, -126, 0, -97, -123, 1, 3, 1, 5, -127, 2, -112, -103, -125, -112, -124, -118, -99, 0, 4, 2, -118, 5,
112             -128, -122, -120, 5,
113             -121, -112, 2, 5, -102, -125, -116, -127, -128, -102, 0, 2, 5, -122, -126, -120, -127, -101, -102, 5, -100,
114             -120, -107, -107,
115             -126, -101, 5, 4, -125, 4, -124, -125, -119, -123, -103, 2, -123, -105, 0, 1, 3, -121, -101, 3, -107, -105,
116             1, -105,
117             -122, -124, 0, -103, -116, 0, -101, -127, -122, -118, -103, 1, -107, -123, 1, -121, -107, 4, -102, -101, 4,
118             -127, -101, 3,
119             -121, -103, -125, -124, -127, 5, -128, 1, 3, -119, -126, -119, -125, -112, -124
120     };
121 
122     byte[] types;
123     long next;
124     long mask;
125 
126     @Setup
127     public void initDataSet() {
128         types = DATASET;
129         next = 0;
130         mask = types.length - 1;
131         if (Integer.bitCount(types.length) != 1) {
132             throw new AssertionError("The data set should contains power of 2 items");
133         }
134     }
135 
136     @Benchmark
137     public MqttConnectReturnCode getViaArray() {
138         long next = this.next;
139         int nextIndex = (int) (next & mask);
140         MqttConnectReturnCode code = MqttConnectReturnCode.valueOf(types[nextIndex]);
141         this.next = next + 1;
142         return code;
143     }
144 
145     @Benchmark
146     public MqttConnectReturnCode getViaSwitch() {
147         long next = this.next;
148         int nextIndex = (int) (next & mask);
149         MqttConnectReturnCode code = switchValueOf(types[nextIndex]);
150         this.next = next + 1;
151         return code;
152     }
153 
154     public static MqttConnectReturnCode switchValueOf(byte b) {
155         switch (b) {
156         case 0:
157             return MqttConnectReturnCode.CONNECTION_ACCEPTED;
158         case 1:
159             return MqttConnectReturnCode.CONNECTION_REFUSED_UNACCEPTABLE_PROTOCOL_VERSION;
160         case 2:
161             return MqttConnectReturnCode.CONNECTION_REFUSED_IDENTIFIER_REJECTED;
162         case 3:
163             return MqttConnectReturnCode.CONNECTION_REFUSED_SERVER_UNAVAILABLE;
164         case 4:
165             return MqttConnectReturnCode.CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD;
166         case 5:
167             return MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED;
168         case -128:
169             return MqttConnectReturnCode.CONNECTION_REFUSED_UNSPECIFIED_ERROR;
170         case -127:
171             return MqttConnectReturnCode.CONNECTION_REFUSED_MALFORMED_PACKET;
172         case -126:
173             return MqttConnectReturnCode.CONNECTION_REFUSED_PROTOCOL_ERROR;
174         case -125:
175             return MqttConnectReturnCode.CONNECTION_REFUSED_IMPLEMENTATION_SPECIFIC;
176         case -124:
177             return MqttConnectReturnCode.CONNECTION_REFUSED_UNSUPPORTED_PROTOCOL_VERSION;
178         case -123:
179             return MqttConnectReturnCode.CONNECTION_REFUSED_CLIENT_IDENTIFIER_NOT_VALID;
180         case -122:
181             return MqttConnectReturnCode.CONNECTION_REFUSED_BAD_USERNAME_OR_PASSWORD;
182         case -121:
183             return MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED_5;
184         case -120:
185             return MqttConnectReturnCode.CONNECTION_REFUSED_SERVER_UNAVAILABLE_5;
186         case -119:
187             return MqttConnectReturnCode.CONNECTION_REFUSED_SERVER_BUSY;
188         case -118:
189             return MqttConnectReturnCode.CONNECTION_REFUSED_BANNED;
190         case -116:
191             return MqttConnectReturnCode.CONNECTION_REFUSED_BAD_AUTHENTICATION_METHOD;
192         case -112:
193             return MqttConnectReturnCode.CONNECTION_REFUSED_TOPIC_NAME_INVALID;
194         case -107:
195             return MqttConnectReturnCode.CONNECTION_REFUSED_PACKET_TOO_LARGE;
196         case -105:
197             return MqttConnectReturnCode.CONNECTION_REFUSED_QUOTA_EXCEEDED;
198         case -103:
199             return MqttConnectReturnCode.CONNECTION_REFUSED_PAYLOAD_FORMAT_INVALID;
200         case -102:
201             return MqttConnectReturnCode.CONNECTION_REFUSED_RETAIN_NOT_SUPPORTED;
202         case -101:
203             return MqttConnectReturnCode.CONNECTION_REFUSED_QOS_NOT_SUPPORTED;
204         case -100:
205             return MqttConnectReturnCode.CONNECTION_REFUSED_USE_ANOTHER_SERVER;
206         case -99:
207             return MqttConnectReturnCode.CONNECTION_REFUSED_SERVER_MOVED;
208         case -97:
209             return MqttConnectReturnCode.CONNECTION_REFUSED_CONNECTION_RATE_EXCEEDED;
210         default:
211             throw new IllegalArgumentException("unknown connect return code: " + (b & 0xFF));
212         }
213     }
214 }