1 /*
2  * Copyright (c) 2009, 2012, 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 
main(String[] args)33     public static void main(String[] args) throws Throwable {
34         final int itrs = Integer.getInteger("iterations", 100000);
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 
57             "Big5_HKSCS",
58             "Big5_HKSCS_2001",
59             "Big5_Solaris",
60             "MS950_HKSCS",
61             "MS950_HKSCS_XP",
62             "IBM1364",
63             "IBM1381",
64             "IBM1383",
65             "IBM930",
66             "IBM933",
67             "IBM935",
68             "IBM937",
69             "IBM939",
70             "IBM942",
71             "IBM943",
72             "IBM948",
73             "IBM949",
74             "IBM950",
75             "IBM970",
76         };
77 
78         ArrayList<long[]> sum = new ArrayList<>();
79 
80         for (final String csn : csns) {
81             final Charset cs = Charset.forName(csn);
82             List<Integer> cps = new ArrayList<>(0x4000);
83             int off = 0;
84             int cp = 0;
85             int n = 0;
86             CharsetEncoder enc = cs.newEncoder();
87             while (cp < 0x10000 && n < cps.size()) {
88                 if (enc.canEncode((char)cp)) {
89                     cps.add(cp);
90                     n++;
91                 }
92                 cp++;
93             }
94             Collections.shuffle(cps);
95             char[] ca = new char[cps.size()];
96             for (int i = 0; i < cps.size(); i++)
97                 ca[i] = (char)(int)cps.get(i);
98 
99 
100             System.out.printf("%n--------%s---------%n", csn);
101             for (int sz = 8; sz <= 2048; sz *= 2) {
102                 System.out.printf("   [len=%d]%n", sz);
103 
104                 final char[] chars  = Arrays.copyOf(ca, sz);
105                 final String str = new String(chars);
106                 final byte[] bs  = str.getBytes(cs);
107 
108                 Job[] jobs = {
109 
110                     new Job("String decode: csn") {
111                     public void work() throws Throwable {
112                         for (int i = 0; i < itrs; i++)
113                             new String(bs, csn);
114                     }},
115 
116                     new Job("String decode: cs") {
117                     public void work() throws Throwable {
118                         for (int i = 0; i < itrs; i++)
119                             new String(bs, cs);
120                     }},
121 
122                     new Job("String encode: csn") {
123                     public void work() throws Throwable {
124                         for (int i = 0; i < itrs; i++)
125                             str.getBytes(csn);
126                     }},
127 
128                     new Job("String encode: cs") {
129                     public void work() throws Throwable {
130                         for (int i = 0; i < itrs; i++)
131                             str.getBytes(cs);
132                     }},
133                 };
134                 sum.add(time(jobs));
135 
136             }
137         }
138     }
139 }
140