Path: blob/master/test/micro/org/openjdk/bench/java/lang/StringEncode.java
41161 views
/*1* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package org.openjdk.bench.java.lang;2324import org.openjdk.jmh.annotations.*;25import org.openjdk.jmh.infra.Blackhole;2627import java.nio.charset.Charset;28import java.util.concurrent.TimeUnit;2930@BenchmarkMode(Mode.AverageTime)31@OutputTimeUnit(TimeUnit.NANOSECONDS)32@Fork(value = 3, jvmArgs = "-Xmx1g")33@Warmup(iterations = 5, time = 2)34@Measurement(iterations = 5, time = 3)35@State(Scope.Thread)36public class StringEncode {3738@BenchmarkMode(Mode.AverageTime)39@OutputTimeUnit(TimeUnit.NANOSECONDS)40@Fork(value = 3, jvmArgs = "-Xmx1g")41@Warmup(iterations = 5, time = 2)42@Measurement(iterations = 5, time = 2)43@State(Scope.Thread)44public static class WithCharset {4546@Param({"US-ASCII", "ISO-8859-1", "UTF-8", "MS932", "ISO-8859-6"})47private String charsetName;4849private Charset charset;50private String asciiString;51private String utf16String;5253@Setup54public void setup() {55charset = Charset.forName(charsetName);56asciiString = "ascii string";57utf16String = "UTF-\uFF11\uFF16 string";58}5960@Benchmark61public void encodeCharsetName(Blackhole bh) throws Exception {62bh.consume(asciiString.getBytes(charsetName));63bh.consume(utf16String.getBytes(charsetName));64}6566@Benchmark67public void encodeCharset(Blackhole bh) throws Exception {68bh.consume(asciiString.getBytes(charset));69bh.consume(utf16String.getBytes(charset));70}71}7273private String asciiDefaultString;74private String utf16DefaultString;7576@Setup77public void setup() {78asciiDefaultString = "ascii string";79utf16DefaultString = "UTF-\uFF11\uFF16 string";80}8182@Benchmark83public void encodeDefault(Blackhole bh) throws Exception {84bh.consume(asciiDefaultString.getBytes());85bh.consume(utf16DefaultString.getBytes());86}87}888990