Debian Codegraph @

proton-j/src/test/java/org/apache/qpid/proton/codec/Benchmark.java

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21 package org.apache.qpid.proton.codec;
22
23 import java.io.IOException;
24 import java.lang.management.ManagementFactory;
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.util.HashMap;
28 import java.util.UUID;
29 import java.util.concurrent.TimeUnit;
30
31 import org.apache.qpid.proton.amqp.Binary;
32 import org.apache.qpid.proton.amqp.Symbol;
33 import org.apache.qpid.proton.amqp.UnsignedByte;
34 import org.apache.qpid.proton.amqp.UnsignedInteger;
35 import org.apache.qpid.proton.amqp.UnsignedShort;
36 import org.apache.qpid.proton.amqp.messaging.Accepted;
37 import org.apache.qpid.proton.amqp.messaging.ApplicationProperties;
38 import org.apache.qpid.proton.amqp.messaging.Data;
39 import org.apache.qpid.proton.amqp.messaging.Header;
40 import org.apache.qpid.proton.amqp.messaging.MessageAnnotations;
41 import org.apache.qpid.proton.amqp.messaging.Properties;
42 import org.apache.qpid.proton.amqp.transport.Disposition;
43 import org.apache.qpid.proton.amqp.transport.Flow;
44 import org.apache.qpid.proton.amqp.transport.Role;
45 import org.apache.qpid.proton.amqp.transport.Transfer;
46 import org.apache.qpid.proton.codec.WritableBuffer.ByteBufferWrapper;
47
48 public class Benchmark implements Runnable {
49
50 private static final int ITERATIONS = 10 * 1024 * 1024;
51
52 private ByteBufferWrapper outputBuf = WritableBuffer.ByteBufferWrapper.allocate(8192);
53 private BenchmarkResult resultSet = new BenchmarkResult();
54 private boolean warming = true;
55
56 private final DecoderImpl decoder = new DecoderImpl();
57 private final EncoderImpl encoder = new EncoderImpl(decoder);
58
59 public static final void main(String[] args) throws IOException, InterruptedException {
60 System.out.println("Current PID: " + ManagementFactory.getRuntimeMXBean().getName());
61 Benchmark benchmark = new Benchmark();
62 benchmark.run();
63 }
64
65 @Override
66 public void run() {
67 AMQPDefinedTypes.registerAllTypes(decoder, encoder);
68
69 encoder.setByteBuffer(outputBuf);
70
71 try {
72 doBenchmarks();
73 warming = false;
74 doBenchmarks();
75 } catch (IOException e) {
76 System.out.println("Unexpected error: " + e.getMessage());
77 }
78 }
79
80 private void time(String message, BenchmarkResult resultSet) {
81 if (!warming) {
82 System.out.println("Benchamrk of type: " + message + ": ");
83 System.out.println(" Encode time = " + resultSet.getEncodeTimeMills());
84 System.out.println(" Decode time = " + resultSet.getDecodeTimeMills());
85 }
86 }
87
88 private final void doBenchmarks() throws IOException {
89 benchmarkListOfInts();
90 benchmarkUUIDs();
91 benchmarkHeader();
92 benchmarkProperties();
93 benchmarkMessageAnnotations();
94 benchmarkApplicationProperties();
95 benchmarkSymbols();
96 benchmarkTransfer();
97 benchmarkFlow();
98 benchmarkDisposition();
99 benchmarkString();
100 benchmarkData();
101 warming = false;
102 }
103
104 private CompositeReadableBuffer convertToComposite(WritableBuffer buffer) {
105 CompositeReadableBuffer composite = new CompositeReadableBuffer();
106 ReadableBuffer readableView = outputBuf.toReadableBuffer();
107
108 byte[] copy = new byte[readableView.remaining()];
109 readableView.get(copy);
110
111 return composite.append(copy);
112 }
113
114 private void benchmarkListOfInts() throws IOException {
115 ArrayList<Object> list = new ArrayList<>(10);
116 for (int j = 0; j < 10; j++) {
117 list.add(0);
118 }
119
120 resultSet.start();
121 for (int i = 0; i < ITERATIONS; i++) {
122 outputBuf.byteBuffer().clear();
123 encoder.writeList(list);
124 }
125 resultSet.encodesComplete();
126
127 CompositeReadableBuffer inputBuf = convertToComposite(outputBuf);
128 decoder.setBuffer(inputBuf);
129
130 resultSet.start();
131 for (int i = 0; i < ITERATIONS; i++) {
132 decoder.readList();
133 inputBuf.flip();
134 }
135 resultSet.decodesComplete();
136
137 time("List<Integer>", resultSet);
138 }
139
140 private void benchmarkUUIDs() throws IOException {
141 UUID uuid = UUID.randomUUID();
142
143 resultSet.start();
144 for (int i = 0; i < ITERATIONS; i++) {
145 outputBuf.byteBuffer().clear();
146 encoder.writeUUID(uuid);
147 }
148 resultSet.encodesComplete();
149
150 CompositeReadableBuffer inputBuf = convertToComposite(outputBuf);
151 decoder.setBuffer(inputBuf);
152
153 resultSet.start();
154 for (int i = 0; i < ITERATIONS; i++) {
155 decoder.readUUID();
156 inputBuf.flip();
157 }
158 resultSet.decodesComplete();
159
160 time("UUID", resultSet);
161 }
162
163 private void benchmarkHeader() throws IOException {
164 Header header = new Header();
165 header.setDurable(true);
166 header.setFirstAcquirer(true);
167
168 resultSet.start();
169 for (int i = 0; i < ITERATIONS; i++) {
170 outputBuf.byteBuffer().clear();
171 encoder.writeObject(header);
172 }
173 resultSet.encodesComplete();
174
175 CompositeReadableBuffer inputBuf = convertToComposite(outputBuf);
176 decoder.setBuffer(inputBuf);
177
178 resultSet.start();
179 for (int i = 0; i < ITERATIONS; i++) {
180 decoder.readObject();
181 inputBuf.flip();
182 }
183 resultSet.decodesComplete();
184
185 time("Header", resultSet);
186 }
187
188 private void benchmarkTransfer() throws IOException {
189 Transfer transfer = new Transfer();
190 transfer.setDeliveryTag(new Binary(new byte[] {1, 2, 3}));
191 transfer.setHandle(UnsignedInteger.valueOf(10));
192 transfer.setMessageFormat(UnsignedInteger.ZERO);
193
194 resultSet.start();
195 for (int i = 0; i < ITERATIONS; i++) {
196 outputBuf.byteBuffer().clear();
197 encoder.writeObject(transfer);
198 }
199 resultSet.encodesComplete();
200
201 CompositeReadableBuffer inputBuf = convertToComposite(outputBuf);
202 decoder.setBuffer(inputBuf);
203
204 resultSet.start();
205 for (int i = 0; i < ITERATIONS; i++) {
206 decoder.readObject();
207 inputBuf.flip();
208 }
209 resultSet.decodesComplete();
210
211 time("Transfer", resultSet);
212 }
213
214 private void benchmarkFlow() throws IOException {
215 Flow flow = new Flow();
216 flow.setNextIncomingId(UnsignedInteger.valueOf(1));
217 flow.setIncomingWindow(UnsignedInteger.valueOf(2047));
218 flow.setNextOutgoingId(UnsignedInteger.valueOf(1));
219 flow.setOutgoingWindow(UnsignedInteger.MAX_VALUE);
220 flow.setHandle(UnsignedInteger.ZERO);
221 flow.setDeliveryCount(UnsignedInteger.valueOf(10));
222 flow.setLinkCredit(UnsignedInteger.valueOf(1000));
223
224 resultSet.start();
225 for (int i = 0; i < ITERATIONS; i++) {
226 outputBuf.byteBuffer().clear();
227 encoder.writeObject(flow);
228 }
229 resultSet.encodesComplete();
230
231 CompositeReadableBuffer inputBuf = convertToComposite(outputBuf);
232 decoder.setBuffer(inputBuf);
233
234 resultSet.start();
235 for (int i = 0; i < ITERATIONS; i++) {
236 decoder.readObject();
237 inputBuf.flip();
238 }
239 resultSet.decodesComplete();
240
241 time("Flow", resultSet);
242 }
243
244 private void benchmarkProperties() throws IOException {
245 Properties properties = new Properties();
246 properties.setTo("queue:1-1024");
247 properties.setReplyTo("queue:1-11024-reply");
248 properties.setMessageId("ID:255f1297-5a71-4df1-8147-b2cdf850a56f:1");
249 properties.setCreationTime(new Date(System.currentTimeMillis()));
250
251 resultSet.start();
252 for (int i = 0; i < ITERATIONS; i++) {
253 outputBuf.byteBuffer().clear();
254 encoder.writeObject(properties);
255 }
256 resultSet.encodesComplete();
257
258 CompositeReadableBuffer inputBuf = convertToComposite(outputBuf);
259 decoder.setBuffer(inputBuf);
260
261 resultSet.start();
262 for (int i = 0; i < ITERATIONS; i++) {
263 decoder.readObject();
264 inputBuf.flip();
265 }
266 resultSet.decodesComplete();
267
268 time("Properties", resultSet);
269 }
270
271 private void benchmarkMessageAnnotations() throws IOException {
272 MessageAnnotations annotations = new MessageAnnotations(new HashMap<Symbol, Object>());
273 annotations.getValue().put(Symbol.valueOf("test1"), UnsignedByte.valueOf((byte) 128));
274 annotations.getValue().put(Symbol.valueOf("test2"), UnsignedShort.valueOf((short) 128));
275 annotations.getValue().put(Symbol.valueOf("test3"), UnsignedInteger.valueOf((byte) 128));
276
277 resultSet.start();
278 for (int i = 0; i < ITERATIONS; i++) {
279 outputBuf.byteBuffer().clear();
280 encoder.writeObject(annotations);
281 }
282 resultSet.encodesComplete();
283
284 CompositeReadableBuffer inputBuf = convertToComposite(outputBuf);
285 decoder.setBuffer(inputBuf);
286
287 resultSet.start();
288 for (int i = 0; i < ITERATIONS; i++) {
289 decoder.readObject();
290 inputBuf.flip();
291 }
292 resultSet.decodesComplete();
293
294 time("MessageAnnotations", resultSet);
295 }
296
297 private void benchmarkApplicationProperties() throws IOException {
298 ApplicationProperties properties = new ApplicationProperties(new HashMap<String, Object>());
299 properties.getValue().put("test1", UnsignedByte.valueOf((byte) 128));
300 properties.getValue().put("test2", UnsignedShort.valueOf((short) 128));
301 properties.getValue().put("test3", UnsignedInteger.valueOf((byte) 128));
302
303 resultSet.start();
304 for (int i = 0; i < ITERATIONS; i++) {
305 outputBuf.byteBuffer().clear();
306 encoder.writeObject(properties);
307 }
308 resultSet.encodesComplete();
309
310 CompositeReadableBuffer inputBuf = convertToComposite(outputBuf);
311 decoder.setBuffer(inputBuf);
312
313 resultSet.start();
314 for (int i = 0; i < ITERATIONS; i++) {
315 decoder.readObject();
316 inputBuf.flip();
317 }
318 resultSet.decodesComplete();
319
320 time("ApplicationProperties", resultSet);
321 }
322
323 private void benchmarkSymbols() throws IOException {
324 Symbol symbol1 = Symbol.valueOf("Symbol-1");
325 Symbol symbol2 = Symbol.valueOf("Symbol-2");
326 Symbol symbol3 = Symbol.valueOf("Symbol-3");
327
328 resultSet.start();
329 for (int i = 0; i < ITERATIONS; i++) {
330 outputBuf.byteBuffer().clear();
331 encoder.writeSymbol(symbol1);
332 encoder.writeSymbol(symbol2);
333 encoder.writeSymbol(symbol3);
334 }
335 resultSet.encodesComplete();
336
337 CompositeReadableBuffer inputBuf = convertToComposite(outputBuf);
338 decoder.setBuffer(inputBuf);
339
340 resultSet.start();
341 for (int i = 0; i < ITERATIONS; i++) {
342 decoder.readSymbol();
343 decoder.readSymbol();
344 decoder.readSymbol();
345 inputBuf.flip();
346 }
347 resultSet.decodesComplete();
348
349 time("Symbol", resultSet);
350 }
351
352 private void benchmarkDisposition() throws IOException {
353 Disposition disposition = new Disposition();
354 disposition.setRole(Role.RECEIVER);
355 disposition.setSettled(true);
356 disposition.setState(Accepted.getInstance());
357 disposition.setFirst(UnsignedInteger.valueOf(2));
358 disposition.setLast(UnsignedInteger.valueOf(2));
359
360 resultSet.start();
361 for (int i = 0; i < ITERATIONS; i++) {
362 outputBuf.byteBuffer().clear();
363 encoder.writeObject(disposition);
364 }
365 resultSet.encodesComplete();
366
367 CompositeReadableBuffer inputBuf = convertToComposite(outputBuf);
368 decoder.setBuffer(inputBuf);
369
370 resultSet.start();
371 for (int i = 0; i < ITERATIONS; i++) {
372 decoder.readObject();
373 inputBuf.flip();
374 }
375 resultSet.decodesComplete();
376
377 time("Disposition", resultSet);
378 }
379
380 private void benchmarkString() throws IOException {
381 String string1 = new String("String-1-somewhat-long-test-to-validate-performance-improvements-to-the-proton-j-codec-@!%$");
382 String string2 = new String("String-2-somewhat-long-test-to-validate-performance-improvements-to-the-proton-j-codec-@!%$");
383 String string3 = new String("String-3-somewhat-long-test-to-validate-performance-improvements-to-the-proton-j-codec-@!%$");
384
385 resultSet.start();
386 for (int i = 0; i < ITERATIONS; i++) {
387 outputBuf.byteBuffer().clear();
388 encoder.writeString(string1);
389 encoder.writeString(string2);
390 encoder.writeString(string3);
391 }
392 resultSet.encodesComplete();
393
394 CompositeReadableBuffer inputBuf = convertToComposite(outputBuf);
395 decoder.setBuffer(inputBuf);
396
397 resultSet.start();
398 for (int i = 0; i < ITERATIONS; i++) {
399 decoder.readString();
400 decoder.readString();
401 decoder.readString();
402 inputBuf.flip();
403 }
404 resultSet.decodesComplete();
405
406 time("String", resultSet);
407 }
408
409 private void benchmarkData() throws IOException {
410 Data data1 = new Data(new Binary(new byte[] {1, 2, 3}));
411 Data data2 = new Data(new Binary(new byte[] {4, 5, 6}));
412 Data data3 = new Data(new Binary(new byte[] {7, 8, 9}));
413
414 resultSet.start();
415 for (int i = 0; i < ITERATIONS; i++) {
416 outputBuf.byteBuffer().clear();
417 encoder.writeObject(data1);
418 encoder.writeObject(data2);
419 encoder.writeObject(data3);
420 }
421 resultSet.encodesComplete();
422
423 CompositeReadableBuffer inputBuf = convertToComposite(outputBuf);
424 decoder.setBuffer(inputBuf);
425
426 resultSet.start();
427 for (int i = 0; i < ITERATIONS; i++) {
428 decoder.readObject();
429 decoder.readObject();
430 decoder.readObject();
431 inputBuf.flip();
432 }
433 resultSet.decodesComplete();
434
435 time("Data", resultSet);
436 }
437
438 private static class BenchmarkResult {
439
440 private long startTime;
441
442 private long encodeTime;
443 private long decodeTime;
444
445 public void start() {
446 startTime = System.nanoTime();
447 }
448
449 public void encodesComplete() {
450 encodeTime = System.nanoTime() - startTime;
451 }
452
453 public void decodesComplete() {
454 decodeTime = System.nanoTime() - startTime;
455 }
456
457 public long getEncodeTimeMills() {
458 return TimeUnit.NANOSECONDS.toMillis(encodeTime);
459 }
460
461 public long getDecodeTimeMills() {
462 return TimeUnit.NANOSECONDS.toMillis(decodeTime);
463 }
464 }
465 }