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