]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/perf/ubrkperf/ubrkperf.cpp
ICU-6.2.4.tar.gz
[apple/icu.git] / icuSources / test / perf / ubrkperf / ubrkperf.cpp
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (C) 2001 IBM, Inc. All Rights Reserved.
4 *
5 ********************************************************************/
6 /********************************************************************************
7 *
8 * File ubrkperf.cpp
9 *
10 * Modification History:
11 * Name Description
12 * Vladimir Weinstein First Version, based on collperf
13 *
14 *********************************************************************************
15 */
16
17 #include "ubrkperf.h"
18
19
20 #if 0
21 #ifdef U_DARWIN
22 #include <ApplicationServices/ApplicationServices.h>
23 enum{
24 kUCTextBreakAllMask = (kUCTextBreakClusterMask | kUCTextBreakWordMask | kUCTextBreakLineMask)
25 };
26 UCTextBreakType breakTypes[4] = {kUCTextBreakCharMask, kUCTextBreakClusterMask, kUCTextBreakWordMask, kUCTextBreakLineMask};
27 TextBreakLocatorRef breakRef;
28 UCTextBreakType macBreakType;
29
30 void createMACBrkIt() {
31 OSStatus status = noErr;
32 LocaleRef lref;
33 status = LocaleRefFromLocaleString(opt_locale, &lref);
34 status = UCCreateTextBreakLocator(lref, 0, kUCTextBreakAllMask, (TextBreakLocatorRef*)&breakRef);
35 if(opt_char == TRUE) {
36 macBreakType = kUCTextBreakClusterMask;
37 } else if(opt_word == TRUE) {
38 macBreakType = kUCTextBreakWordMask;
39 } else if(opt_line == TRUE) {
40 macBreakType = kUCTextBreakLineMask;
41 } else if(opt_sentence == TRUE) {
42 // error
43 // brkit = BreakIterator::createSentenceInstance(opt_locale, status);
44 } else {
45 // default is character iterator
46 macBreakType = kUCTextBreakClusterMask;
47 }
48 }
49 #endif
50
51
52 void doForwardTest() {
53 if (opt_terse == FALSE) {
54 printf("Doing the forward test\n");
55 }
56 int32_t noBreaks = 0;
57 int32_t i = 0;
58 unsigned long startTime = timeGetTime();
59 unsigned long elapsedTime = 0;
60 if(opt_icu) {
61 createICUBrkIt();
62 brkit->setText(text);
63 brkit->first();
64 if (opt_terse == FALSE) {
65 printf("Warmup\n");
66 }
67 while(brkit->next() != BreakIterator::DONE) {
68 noBreaks++;
69 }
70
71 if (opt_terse == FALSE) {
72 printf("Measure\n");
73 }
74 startTime = timeGetTime();
75 for(i = 0; i < opt_loopCount; i++) {
76 brkit->first();
77 while(brkit->next() != BreakIterator::DONE) {
78 }
79 }
80
81 elapsedTime = timeGetTime()-startTime;
82 } else if(opt_mac) {
83 #ifdef U_DARWIN
84 createMACBrkIt();
85 UniChar* filePtr = text;
86 OSStatus status = noErr;
87 UniCharCount startOffset = 0, breakOffset = 0, numUniChars = textSize;
88 startOffset = 0;
89 //printf("\t---Search forward--\n");
90
91 while (startOffset < numUniChars)
92 {
93 status = UCFindTextBreak(breakRef, macBreakType, kUCTextBreakLeadingEdgeMask, filePtr, numUniChars,
94 startOffset, &breakOffset);
95 //require_action(status == noErr, EXIT, printf( "**UCFindTextBreak failed: startOffset %d, status %d\n", (int)startOffset, (int)status));
96 //require_action((breakOffset <= numUniChars),EXIT, printf("**UCFindTextBreak breakOffset too big: startOffset %d, breakOffset %d\n", (int)startOffset, (int)breakOffset));
97
98 // Output break
99 //printf("\t%d\n", (int)breakOffset);
100
101 // Increment counters
102 noBreaks++;
103 startOffset = breakOffset;
104 }
105 startTime = timeGetTime();
106 for(i = 0; i < opt_loopCount; i++) {
107 startOffset = 0;
108
109 while (startOffset < numUniChars)
110 {
111 status = UCFindTextBreak(breakRef, macBreakType, kUCTextBreakLeadingEdgeMask, filePtr, numUniChars,
112 startOffset, &breakOffset);
113 // Increment counters
114 startOffset = breakOffset;
115 }
116 }
117 elapsedTime = timeGetTime()-startTime;
118 UCDisposeTextBreakLocator(&breakRef);
119 #endif
120
121
122 }
123
124
125 if (opt_terse == FALSE) {
126 int32_t loopTime = (int)(float(1000) * ((float)elapsedTime/(float)opt_loopCount));
127 int32_t timePerCU = (int)(float(1000) * ((float)loopTime/(float)textSize));
128 int32_t timePerBreak = (int)(float(1000) * ((float)loopTime/(float)noBreaks));
129 printf("forward break iteration average loop time %d\n", loopTime);
130 printf("number of code units %d average time per code unit %d\n", textSize, timePerCU);
131 printf("number of breaks %d average time per break %d\n", noBreaks, timePerBreak);
132 } else {
133 printf("time=%d\nevents=%d\nsize=%d\n", elapsedTime, noBreaks, textSize);
134 }
135
136
137 }
138
139
140
141
142 #endif
143
144 UPerfFunction* BreakIteratorPerformanceTest::TestICUForward()
145 {
146 return new ICUForward(locale, m_mode_, m_file_, m_fileLen_);
147 }
148
149 UPerfFunction* BreakIteratorPerformanceTest::TestICUIsBound()
150 {
151 return new ICUIsBound(locale, m_mode_, m_file_, m_fileLen_);
152 }
153
154 UPerfFunction* BreakIteratorPerformanceTest::TestDarwinForward()
155 {
156 return NULL;
157 }
158
159 UPerfFunction* BreakIteratorPerformanceTest::TestDarwinIsBound()
160 {
161 return NULL;
162 }
163
164 UPerfFunction* BreakIteratorPerformanceTest::runIndexedTest(int32_t index, UBool exec,
165 const char *&name,
166 char* par)
167 {
168 switch (index) {
169 TESTCASE(0, TestICUForward);
170 TESTCASE(1, TestICUIsBound);
171 TESTCASE(2, TestDarwinForward);
172 TESTCASE(3, TestDarwinIsBound);
173 default:
174 name = "";
175 return NULL;
176 }
177 return NULL;
178 }
179
180 UOption options[]={
181 UOPTION_DEF( "mode", 'm', UOPT_REQUIRES_ARG)
182 };
183
184
185 BreakIteratorPerformanceTest::BreakIteratorPerformanceTest(int32_t argc, const char* argv[], UErrorCode& status)
186 : UPerfTest(argc,argv,status),
187 m_mode_(NULL),
188 m_file_(NULL),
189 m_fileLen_(0)
190 {
191
192 _remainingArgc = u_parseArgs(_remainingArgc, (char**)argv, (int32_t)(sizeof(options)/sizeof(options[0])), options);
193
194
195 if(options[0].doesOccur) {
196 m_mode_ = options[0].value;
197 switch(options[0].value[0]) {
198 case 'w' :
199 case 'c' :
200 case 's' :
201 case 'l' :
202 break;
203 default:
204 status = U_ILLEGAL_ARGUMENT_ERROR;
205 break;
206 }
207 } else {
208 status = U_ILLEGAL_ARGUMENT_ERROR;
209 }
210
211 m_file_ = getBuffer(m_fileLen_, status);
212
213 if(status== U_ILLEGAL_ARGUMENT_ERROR){
214 fprintf(stderr, gUsageString, "normperf");
215 fprintf(stderr, "\t-m or --mode Required mode for breakiterator: char, word, line or sentence\n");
216
217 return;
218 }
219
220 if(U_FAILURE(status)){
221 fprintf(stderr, "FAILED to create UPerfTest object. Error: %s\n", u_errorName(status));
222 return;
223 }
224 }
225
226 BreakIteratorPerformanceTest::~BreakIteratorPerformanceTest()
227 {
228 }
229
230
231 //----------------------------------------------------------------------------------------
232 //
233 // Main -- process command line, read in and pre-process the test file,
234 // call other functions to do the actual tests.
235 //
236 //----------------------------------------------------------------------------------------
237 int main(int argc, const char** argv) {
238 UErrorCode status = U_ZERO_ERROR;
239 BreakIteratorPerformanceTest test(argc, argv, status);
240 if(U_FAILURE(status)){
241 return status;
242 }
243 if(test.run()==FALSE){
244 fprintf(stderr,"FAILED: Tests could not be run please check the arguments.\n");
245 return -1;
246 }
247 return 0;
248 }