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