Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/util/Base64Encode.java
41161 views
1
/*
2
* Copyright (c) 2020, 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 Base64Encode {
38
39
private Base64.Encoder encoder;
40
private ArrayList<byte[]> unencoded;
41
private byte[] encoded;
42
43
private static final int TESTSIZE = 1000;
44
45
@Param({"1", "2", "3", "6", "7", "9", "10", "48", "512", "1000", "20000"})
46
private int maxNumBytes;
47
48
@Setup
49
public void setup() {
50
Random r = new Random(1123);
51
52
int dstLen = ((maxNumBytes + 16) / 3) * 4;
53
54
encoder = Base64.getEncoder();
55
unencoded = new ArrayList<byte[]> ();
56
encoded = new byte[dstLen];
57
58
for (int i = 0; i < TESTSIZE; i++) {
59
int srcLen = 1 + r.nextInt(maxNumBytes);
60
byte[] src = new byte[srcLen];
61
r.nextBytes(src);
62
unencoded.add(src);
63
}
64
}
65
66
@Benchmark
67
@OperationsPerInvocation(TESTSIZE)
68
public void testBase64Encode(Blackhole bh) {
69
for (byte[] s : unencoded) {
70
encoder.encode(s, encoded);
71
bh.consume(encoded);
72
}
73
}
74
}
75
76