1 /*
   2  * Copyright (c) 2009, 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 
  24 import java.util.*;
  25 import java.nio.*;
  26 import java.nio.charset.*;
  27 import java.util.concurrent.*;
  28 import java.util.regex.Pattern;
  29 
  30 public class StrCodingBenchmarkDB extends StrCodingBenchmark {
  31 
  32 
  33     public static void main(String[] args) throws Throwable {
  34         final int itrs = Integer.getInteger("iterations", 10000);
  35         //final int itrs = Integer.getInteger("iterations", 12);
  36         final int size       = Integer.getInteger("size", 2048);
  37         final int subsize    = Integer.getInteger("subsize", 128);
  38         final int maxchar    = Integer.getInteger("maxchar", 128);
  39         final String regex = System.getProperty("filter");
  40         final Pattern filter = (regex == null) ? null : Pattern.compile(regex);
  41         final boolean useSecurityManager = Boolean.getBoolean("SecurityManager");
  42         if (useSecurityManager)
  43             System.setSecurityManager(new PermissiveSecurityManger());
  44         final Random rnd = new Random();
  45 
  46         String[] csns = new String[] {
  47             "Big5",
  48             "Johab",
  49             "EUC_CN",
  50             "EUC_KR",
  51             "MS932",
  52             "MS936",
  53             "MS949",
  54             "MS950",
  55             "GBK",
  56             "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         };
  76 
  77         ArrayList<long[]> sum = new ArrayList<>();
  78 
  79         for (final String csn : csns) {
  80             final Charset cs = Charset.forName(csn);
  81             List<Integer> cps = new ArrayList<>(0x4000);
  82             int off = 0;
  83             int cp = 0;
  84             int n = 0; 
  85             CharsetEncoder enc = cs.newEncoder();
  86             while (cp < 0x10000 && n < cps.size()) {
  87                 if (enc.canEncode((char)cp)) {
  88                     cps.add(cp);
  89                     n++;
  90                 }
  91                 cp++;
  92             }
  93             Collections.shuffle(cps);
  94             char[] ca = new char[cps.size()];
  95             for (int i = 0; i < cps.size(); i++)
  96                 ca[i] = (char)(int)cps.get(i);
  97 
  98 
  99             System.out.printf("%n--------%s---------%n", csn);
 100             for (int sz = 8; sz <= 2048; sz *= 2) {
 101                 System.out.printf("   [len=%d]%n", sz);
 102 
 103                 final char[] chars  = Arrays.copyOf(ca, sz);
 104                 final String str = new String(chars);
 105                 final byte[] bs  = str.getBytes(cs);
 106 
 107                 Job[] jobs = {
 108 
 109                     new Job("String decode: csn") {
 110                     public void work() throws Throwable {
 111                         for (int i = 0; i < itrs; i++)
 112                             new String(bs, csn);
 113                     }},
 114 
 115                     new Job("String decode: cs") {
 116                     public void work() throws Throwable {
 117                         for (int i = 0; i < itrs; i++)
 118                             new String(bs, cs);
 119                     }},
 120 
 121                     new Job("String encode: csn") {
 122                     public void work() throws Throwable {
 123                         for (int i = 0; i < itrs; i++)
 124                             str.getBytes(csn);
 125                     }},
 126 
 127                     new Job("String encode: cs") {
 128                     public void work() throws Throwable {
 129                         for (int i = 0; i < itrs; i++)
 130                             str.getBytes(cs);
 131                     }},
 132                 };
 133                 sum.add(time(jobs));
 134                 
 135             }
 136         }
 137     }
 138 }