Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/util/Base64VarLenDecode.java
41161 views
1
/*
2
* Copyright (c) 2020, Oracle America, Inc.
3
* All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
7
*
8
* * Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
*
11
* * Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
*
15
* * Neither the name of Oracle nor the names of its contributors may be used
16
* to endorse or promote products derived from this software without
17
* specific prior written permission.
18
*
19
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29
* THE POSSIBILITY OF SUCH DAMAGE.
30
*/
31
32
package org.openjdk.micro.bench.java.util;
33
34
import org.openjdk.jmh.annotations.*;
35
import java.util.*;
36
37
public class Base64VarLenDecode {
38
39
@State(Scope.Thread)
40
public static class MyState {
41
42
@Setup(Level.Trial)
43
public void doSetupTrial() {
44
ran = new Random(10101); // fixed seed for repeatability
45
encoder = Base64.getEncoder();
46
decoder = Base64.getDecoder();
47
System.out.println("Do Trial Setup");
48
}
49
50
@Setup(Level.Invocation)
51
public void doSetupInvocation() {
52
bin_src_len = 8 + ran.nextInt(20000);
53
base64_len = ((bin_src_len + 2) / 3) * 4;
54
unencoded = new byte[bin_src_len];
55
encoded = new byte[base64_len];
56
decoded = new byte[bin_src_len];
57
ran.nextBytes(unencoded);
58
encoder.encode(unencoded, encoded);
59
}
60
61
@TearDown(Level.Invocation)
62
public void doTearDownInvocation() {
63
// This isn't really a teardown. It's a check for correct functionality.
64
// Each iteration should produce a correctly decoded buffer that's equal
65
// to the unencoded data.
66
if (!Arrays.equals(unencoded, decoded)) {
67
System.out.println("Original data and decoded data are not equal!");
68
for (int j = 0; j < unencoded.length; j++) {
69
if (unencoded[j] != decoded[j]) {
70
System.out.format("%06x: %02x %02x\n", j, unencoded[j], decoded[j]);
71
}
72
}
73
System.exit(1);
74
}
75
}
76
77
public Random ran;
78
public Base64.Encoder encoder;
79
public Base64.Decoder decoder;
80
public int bin_src_len;
81
public int base64_len;
82
public byte[] unencoded;
83
public byte[] encoded;
84
public byte[] decoded;
85
}
86
87
@Benchmark
88
public void decodeMethod(MyState state) {
89
state.decoder.decode(state.encoded, state.decoded);
90
}
91
}
92
93