|
| 1 | +/* |
| 2 | + * Copyright 2018 Patrick Favre-Bulle |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 5 | + * or more contributor license agreements. See the NOTICE file |
| 6 | + * distributed with this work for additional information |
| 7 | + * regarding copyright ownership. The ASF licenses this file |
| 8 | + * to you under the Apache License, Version 2.0 (the |
| 9 | + * "License"); you may not use this file except in compliance |
| 10 | + * with the License. You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, |
| 15 | + * software distributed under the License is distributed on an |
| 16 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | + * KIND, either express or implied. See the License for the |
| 18 | + * specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + */ |
| 21 | + |
| 22 | +package at.favre.lib.bytes; |
| 23 | + |
| 24 | +import org.openjdk.jmh.annotations.*; |
| 25 | + |
| 26 | +import java.nio.ByteOrder; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Random; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | + |
| 32 | +/* |
| 33 | +# JMH version: 1.21 |
| 34 | +# VM version: JDK 1.8.0_172, Java HotSpot(TM) 64-Bit Server VM, 25.172-b11 |
| 35 | +# i7 7700K / 24G |
| 36 | +
|
| 37 | +Benchmark (byteLength) Mode Cnt Score Error Units |
| 38 | +EncodingJmhBenchmark.encodeBase64Guava 1 thrpt 4 10361634,745 ± 152739,710 ops/s |
| 39 | +EncodingJmhBenchmark.encodeBase64Guava 16 thrpt 4 4360485,804 ± 44729,417 ops/s |
| 40 | +EncodingJmhBenchmark.encodeBase64Guava 128 thrpt 4 790407,010 ± 8095,476 ops/s |
| 41 | +EncodingJmhBenchmark.encodeBase64Guava 512 thrpt 4 192448,674 ± 2196,035 ops/s |
| 42 | +EncodingJmhBenchmark.encodeBase64Guava 1000000 thrpt 4 102,780 ± 2,949 ops/s |
| 43 | +EncodingJmhBenchmark.encodeBase64Okio 1 thrpt 4 12658987,399 ± 361955,366 ops/s |
| 44 | +EncodingJmhBenchmark.encodeBase64Okio 16 thrpt 4 7059404,777 ± 293665,348 ops/s |
| 45 | +EncodingJmhBenchmark.encodeBase64Okio 128 thrpt 4 1749131,031 ± 85915,325 ops/s |
| 46 | +EncodingJmhBenchmark.encodeBase64Okio 512 thrpt 4 239764,488 ± 6204,540 ops/s |
| 47 | +EncodingJmhBenchmark.encodeBase64Okio 1000000 thrpt 4 107,868 ± 0,569 ops/s |
| 48 | + */ |
| 49 | + |
| 50 | +@State(Scope.Thread) |
| 51 | +@Fork(1) |
| 52 | +@Warmup(iterations = 2, time = 2) |
| 53 | +@Measurement(iterations = 4, time = 5) |
| 54 | +@BenchmarkMode(Mode.Throughput) |
| 55 | +@OutputTimeUnit(TimeUnit.SECONDS) |
| 56 | +public class EncodingJmhBenchmark { |
| 57 | + |
| 58 | + @Param({"1", "16", "128", "512", "1000000"}) |
| 59 | + private int byteLength; |
| 60 | + private Map<Integer, Bytes[]> rndMap; |
| 61 | + |
| 62 | + private BinaryToTextEncoding.EncoderDecoder base64Okio; |
| 63 | + private BinaryToTextEncoding.EncoderDecoder base64Guava; |
| 64 | + private Random random; |
| 65 | + |
| 66 | + @Setup(Level.Trial) |
| 67 | + public void setup() { |
| 68 | + random = new Random(); |
| 69 | + base64Okio = new BinaryToTextEncoding.Base64Encoding(); |
| 70 | + base64Guava = new BaseEncoding(new BaseEncoding.Alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray()), BaseEncoding.BASE32_RFC4848_PADDING); |
| 71 | + rndMap = new HashMap<>(); |
| 72 | + int[] lengths = new int[]{1, 16, 128, 512, 1000000}; |
| 73 | + for (int length : lengths) { |
| 74 | + int count = 10; |
| 75 | + rndMap.put(length, new Bytes[count]); |
| 76 | + for (int i = 0; i < count; i++) { |
| 77 | + rndMap.get(length)[i] = Bytes.random(length); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Benchmark |
| 83 | + public byte[] encodeBase64Okio() { |
| 84 | + return encodeDecode(base64Okio); |
| 85 | + } |
| 86 | + |
| 87 | + @Benchmark |
| 88 | + public byte[] encodeBase64Guava() { |
| 89 | + return encodeDecode(base64Guava); |
| 90 | + } |
| 91 | + |
| 92 | + private byte[] encodeDecode(BinaryToTextEncoding.EncoderDecoder encoder) { |
| 93 | + Bytes[] bytes = rndMap.get(byteLength); |
| 94 | + int rndNum = random.nextInt(bytes.length); |
| 95 | + |
| 96 | + String encoded = encoder.encode(bytes[rndNum].array(), ByteOrder.BIG_ENDIAN); |
| 97 | + return encoder.decode(encoded); |
| 98 | + } |
| 99 | +} |
0 commit comments