Skip to content

Commit 3615d77

Browse files
committed
Add JMH encoding benchmark
1 parent b21ed76 commit 3615d77

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,18 @@
215215
<version>4.12</version>
216216
<scope>test</scope>
217217
</dependency>
218+
<dependency>
219+
<groupId>org.openjdk.jmh</groupId>
220+
<artifactId>jmh-core</artifactId>
221+
<version>1.21</version>
222+
<scope>test</scope>
223+
</dependency>
224+
<dependency>
225+
<groupId>org.openjdk.jmh</groupId>
226+
<artifactId>jmh-generator-annprocess</artifactId>
227+
<version>1.21</version>
228+
<scope>test</scope>
229+
</dependency>
218230
</dependencies>
219231
<developers>
220232
<developer>

src/main/java/at/favre/lib/bytes/BaseEncoding.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ private int decodeTo(byte[] target, CharSequence chars) {
151151
return bytesWritten;
152152
}
153153

154-
private static final class Alphabet {
154+
static final class Alphabet {
155155
// this is meant to be immutable -- don't modify it!
156156
private final char[] chars;
157157
final int mask;
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)