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) 2002-2008, International Business Machines Corporation and    *
      6  * others. All Rights Reserved.                                                *
      7  *******************************************************************************
      8  */
      9 package com.ibm.icu.dev.test.perf;
     10 
     11 import java.io.ByteArrayInputStream;
     12 import java.io.ByteArrayOutputStream;
     13 import java.io.FileInputStream;
     14 import java.io.InputStreamReader;
     15 import java.io.OutputStreamWriter;
     16 import java.nio.ByteBuffer;
     17 import java.nio.CharBuffer;
     18 import java.nio.charset.Charset;
     19 import java.nio.charset.CharsetDecoder;
     20 import java.nio.charset.CharsetEncoder;
     21 import java.nio.charset.CodingErrorAction;
     22 
     23 import com.ibm.icu.charset.CharsetProviderICU;
     24 
     25 /**
     26  * @author ram
     27  */
     28 public class ConverterPerformanceTest extends PerfTest {
     29    public static void main(String[] args) throws Exception {
     30        new ConverterPerformanceTest().run(args);
     31    }
     32    char[] unicodeBuffer = null;
     33    byte[] encBuffer = null;
     34 
     35    protected void setup(String[] args) {
     36         try{
     37             // read in the input file, being careful with a possible BOM
     38             FileInputStream in = new FileInputStream(fileName);
     39             BOMFreeReader reader = new BOMFreeReader(in, encoding);
     40             unicodeBuffer = readToEOS(reader);
     41 
     42             // use java.nio to convert unicodeBuffer from char[] to byte[]
     43             CharBuffer source = CharBuffer.wrap(unicodeBuffer, 0, unicodeBuffer.length);
     44             CharsetEncoder encoder = Charset.forName(encoding).newEncoder();
     45             encoder.onMalformedInput(CodingErrorAction.REPORT);
     46             encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
     47             ByteBuffer target = encoder.encode(source);
     48 
     49             // target.array() will probably return what we want, but lets take no chances
     50             encBuffer = new byte[target.limit()];
     51             for (int i=0; i<encBuffer.length; i++)
     52                 encBuffer[i] = target.get(i);
     53 
     54         } catch(Exception ex){
     55             ex.printStackTrace();
     56             throw new RuntimeException(ex.getMessage());
     57         }
     58 
     59         // we created some heavy objects, so lets try to clean up a little before running the tests
     60         gc();
     61    }
     62 
     63    PerfTest.Function TestFromUnicodeStream() {
     64         return new PerfTest.Function() {
     65             public void call() {
     66                 try{
     67                     ByteArrayOutputStream out = new ByteArrayOutputStream(unicodeBuffer.length * 10);
     68                     OutputStreamWriter writer = new OutputStreamWriter(out, testName);
     69                     writer.write(unicodeBuffer, 0, unicodeBuffer.length);
     70                     writer.flush();
     71                 }catch(Exception e){
     72                     e.printStackTrace();
     73                     throw new RuntimeException(e.getMessage());
     74                 }
     75             }
     76             public long getOperationsPerIteration() {
     77                 return unicodeBuffer.length;
     78             }
     79         };
     80     }
     81     PerfTest.Function TestToUnicodeStream() {
     82         return new PerfTest.Function() {
     83             char[] dst = new char[encBuffer.length];
     84             public void call() {
     85                 try{
     86                     ByteArrayInputStream is = new ByteArrayInputStream(encBuffer, 0, encBuffer.length);
     87                     InputStreamReader reader = new InputStreamReader(is, testName);
     88                     reader.read(dst, 0, dst.length);
     89                     reader.close();
     90                 }catch(Exception e){
     91                     e.printStackTrace();
     92                     throw new RuntimeException(e.getMessage());
     93                 }
     94             }
     95             public long getOperationsPerIteration() {
     96                 return encBuffer.length;
     97             }
     98         };
     99     }
    100 /*
    101     PerfTest.Function TestByteToCharConverter() { // decoder  charset.forname().newencoder().decode
    102         try{
    103             return new PerfTest.Function() {
    104                 char[] dst = new char[encBuffer.length];
    105                 int numOut =0;
    106                 ByteToCharConverter conv = ByteToCharConverter.getConverter(testEncoderName);
    107                 int num =0;
    108                 public void call() {
    109                     try{
    110                         numOut= conv.convert(encBuffer, 0, encBuffer.length, dst, 0,dst.length);
    111                         conv.reset();
    112                     }catch(Exception e){
    113                         e.printStackTrace();
    114                         throw new RuntimeException(e.getMessage());
    115                     }
    116                 }
    117                 public long getOperationsPerIteration() {
    118                     return encBuffer.length;
    119                 }
    120             };
    121         }catch(Exception e){
    122             e.printStackTrace();
    123             throw new RuntimeException(e.getMessage());
    124         }
    125     }
    126 
    127     PerfTest.Function TestCharToByteConverter() { // encoder charset.forname().newencoder().encode
    128         try{
    129             return new PerfTest.Function() {
    130                 byte[] dst = new byte[encBuffer.length];
    131                 int numOut =0;
    132                 CharToByteConverter conv = CharToByteConverter.getConverter(testEncoderName);
    133                 int num =0;
    134                 public void call() {
    135                     try{
    136                         numOut= conv.convert(unicodeBuffer, 0,unicodeBuffer.length,dst,0, dst.length);
    137                         conv.reset();
    138                     }catch(Exception e){
    139                         e.printStackTrace();
    140                         throw new RuntimeException(e.getMessage());
    141                     }
    142                 }
    143                 public long getOperationsPerIteration() {
    144                     return unicodeBuffer.length;
    145                 }
    146             };
    147         }catch(Exception e){
    148             e.printStackTrace();
    149             throw new RuntimeException(e.getMessage());
    150         }
    151     }
    152 
    153     PerfTest.Function TestByteToCharConverterICU() { // decoder  charsetprovidericu.getdecoder
    154         try{
    155             return new PerfTest.Function() {
    156                 char[] dst = new char[encBuffer.length];
    157                 int numOut =0;
    158                 ByteToCharConverter conv = ByteToCharConverterICU.createConverter(testEncoderName);
    159                 int num =0;
    160                 public void call() {
    161                     try{
    162                         numOut= conv.convert(encBuffer, 0, encBuffer.length, dst, 0,dst.length);
    163                         conv.reset();
    164                     }catch(Exception e){
    165                         e.printStackTrace();
    166                         throw new RuntimeException(e.getMessage());
    167                     }
    168                 }
    169                 public long getOperationsPerIteration() {
    170                     return encBuffer.length;
    171                 }
    172             };
    173         }catch(Exception e){
    174             e.printStackTrace();
    175             throw new RuntimeException(e.getMessage());
    176         }
    177     }
    178 
    179     PerfTest.Function TestCharToByteConverterICU() {
    180         try{
    181             return new PerfTest.Function() {
    182                 byte[] dst = new byte[encBuffer.length*2];
    183                 int numOut =0;
    184                 CharToByteConverter conv = CharToByteConverterICU.createConverter(testEncoderName);
    185                 int num =0;
    186                 public void call() {
    187                     try{
    188                         numOut= conv.convert(unicodeBuffer, 0,unicodeBuffer.length,dst,0, dst.length);
    189                         conv.reset();
    190                     }catch(Exception e){
    191                         e.printStackTrace();
    192                         throw new RuntimeException(e.getMessage());
    193                     }
    194                 }
    195                 public long getOperationsPerIteration() {
    196                     return unicodeBuffer.length;
    197                 }
    198             };
    199         }catch(Exception e){
    200             e.printStackTrace();
    201             throw new RuntimeException(e.getMessage());
    202         }
    203     }
    204 */
    205     PerfTest.Function TestCharsetDecoder() {
    206         try{
    207             return new PerfTest.Function() {
    208                 CharBuffer outBuf = CharBuffer.allocate(unicodeBuffer.length);
    209                 Charset myCharset = Charset.forName(testName);
    210                 ByteBuffer srcBuf = ByteBuffer.wrap(encBuffer,0,encBuffer.length);
    211                 CharsetDecoder decoder = myCharset.newDecoder();
    212 
    213                 public void call() {
    214                     try{
    215                         decoder.decode(srcBuf,outBuf,false);
    216                         decoder.reset();
    217                         srcBuf.rewind();
    218                         outBuf.rewind();
    219                     }catch(Exception e){
    220                         e.printStackTrace();
    221                         throw new RuntimeException(e.getMessage());
    222                     }
    223                 }
    224                 public long getOperationsPerIteration() {
    225                     return encBuffer.length;
    226                 }
    227             };
    228         }catch(Exception e){
    229             e.printStackTrace();
    230             throw new RuntimeException(e.getMessage());
    231         }
    232     }
    233 
    234     PerfTest.Function TestCharsetEncoder() {
    235         try{
    236             return new PerfTest.Function() {
    237                 ByteBuffer outBuf = ByteBuffer.allocate(encBuffer.length);
    238                 Charset myCharset = Charset.forName(testName);
    239                 CharBuffer srcBuf = CharBuffer.wrap(unicodeBuffer,0,unicodeBuffer.length);
    240                 CharsetEncoder encoder = myCharset.newEncoder();
    241 
    242                 public void call() {
    243                     try{
    244                         encoder.encode(srcBuf,outBuf,false);
    245                         encoder.reset();
    246                         srcBuf.rewind();
    247                         outBuf.rewind();
    248                     }catch(Exception e){
    249                         e.printStackTrace();
    250                         throw new RuntimeException(e.getMessage());
    251                     }
    252                 }
    253                 public long getOperationsPerIteration() {
    254                     return unicodeBuffer.length;
    255                 }
    256             };
    257         }catch(Exception e){
    258             e.printStackTrace();
    259             throw new RuntimeException(e.getMessage());
    260         }
    261     }
    262 
    263     PerfTest.Function TestCharsetDecoderICU() {
    264         try{
    265             return new PerfTest.Function() {
    266                 CharBuffer outBuf = CharBuffer.allocate(unicodeBuffer.length);
    267                 Charset myCharset = new CharsetProviderICU().charsetForName(testName);
    268                 ByteBuffer srcBuf = ByteBuffer.wrap(encBuffer,0,encBuffer.length);
    269                 CharsetDecoder decoder = myCharset.newDecoder();
    270 
    271                 public void call() {
    272                     try{
    273                         decoder.decode(srcBuf,outBuf,false);
    274                         decoder.reset();
    275                         srcBuf.rewind();
    276                         outBuf.rewind();
    277                     }catch(Exception e){
    278                         e.printStackTrace();
    279                         throw new RuntimeException(e.getMessage());
    280                     }
    281                 }
    282                 public long getOperationsPerIteration() {
    283                     return encBuffer.length;
    284                 }
    285             };
    286         }catch(Exception e){
    287             e.printStackTrace();
    288             throw new RuntimeException(e.getMessage());
    289         }
    290     }
    291 
    292     PerfTest.Function TestCharsetEncoderICU() {
    293         try{
    294             return new PerfTest.Function() {
    295                 ByteBuffer outBuf = ByteBuffer.allocate(encBuffer.length);
    296                 Charset myCharset = new CharsetProviderICU().charsetForName(testName);
    297                 CharBuffer srcBuf = CharBuffer.wrap(unicodeBuffer,0,unicodeBuffer.length);
    298                 CharsetEncoder encoder = myCharset.newEncoder();
    299 
    300                 public void call() {
    301                     try{
    302                         encoder.encode(srcBuf,outBuf,false);
    303                         encoder.reset();
    304                         srcBuf.rewind();
    305                         outBuf.rewind();
    306                     }catch(Exception e){
    307                         e.printStackTrace();
    308                         throw new RuntimeException(e.getMessage());
    309                     }
    310                 }
    311                 public long getOperationsPerIteration() {
    312                     return unicodeBuffer.length;
    313                 }
    314             };
    315         }catch(Exception e){
    316             e.printStackTrace();
    317             throw new RuntimeException(e.getMessage());
    318         }
    319     }
    320 }
    321