Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/io/ByteStreamDecoder.java
41161 views
1
/*
2
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. 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
package org.openjdk.bench.java.io;
24
25
import org.openjdk.jmh.annotations.Benchmark;
26
import org.openjdk.jmh.annotations.BenchmarkMode;
27
import org.openjdk.jmh.annotations.Fork;
28
import org.openjdk.jmh.annotations.Measurement;
29
import org.openjdk.jmh.annotations.Mode;
30
import org.openjdk.jmh.annotations.OutputTimeUnit;
31
import org.openjdk.jmh.annotations.Param;
32
import org.openjdk.jmh.annotations.Scope;
33
import org.openjdk.jmh.annotations.Setup;
34
import org.openjdk.jmh.annotations.State;
35
import org.openjdk.jmh.annotations.Warmup;
36
37
import java.io.ByteArrayInputStream;
38
import java.io.IOException;
39
import java.io.InputStreamReader;
40
import java.nio.charset.Charset;
41
import java.util.concurrent.TimeUnit;
42
43
/**
44
* Tests the overheads of reading encoded byte arrays via StreamDecoder
45
*/
46
@BenchmarkMode(Mode.AverageTime)
47
@OutputTimeUnit(TimeUnit.NANOSECONDS)
48
@State(Scope.Thread)
49
@Warmup(time=2, iterations=5)
50
@Measurement(time=3, iterations=5)
51
@Fork(value=2, jvmArgs="-Xmx1g")
52
public class ByteStreamDecoder {
53
54
@Param({"US-ASCII", "ISO-8859-1", "UTF-8", "ISO-8859-6", "MS932"})
55
private String charsetName;
56
57
@Param({"256", "4096", "25000"})
58
private int length;
59
60
private byte[] bytes;
61
62
private byte[] nonASCIIBytesEnd;
63
64
private byte[] nonASCIIBytesStart;
65
66
private byte[] nonASCIIBytesEveryOther;
67
68
private char[] chars;
69
70
private Charset cs;
71
72
@Setup
73
public void setup() throws IOException {
74
var s = """
75
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non
76
magna augue. Sed tristique ante id maximus interdum. Suspendisse
77
potenti. Aliquam molestie metus vitae magna gravida egestas.
78
Phasellus eleifend tortor sit amet neque euismod, vitae luctus
79
ante viverra. Sed quis justo ultrices, eleifend dui sed, egestas
80
lorem. Mauris ipsum ex, interdum eu turpis sed, fermentum efficitur
81
lorem. Sed vel imperdiet libero, eget ullamcorper sem. Praesent
82
gravida arcu quis ipsum viverra tristique. Quisque maximus
83
elit nec nisi vulputate tempor. Integer aliquet tortor vel
84
vehicula efficitur. Sed neque felis, ultricies eu leo ultricies,
85
egestas placerat dolor. Etiam iaculis magna quis lacinia
86
tincidunt. Donec in tellus volutpat, semper nunc ornare,
87
tempus erat. Donec volutpat mauris in arcu mattis sollicitudin.
88
Morbi vestibulum ipsum sed erat porta, mollis commodo nisi
89
gravida.
90
""";
91
int n = length / s.length();
92
String full = "";
93
if (n > 0) {
94
full = s.repeat(n);
95
}
96
n = length % s.length();
97
if (n > 0) {
98
full += s.substring(0, n);
99
}
100
cs = Charset.forName(charsetName);
101
bytes = full.getBytes(cs);
102
nonASCIIBytesEnd = (full + "\u00e5").getBytes(cs);
103
nonASCIIBytesStart = ("\u00e5" + full).getBytes(cs);
104
105
// string hostile to ASCII fast-path optimizations: every other char is ASCII
106
StringBuilder sb = new StringBuilder();
107
for (int i = 0; i < full.length(); i++) {
108
sb.append(i % 2 == 0 ? full.charAt(i) : '\u00e5');
109
}
110
nonASCIIBytesEveryOther = sb.toString().getBytes(cs);
111
chars = new char[full.length() + 2];
112
113
try {
114
if (!readStringDirect().equals(readStringReader())) {
115
System.out.println("direct: " + readStringDirect());
116
System.out.println("reader: " + readStringReader());
117
throw new RuntimeException("Unexpectedly different");
118
}
119
if (!readStringDirect_NonASCIIEnd().equals(readStringReader_NonASCIIEnd())) {
120
throw new RuntimeException("Unexpectedly different");
121
}
122
if (!readStringDirect_NonASCIIStart().equals(readStringReader_NonASCIIStart())) {
123
throw new RuntimeException("Unexpectedly different");
124
}
125
if (!readStringDirect_NonASCIIEveryOther().equals(readStringReader_NonASCIIEveryOther())) {
126
throw new RuntimeException("Unexpectedly different");
127
}
128
} catch (Exception e) {
129
e.printStackTrace();
130
}
131
}
132
133
@Benchmark
134
public String readStringReader() throws Exception {
135
int len = new InputStreamReader(new ByteArrayInputStream(bytes), cs).read(chars);
136
return new String(chars, 0, len);
137
}
138
139
@Benchmark
140
public String readStringReader_NonASCIIEnd() throws Exception {
141
int len = new InputStreamReader(new ByteArrayInputStream(nonASCIIBytesEnd), cs).read(chars);
142
return new String(chars, 0, len);
143
}
144
145
@Benchmark
146
public String readStringReader_NonASCIIStart() throws Exception {
147
int len = new InputStreamReader(new ByteArrayInputStream(nonASCIIBytesStart), cs).read(chars);
148
return new String(chars, 0, len);
149
}
150
151
@Benchmark
152
public String readStringReader_NonASCIIEveryOther() throws Exception {
153
int len = new InputStreamReader(new ByteArrayInputStream(nonASCIIBytesEveryOther), cs).read(chars);
154
return new String(chars, 0, len);
155
}
156
157
@Benchmark
158
public String readStringDirect() throws Exception {
159
return new String(bytes, cs);
160
}
161
162
@Benchmark
163
public String readStringDirect_NonASCIIEnd() throws Exception {
164
return new String(nonASCIIBytesEnd, cs);
165
}
166
167
@Benchmark
168
public String readStringDirect_NonASCIIStart() throws Exception {
169
return new String(nonASCIIBytesStart, cs);
170
}
171
172
@Benchmark
173
public String readStringDirect_NonASCIIEveryOther() throws Exception {
174
return new String(nonASCIIBytesEveryOther, cs);
175
}
176
}
177
178