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-2004, International Business Machines
      6 * Corporation and others.  All Rights Reserved.
      7 **********************************************************************
      8 */
      9 package com.ibm.icu.dev.test.perf;
     10 import com.ibm.icu.text.*;
     11 import java.io.FileInputStream;
     12 import java.io.InputStream;
     13 import java.io.InputStreamReader;
     14 import java.io.IOException;
     15 import java.text.BreakIterator;
     16 
     17 /**
     18  * A class for testing UnicodeSet performance.
     19  *
     20  * @author Alan Liu
     21  * @since ICU 2.4
     22  */
     23 public class RBBIPerf extends PerfTest {
     24 
     25     String                  dataFileName;
     26     RuleBasedBreakIterator  bi;
     27     BreakIterator           jdkbi;
     28     String                  testString;
     29 
     30     public static void main(String[] args) throws Exception {
     31         new RBBIPerf().run(args);
     32     }
     33 
     34     protected void setup(String[] args) {
     35         // We only take one argument, the pattern
     36         if (args.length != 2) {
     37             throw new RuntimeException("RBBITest params:  data_file_name break_iterator_type ");
     38         }
     39 
     40         try {
     41             dataFileName = args[0];
     42             StringBuffer  testFileBuf = new StringBuffer();
     43             InputStream is = new FileInputStream(dataFileName);
     44             InputStreamReader isr = new InputStreamReader(is, "UTF-8");
     45             int c;
     46             for (;;) {
     47                 c = isr.read();
     48                 if (c < 0) {
     49                     break;
     50                 }
     51                 UTF16.append(testFileBuf, c);
     52             }
     53             testString = testFileBuf.toString();
     54         }
     55         catch (IOException e) {
     56             throw new RuntimeException(e.toString());
     57         }
     58 
     59         if (args.length >= 2) {
     60             if (args[1].equals("char")) {
     61                 bi  = (RuleBasedBreakIterator)com.ibm.icu.text.BreakIterator.getCharacterInstance();
     62             } else if (args[1].equals("word")) {
     63                 bi  = (RuleBasedBreakIterator)com.ibm.icu.text.BreakIterator.getWordInstance();
     64             } else if (args[1].equals("line")) {
     65                 bi  = (RuleBasedBreakIterator)com.ibm.icu.text.BreakIterator.getLineInstance();
     66             } else if (args[1].equals("jdkline")) {
     67                 jdkbi  = BreakIterator.getLineInstance();
     68             }
     69         }
     70         if (bi!=null ) {
     71             bi.setText(testString);
     72         }
     73         if (jdkbi != null) {
     74             jdkbi.setText(testString);
     75         }
     76 
     77     }
     78 
     79 
     80 
     81     PerfTest.Function testRBBINext() {
     82         return new PerfTest.Function() {
     83 
     84             public void call() {
     85                 int n;
     86                 if (bi != null) {
     87                     n = bi.first();
     88                     for (; n != BreakIterator.DONE; n=bi.next()) {
     89                     }
     90                 } else {
     91                     n = jdkbi.first();
     92                     for (; n != BreakIterator.DONE; n=jdkbi.next()) {
     93                     }
     94                 }
     95             }
     96 
     97 
     98             public long getOperationsPerIteration() {
     99                 int n;
    100                 int count = 0;
    101                 if (bi != null) {
    102                     for (n=bi.first(); n != BreakIterator.DONE; n=bi.next()) {
    103                         count++;
    104                     }
    105                 } else {
    106                     for (n=jdkbi.first(); n != BreakIterator.DONE; n=jdkbi.next()) {
    107                         count++;
    108                     }
    109                 }
    110                 return count;
    111             }
    112         };
    113     }
    114 
    115 
    116     PerfTest.Function testRBBIPrevious() {
    117         return new PerfTest.Function() {
    118 
    119             public void call() {
    120                 bi.first();
    121                 int n=0;
    122                 for (n=bi.last(); n != BreakIterator.DONE; n=bi.previous()) {
    123                 }
    124             }
    125 
    126 
    127             public long getOperationsPerIteration() {
    128                 int n;
    129                 int count = 0;
    130                 for (n=bi.last(); n != BreakIterator.DONE; n=bi.previous()) {
    131                     count++;
    132                 }
    133                 return count;
    134             }
    135         };
    136     }
    137 
    138 
    139     PerfTest.Function testRBBIIsBoundary() {
    140         return new PerfTest.Function() {
    141 
    142             public void call() {
    143                 int n=testString.length();
    144                 int i;
    145                 for (i=0; i<n; i++) {
    146                     bi.isBoundary(i);
    147                 }
    148             }
    149 
    150             public long getOperationsPerIteration() {
    151                 int n = testString.length();
    152                 return n;
    153             }
    154         };
    155     }
    156 
    157 
    158 
    159 }
    160