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