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