Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/micro/org/openjdk/bench/java/nio/CharsetEncodeDecode.java
66646 views
1
/*
2
* Copyright (c) 2014, 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.nio;
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.nio.ByteBuffer;
38
import java.nio.CharBuffer;
39
import java.nio.charset.CharacterCodingException;
40
import java.nio.charset.Charset;
41
import java.nio.charset.CharsetDecoder;
42
import java.nio.charset.CharsetEncoder;
43
import java.util.concurrent.TimeUnit;
44
45
/**
46
* This benchmark tests the encode/decode loops on different Charsets. It was created from an adhoc benchmark addressing
47
* a performance issue which in the end boiled down to the encode/decode loops. This is the reason for the values on the
48
* char and byte arrays.
49
*/
50
@BenchmarkMode(Mode.AverageTime)
51
@Warmup(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
52
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
53
@OutputTimeUnit(TimeUnit.MICROSECONDS)
54
@State(Scope.Thread)
55
@Fork(3)
56
public class CharsetEncodeDecode {
57
58
private byte[] BYTES;
59
private char[] CHARS;
60
61
private CharsetEncoder encoder;
62
private CharsetDecoder decoder;
63
64
@Param({"UTF-8", "BIG5", "ISO-8859-15", "ASCII", "UTF-16"})
65
private String type;
66
67
@Param("16384")
68
private int size;
69
70
@Setup
71
public void prepare() {
72
BYTES = new byte[size];
73
CHARS = new char[size];
74
for (int i = 0; i < size; ++i) {
75
int val = 48 + (i % 16);
76
BYTES[i] = (byte) val;
77
CHARS[i] = (char) val;
78
}
79
80
encoder = Charset.forName(type).newEncoder();
81
decoder = Charset.forName(type).newDecoder();
82
}
83
84
@Benchmark
85
public ByteBuffer encode() throws CharacterCodingException {
86
CharBuffer charBuffer = CharBuffer.wrap(CHARS);
87
return encoder.encode(charBuffer);
88
}
89
90
@Benchmark
91
public CharBuffer decode() throws CharacterCodingException {
92
ByteBuffer byteBuffer = ByteBuffer.wrap(BYTES);
93
return decoder.decode(byteBuffer);
94
}
95
96
}
97
98