001: /*
002: **********************************************************************
003: * Copyright (c) 2002-2004, International Business Machines
004: * Corporation and others. All Rights Reserved.
005: **********************************************************************
006: */
007: package com.ibm.icu.dev.test.perf;
008:
009: import com.ibm.icu.text.*;
010: import java.io.FileInputStream;
011: import java.io.InputStream;
012: import java.io.InputStreamReader;
013: import java.io.IOException;
014: import java.text.BreakIterator;
015:
016: /**
017: * A class for testing UnicodeSet performance.
018: *
019: * @author Alan Liu
020: * @since ICU 2.4
021: */
022: public class RBBIPerf extends PerfTest {
023:
024: String dataFileName;
025: RuleBasedBreakIterator bi;
026: BreakIterator jdkbi;
027: String testString;
028:
029: public static void main(String[] args) throws Exception {
030: new RBBIPerf().run(args);
031: }
032:
033: protected void setup(String[] args) {
034: // We only take one argument, the pattern
035: if (args.length != 2) {
036: throw new RuntimeException(
037: "RBBITest params: data_file_name break_iterator_type ");
038: }
039:
040: try {
041: dataFileName = args[0];
042: StringBuffer testFileBuf = new StringBuffer();
043: InputStream is = new FileInputStream(dataFileName);
044: InputStreamReader isr = new InputStreamReader(is, "UTF-8");
045: int c;
046: for (;;) {
047: c = isr.read();
048: if (c < 0) {
049: break;
050: }
051: UTF16.append(testFileBuf, c);
052: }
053: testString = testFileBuf.toString();
054: } catch (IOException e) {
055: throw new RuntimeException(e.toString());
056: }
057:
058: if (args.length >= 2) {
059: if (args[1].equals("char")) {
060: bi = (RuleBasedBreakIterator) com.ibm.icu.text.BreakIterator
061: .getCharacterInstance();
062: } else if (args[1].equals("word")) {
063: bi = (RuleBasedBreakIterator) com.ibm.icu.text.BreakIterator
064: .getWordInstance();
065: } else if (args[1].equals("line")) {
066: bi = (RuleBasedBreakIterator) com.ibm.icu.text.BreakIterator
067: .getLineInstance();
068: } else if (args[1].equals("jdkline")) {
069: jdkbi = BreakIterator.getLineInstance();
070: }
071: }
072: if (bi != null) {
073: bi.setText(testString);
074: }
075: if (jdkbi != null) {
076: jdkbi.setText(testString);
077: }
078:
079: }
080:
081: PerfTest.Function testRBBINext() {
082: return new PerfTest.Function() {
083:
084: public void call() {
085: int n;
086: if (bi != null) {
087: n = bi.first();
088: for (; n != BreakIterator.DONE; n = bi.next()) {
089: }
090: } else {
091: n = jdkbi.first();
092: for (; n != BreakIterator.DONE; n = jdkbi.next()) {
093: }
094: }
095: }
096:
097: public long getOperationsPerIteration() {
098: int n;
099: int count = 0;
100: if (bi != null) {
101: for (n = bi.first(); n != BreakIterator.DONE; n = bi
102: .next()) {
103: count++;
104: }
105: } else {
106: for (n = jdkbi.first(); n != BreakIterator.DONE; n = jdkbi
107: .next()) {
108: count++;
109: }
110: }
111: return count;
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
123: .previous()) {
124: }
125: }
126:
127: public long getOperationsPerIteration() {
128: int n;
129: int count = 0;
130: for (n = bi.last(); n != BreakIterator.DONE; n = bi
131: .previous()) {
132: count++;
133: }
134: return count;
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: }
|