Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/util/Base64Decode.java
41161 views
1
/*
2
* Copyright (c) 2021, Huawei Technologies Co., Ltd. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
package org.openjdk.micro.bench.java.util;
25
26
import org.openjdk.jmh.annotations.*;
27
import org.openjdk.jmh.infra.Blackhole;
28
29
import java.util.Base64;
30
import java.util.Random;
31
import java.util.ArrayList;
32
import java.util.concurrent.TimeUnit;
33
34
@BenchmarkMode(Mode.AverageTime)
35
@OutputTimeUnit(TimeUnit.NANOSECONDS)
36
@State(Scope.Thread)
37
public class Base64Decode {
38
39
private Base64.Encoder encoder, mimeEncoder;
40
private Base64.Decoder decoder, mimeDecoder;
41
private ArrayList<byte[]> encoded, mimeEncoded, errorEncoded;
42
private byte[] decoded, mimeDecoded, errorDecoded;
43
44
private static final int TESTSIZE = 1000;
45
46
@Param({"1", "3", "7", "32", "64", "80", "96",
47
"112", "512", "1000", "20000", "50000"})
48
private int maxNumBytes;
49
50
@Param({"4"})
51
private int lineSize;
52
53
private byte[] lineSeparator = {'\r', '\n'};
54
55
/* Other value can be tested by passing parameters to the JMH
56
tests: -p errorIndex=3,64,144,208,272,1000,20000. */
57
@Param({"144"})
58
private int errorIndex;
59
60
@Setup
61
public void setup() {
62
Random r = new Random(1123);
63
64
decoded = new byte[maxNumBytes + 1];
65
encoder = Base64.getEncoder();
66
decoder = Base64.getDecoder();
67
encoded = new ArrayList<byte[]> ();
68
69
mimeDecoded = new byte[maxNumBytes + 1];
70
mimeEncoder = Base64.getMimeEncoder(lineSize, lineSeparator);
71
mimeDecoder = Base64.getMimeDecoder();
72
mimeEncoded = new ArrayList<byte[]> ();
73
74
errorDecoded = new byte[errorIndex + 100];
75
errorEncoded = new ArrayList<byte[]> ();
76
77
for (int i = 0; i < TESTSIZE; i++) {
78
int srcLen = 1 + r.nextInt(maxNumBytes);
79
byte[] src = new byte[srcLen];
80
byte[] dst = new byte[((srcLen + 2) / 3) * 4];
81
r.nextBytes(src);
82
encoder.encode(src, dst);
83
encoded.add(dst);
84
85
int mimeSrcLen = 1 + r.nextInt(maxNumBytes);
86
byte[] mimeSrc = new byte[mimeSrcLen];
87
byte[] mimeDst = new byte[((mimeSrcLen + 2) / 3) * 4 * (lineSize + lineSeparator.length) / lineSize];
88
r.nextBytes(mimeSrc);
89
mimeEncoder.encode(mimeSrc, mimeDst);
90
mimeEncoded.add(mimeDst);
91
92
int errorSrcLen = errorIndex + r.nextInt(100);
93
byte[] errorSrc = new byte[errorSrcLen];
94
byte[] errorDst = new byte[(errorSrcLen + 2) / 3 * 4];
95
r.nextBytes(errorSrc);
96
encoder.encode(errorSrc, errorDst);
97
errorEncoded.add(errorDst);
98
errorDst[errorIndex] = (byte) '?';
99
}
100
}
101
102
@Benchmark
103
@OperationsPerInvocation(TESTSIZE)
104
public void testBase64Decode(Blackhole bh) {
105
for (byte[] s : encoded) {
106
decoder.decode(s, decoded);
107
bh.consume(decoded);
108
}
109
}
110
111
@Benchmark
112
@OperationsPerInvocation(TESTSIZE)
113
public void testBase64MIMEDecode(Blackhole bh) {
114
for (byte[] s : mimeEncoded) {
115
mimeDecoder.decode(s, mimeDecoded);
116
bh.consume(mimeDecoded);
117
}
118
}
119
120
@Benchmark
121
@OperationsPerInvocation(TESTSIZE)
122
public void testBase64WithErrorInputsDecode (Blackhole bh) {
123
for (byte[] s : errorEncoded) {
124
try {
125
decoder.decode(s, errorDecoded);
126
bh.consume(errorDecoded);
127
} catch (IllegalArgumentException e) {
128
bh.consume(e);
129
}
130
}
131
}
132
}
133
134