Home | History | Annotate | Download | only in perf
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html#License
      3 /*
      4 **********************************************************************
      5 * Copyright (c) 2006-2008, International Business Machines
      6 * Corporation and others.  All Rights Reserved.
      7 **********************************************************************
      8 */
      9 
     10 package com.ibm.icu.dev.test.perf;
     11 
     12 import java.nio.ByteBuffer;
     13 import java.util.ResourceBundle;
     14 
     15 import com.ibm.icu.util.UResourceBundle;
     16 
     17 public class ResourceBundlePerf extends PerfTest {
     18 
     19     private UResourceBundle icuRes  = null;
     20     private ResourceBundle javaRes = null;
     21 
     22     public static void main(String[] org_args) throws Exception {
     23       new ResourceBundlePerf().run(org_args);
     24     }
     25 
     26     protected void setup(String[] args) {
     27         icuRes = UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "testtypes");
     28         javaRes = ResourceBundle.getBundle("com.ibm.icu.dev.data.TestDataElements_testtypes");
     29     }
     30 
     31     PerfTest.Function TestResourceBundleConstructionJava() {
     32         return new PerfTest.Function() {
     33             public void call() {
     34                 javaRes = ResourceBundle.getBundle("com.ibm.icu.dev.data.TestDataElements_testtypes");
     35             }
     36         };
     37     }
     38     PerfTest.Function TestResourceBundleConstructionICU() {
     39         return new PerfTest.Function() {
     40             public void call() {
     41                 UResourceBundle.getBundleInstance("com/ibm/icu/dev/data/testdata", "testtypes");
     42             }
     43         };
     44     }
     45 
     46     PerfTest.Function TestEmptyArrayJava() {
     47         return new PerfTest.Function(){
     48             public void call(){
     49                 String[] s = javaRes.getStringArray("emptyarray");
     50                 if (s.length != 0) throw new Error ("not zero");
     51             }
     52         };
     53     }
     54 
     55     PerfTest.Function TestEmptyArrayICU() {
     56         return new PerfTest.Function(){
     57             public void call(){
     58                 String[] s = icuRes.getStringArray("emptyarray");
     59                 if (s.length != 0) throw new Error ("not zero");
     60             }
     61         };
     62     }
     63 
     64     class GetStringJava extends PerfTest.Function {
     65         String key;
     66         String expected;
     67         GetStringJava(String key, String expected) {
     68             this.key = key;
     69             this.expected = expected;
     70         }
     71         public void call() {
     72             String s = javaRes.getString(key);
     73             if (!s.equals(expected)) throw new Error("not equal");
     74         }
     75     }
     76 
     77     class GetStringIcu extends PerfTest.Function {
     78         String key;
     79         String expected;
     80         GetStringIcu(String key, String expected) {
     81             this.key = key;
     82             this.expected = expected;
     83         }
     84         public void call() {
     85             String s = icuRes.getString(key);
     86             if (!s.equals(expected)) throw new Error("not equal");
     87         }
     88     }
     89 
     90     PerfTest.Function TestZeroTestJava(){
     91         return new GetStringJava("zerotest", "abc\u0000def");
     92     }
     93 
     94     PerfTest.Function TestZeroTestICU(){
     95         return new GetStringIcu("zerotest", "abc\u0000def");
     96     }
     97 
     98     PerfTest.Function TestEmptyExplicitStringJava(){
     99         return new GetStringJava("emptyexplicitstring", "");
    100     }
    101 
    102     PerfTest.Function TestEmptyExplicitStringICU(){
    103         return new GetStringIcu("emptyexplicitstring", "");
    104     }
    105 
    106     PerfTest.Function TestEmptyStringJava(){
    107         return new GetStringJava("emptystring", "");
    108     }
    109 
    110     PerfTest.Function TestEmptyStringICU(){
    111         return new GetStringIcu("emptystring", "");
    112     }
    113 
    114     class GetIntJava extends PerfTest.Function {
    115         String key;
    116         int expected;
    117         GetIntJava(String key, int expected) {
    118             this.key = key;
    119             this.expected = expected;
    120         }
    121         public void call() {
    122             Integer t = (Integer) javaRes.getObject(key);
    123             if (t.intValue() != expected) throw new Error("not equal");
    124         }
    125     }
    126 
    127     class GetIntIcu extends PerfTest.Function {
    128         String key;
    129         int expected;
    130         GetIntIcu(String key, int expected) {
    131             this.key = key;
    132             this.expected = expected;
    133         }
    134         public void call() {
    135             UResourceBundle temp = icuRes.get(key);
    136             int t = temp.getInt();
    137             if (t != expected) throw new Error("not equal");
    138         }
    139     }
    140 
    141     PerfTest.Function TestGet123Java(){
    142         return new GetIntJava("onehundredtwentythree", 123);
    143     }
    144 
    145     PerfTest.Function TestGet123ICU(){
    146         return new GetIntIcu("onehundredtwentythree", 123);
    147     }
    148 
    149     PerfTest.Function TestGetEmptyIntJava(){
    150         return new GetIntJava("emptyint", 0);
    151     }
    152 
    153     PerfTest.Function TestGetEmptyIntICU(){
    154         return new GetIntIcu("emptyint", 0);
    155     }
    156 
    157     PerfTest.Function TestGetOneJava(){
    158         return new GetIntJava("one", 1);
    159     }
    160 
    161     PerfTest.Function TestGetOneICU(){
    162         return new GetIntIcu("one", 1);
    163     }
    164 
    165     PerfTest.Function TestGetMinusOneJava(){
    166         return new GetIntJava("minusone", -1);
    167     }
    168 
    169     PerfTest.Function TestGetMinusOneICU(){
    170         return new GetIntIcu("minusone", -1);
    171     }
    172 
    173     PerfTest.Function TestGetPlusOneJava(){
    174         return new GetIntJava("plusone", 1);
    175     }
    176 
    177     PerfTest.Function TestGetPlusOneICU(){
    178         return new GetIntIcu("plusone", 1);
    179     }
    180 
    181     PerfTest.Function TestGetMinusOneUintJava(){ // TODO: no equivalence?
    182         return new PerfTest.Function(){
    183             public void call(){
    184                 Integer t = (Integer) javaRes.getObject("minusone");
    185                 if (t.intValue() != -1 ) throw new Error("not equal");
    186             }
    187         };
    188     }
    189 
    190     PerfTest.Function TestGetMinusOneUintICU(){
    191         return new PerfTest.Function(){
    192             public void call(){
    193                 UResourceBundle sub = icuRes.get("minusone");
    194                 int t = sub.getUInt();
    195                 if (t != 0xFFFFFFF) throw new Error("not equal");
    196             }
    197         };
    198     }
    199 
    200 
    201     class GetIvJava  extends PerfTest.Function {
    202         String key;
    203         int[] expected;
    204         GetIvJava(String key, int[] expected) {
    205             this.key = key;
    206             this.expected = expected;
    207         }
    208         public void call() {
    209             Integer[] iv = (Integer[]) javaRes.getObject(key);
    210             for (int i = 0; i < iv.length; i++){
    211                 if (expected[i] != iv[i].intValue()) throw new Error("not equal");
    212             }
    213         }
    214     }
    215 
    216     class GetIvIcu  extends PerfTest.Function {
    217         String key;
    218         int[] expected;
    219         GetIvIcu(String key, int[] expected) {
    220             this.key = key;
    221             this.expected = expected;
    222         }
    223         public void call() {
    224             UResourceBundle temp = icuRes.get(key);
    225             int[] iv = temp.getIntVector();
    226             for (int i = 0; i < iv.length; i++){
    227                 if (expected[i] != iv[i]) throw new Error("not equal");
    228             }
    229         }
    230     }
    231 
    232     PerfTest.Function TestGetIntegerArrayJava(){
    233         return new GetIvJava("integerarray", new int[]{1,2,3,-3,4,5,6,7});
    234     }
    235 
    236     PerfTest.Function TestGetIntegerArrayICU(){
    237         return new GetIvIcu("integerarray", new int[]{1,2,3,-3,4,5,6,7});
    238     }
    239 
    240     PerfTest.Function TestGetEmptyIntegerArrayJava(){
    241         return new GetIvJava("emptyintv", new int[0]);
    242     }
    243 
    244     PerfTest.Function TestGetEmptyIntegerArrayICU(){
    245         return new GetIvIcu("emptyintv", new int[0]);
    246     }
    247 
    248 
    249     class GetBinaryIcu extends PerfTest.Function {
    250         String key;
    251         int expected_len;
    252         GetBinaryIcu(String key, int expected_len) {
    253             this.key = key;
    254             this.expected_len = expected_len;
    255         }
    256         public void call() {
    257             UResourceBundle temp = icuRes.get(key);
    258             ByteBuffer got = temp.getBinary();
    259             if(got.remaining() != expected_len) throw new Error("not the expected len");
    260             for(int i=0; i< got.remaining(); i++){
    261               byte b = got.get();
    262               if (i != b) throw new Error("not equal");
    263             }
    264         }
    265     }
    266 
    267     class GetBinaryJava extends PerfTest.Function {
    268         String key;
    269         int expected_len;
    270         GetBinaryJava(String key, int expected_len) {
    271             this.key = key;
    272             this.expected_len = expected_len;
    273         }
    274         public void call() {
    275             ByteBuffer got = ByteBuffer.wrap((byte[])javaRes.getObject(key));
    276             if(got.remaining() != expected_len) throw new Error("not the expected len");
    277             for(int i=0; i< got.remaining(); i++){
    278               byte b = got.get();
    279               if (i != b) throw new Error("not equal");
    280             }
    281         }
    282     }
    283 
    284     PerfTest.Function TestGetBinaryTestICU(){
    285         return new GetBinaryIcu("binarytest", 15);
    286     }
    287 
    288     PerfTest.Function TestGetBinaryTestJava(){
    289         return new GetBinaryJava("binarytest", 15);
    290     }
    291 
    292     PerfTest.Function TestGetEmptyBinaryICU(){
    293         return new GetBinaryIcu("emptybin", 0);
    294     }
    295 
    296     PerfTest.Function TestGetEmptyBinaryJava(){
    297         return new GetBinaryJava("emptybin", 0);
    298     }
    299 
    300     class GetMenuJava extends PerfTest.Function {
    301         String key;
    302         String[] expected;
    303         GetMenuJava(String key, String[] expected) {
    304             this.key = key;
    305             this.expected = expected;
    306         }
    307         public void call() {
    308             int p = 0;
    309             Object[][] menus = (Object[][]) javaRes.getObject(key);
    310             int sizei = menus.length;
    311             for (int i=0; i<sizei; i++){
    312                 String menu_name = (String) menus[i][0];
    313                 Object[][] menu_items = (Object[][]) menus[i][1];
    314                 if (!expected[p++].equals(menu_name)) throw new Error("not equal");
    315 
    316                 int sizej = menu_items.length;
    317                 for (int j=0; j< sizej; j++){
    318                     String itemKey = (String) menu_items[j][0];
    319                     String value = (String) menu_items[j][1];
    320                     if(!expected[p++].equals(itemKey)) throw new Error("not equal");
    321                     if(!expected[p++].equals(value)) throw new Error("not equal");
    322                 }
    323             }
    324 
    325         }
    326     }
    327 
    328     class GetMenuIcu extends PerfTest.Function {
    329         String key;
    330         String[] expected;
    331         GetMenuIcu(String key, String[] expected) {
    332             this.key = key;
    333             this.expected = expected;
    334         }
    335         public void call() {
    336             int p = 0;
    337             UResourceBundle menus = icuRes.get(key);
    338             int sizei = menus.getSize();
    339             for (int i=0; i<sizei; i++){
    340                 UResourceBundle menu = menus.get(i);
    341                 String menu_name = menu.getKey();
    342                 if (!expected[p++].equals(menu_name)) throw new Error("not equal");
    343 
    344                 int sizej = menu.getSize();
    345                 for (int j=0; j< sizej; j++){
    346                     UResourceBundle menu_item = menu.get(j);
    347                     String itemKey = menu_item.getKey();
    348                     String value = menu_item.getString();
    349                     if(!expected[p++].equals(itemKey)) throw new Error("not equal");
    350                     if(!expected[p++].equals(value)) throw new Error("not equal");
    351                 }
    352             }
    353 
    354         }
    355     }
    356 
    357     PerfTest.Function TestGetMenuJava(){
    358         String[] expected = new String[]{"file", "exit", "Exit", "open", "Open", "save", "Save"};
    359         return new GetMenuJava("menu", expected);
    360     }
    361 
    362     PerfTest.Function TestGetMenuICU(){
    363         String[] expected = new String[]{"file", "exit", "Exit", "open", "Open", "save", "Save"};
    364         return new GetMenuIcu("menu", expected);
    365     }
    366 
    367     PerfTest.Function TestGetEmptyMenuJava(){
    368         return new GetMenuJava("emptytable", new String[]{});
    369     }
    370 
    371     PerfTest.Function TestGetEmptyMenuICU(){
    372         return new GetMenuIcu("emptytable", new String[]{});
    373     }
    374 }
    375