Path: blob/master/test/jdk/sun/nio/cs/StrCodingBenchmarkDB.java
41152 views
/*1* Copyright (c) 2009, 2012, 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*/2223import java.util.*;24import java.nio.*;25import java.nio.charset.*;26import java.util.concurrent.*;27import java.util.regex.Pattern;2829public class StrCodingBenchmarkDB extends StrCodingBenchmark {303132public static void main(String[] args) throws Throwable {33final int itrs = Integer.getInteger("iterations", 100000);34//final int itrs = Integer.getInteger("iterations", 12);35final int size = Integer.getInteger("size", 2048);36final int subsize = Integer.getInteger("subsize", 128);37final int maxchar = Integer.getInteger("maxchar", 128);38final String regex = System.getProperty("filter");39final Pattern filter = (regex == null) ? null : Pattern.compile(regex);40final boolean useSecurityManager = Boolean.getBoolean("SecurityManager");41if (useSecurityManager)42System.setSecurityManager(new PermissiveSecurityManger());43final Random rnd = new Random();4445String[] csns = new String[] {46"Big5",47"Johab",48"EUC_CN",49"EUC_KR",50"MS932",51"MS936",52"MS949",53"MS950",54"GBK",5556"Big5_HKSCS",57"Big5_HKSCS_2001",58"Big5_Solaris",59"MS950_HKSCS",60"MS950_HKSCS_XP",61"IBM1364",62"IBM1381",63"IBM1383",64"IBM930",65"IBM933",66"IBM935",67"IBM937",68"IBM939",69"IBM942",70"IBM943",71"IBM948",72"IBM949",73"IBM950",74"IBM970",75};7677ArrayList<long[]> sum = new ArrayList<>();7879for (final String csn : csns) {80final Charset cs = Charset.forName(csn);81List<Integer> cps = new ArrayList<>(0x4000);82int off = 0;83int cp = 0;84int n = 0;85CharsetEncoder enc = cs.newEncoder();86while (cp < 0x10000 && n < cps.size()) {87if (enc.canEncode((char)cp)) {88cps.add(cp);89n++;90}91cp++;92}93Collections.shuffle(cps);94char[] ca = new char[cps.size()];95for (int i = 0; i < cps.size(); i++)96ca[i] = (char)(int)cps.get(i);979899System.out.printf("%n--------%s---------%n", csn);100for (int sz = 8; sz <= 2048; sz *= 2) {101System.out.printf(" [len=%d]%n", sz);102103final char[] chars = Arrays.copyOf(ca, sz);104final String str = new String(chars);105final byte[] bs = str.getBytes(cs);106107Job[] jobs = {108109new Job("String decode: csn") {110public void work() throws Throwable {111for (int i = 0; i < itrs; i++)112new String(bs, csn);113}},114115new Job("String decode: cs") {116public void work() throws Throwable {117for (int i = 0; i < itrs; i++)118new String(bs, cs);119}},120121new Job("String encode: csn") {122public void work() throws Throwable {123for (int i = 0; i < itrs; i++)124str.getBytes(csn);125}},126127new Job("String encode: cs") {128public void work() throws Throwable {129for (int i = 0; i < itrs; i++)130str.getBytes(cs);131}},132};133sum.add(time(jobs));134135}136}137}138}139140141