1 /********************************************************************
3 * Copyright (C) 2001-2008 International Business Machines Corporation
4 * and others. All Rights Reserved.
6 ********************************************************************/
7 /********************************************************************************
11 * Modification History:
13 * Vladimir Weinstein First Version, based on collperf
15 *********************************************************************************
25 #include <ApplicationServices/ApplicationServices.h>
27 kUCTextBreakAllMask
= (kUCTextBreakClusterMask
| kUCTextBreakWordMask
| kUCTextBreakLineMask
)
29 UCTextBreakType breakTypes
[4] = {kUCTextBreakCharMask
, kUCTextBreakClusterMask
, kUCTextBreakWordMask
, kUCTextBreakLineMask
};
30 TextBreakLocatorRef breakRef
;
31 UCTextBreakType macBreakType
;
33 void createMACBrkIt() {
34 OSStatus status
= noErr
;
36 status
= LocaleRefFromLocaleString(opt_locale
, &lref
);
37 status
= UCCreateTextBreakLocator(lref
, 0, kUCTextBreakAllMask
, (TextBreakLocatorRef
*)&breakRef
);
38 if(opt_char
== TRUE
) {
39 macBreakType
= kUCTextBreakClusterMask
;
40 } else if(opt_word
== TRUE
) {
41 macBreakType
= kUCTextBreakWordMask
;
42 } else if(opt_line
== TRUE
) {
43 macBreakType
= kUCTextBreakLineMask
;
44 } else if(opt_sentence
== TRUE
) {
46 // brkit = BreakIterator::createSentenceInstance(opt_locale, status);
48 // default is character iterator
49 macBreakType
= kUCTextBreakClusterMask
;
55 void doForwardTest() {
56 if (opt_terse
== FALSE
) {
57 printf("Doing the forward test\n");
61 unsigned long startTime
= timeGetTime();
62 unsigned long elapsedTime
= 0;
67 if (opt_terse
== FALSE
) {
70 while(brkit
->next() != BreakIterator::DONE
) {
74 if (opt_terse
== FALSE
) {
77 startTime
= timeGetTime();
78 for(i
= 0; i
< opt_loopCount
; i
++) {
80 while(brkit
->next() != BreakIterator::DONE
) {
84 elapsedTime
= timeGetTime()-startTime
;
88 UniChar
* filePtr
= text
;
89 OSStatus status
= noErr
;
90 UniCharCount startOffset
= 0, breakOffset
= 0, numUniChars
= textSize
;
92 //printf("\t---Search forward--\n");
94 while (startOffset
< numUniChars
)
96 status
= UCFindTextBreak(breakRef
, macBreakType
, kUCTextBreakLeadingEdgeMask
, filePtr
, numUniChars
,
97 startOffset
, &breakOffset
);
98 //require_action(status == noErr, EXIT, printf( "**UCFindTextBreak failed: startOffset %d, status %d\n", (int)startOffset, (int)status));
99 //require_action((breakOffset <= numUniChars),EXIT, printf("**UCFindTextBreak breakOffset too big: startOffset %d, breakOffset %d\n", (int)startOffset, (int)breakOffset));
102 //printf("\t%d\n", (int)breakOffset);
104 // Increment counters
106 startOffset
= breakOffset
;
108 startTime
= timeGetTime();
109 for(i
= 0; i
< opt_loopCount
; i
++) {
112 while (startOffset
< numUniChars
)
114 status
= UCFindTextBreak(breakRef
, macBreakType
, kUCTextBreakLeadingEdgeMask
, filePtr
, numUniChars
,
115 startOffset
, &breakOffset
);
116 // Increment counters
117 startOffset
= breakOffset
;
120 elapsedTime
= timeGetTime()-startTime
;
121 UCDisposeTextBreakLocator(&breakRef
);
128 if (opt_terse
== FALSE
) {
129 int32_t loopTime
= (int)(float(1000) * ((float)elapsedTime
/(float)opt_loopCount
));
130 int32_t timePerCU
= (int)(float(1000) * ((float)loopTime
/(float)textSize
));
131 int32_t timePerBreak
= (int)(float(1000) * ((float)loopTime
/(float)noBreaks
));
132 printf("forward break iteration average loop time %d\n", loopTime
);
133 printf("number of code units %d average time per code unit %d\n", textSize
, timePerCU
);
134 printf("number of breaks %d average time per break %d\n", noBreaks
, timePerBreak
);
136 printf("time=%d\nevents=%d\nsize=%d\n", elapsedTime
, noBreaks
, textSize
);
147 UPerfFunction
* BreakIteratorPerformanceTest::TestICUForward()
149 return new ICUForward(locale
, m_mode_
, m_file_
, m_fileLen_
);
152 UPerfFunction
* BreakIteratorPerformanceTest::TestICUIsBound()
154 return new ICUIsBound(locale
, m_mode_
, m_file_
, m_fileLen_
);
157 UPerfFunction
* BreakIteratorPerformanceTest::TestDarwinForward()
162 UPerfFunction
* BreakIteratorPerformanceTest::TestDarwinIsBound()
167 UPerfFunction
* BreakIteratorPerformanceTest::runIndexedTest(int32_t index
, UBool exec
,
172 TESTCASE(0, TestICUForward
);
173 TESTCASE(1, TestICUIsBound
);
174 TESTCASE(2, TestDarwinForward
);
175 TESTCASE(3, TestDarwinIsBound
);
184 UOPTION_DEF( "mode", 'm', UOPT_REQUIRES_ARG
)
188 BreakIteratorPerformanceTest::BreakIteratorPerformanceTest(int32_t argc
, const char* argv
[], UErrorCode
& status
)
189 : UPerfTest(argc
,argv
,status
),
195 _remainingArgc
= u_parseArgs(_remainingArgc
, (char**)argv
, (int32_t)(sizeof(options
)/sizeof(options
[0])), options
);
198 if(options
[0].doesOccur
) {
199 m_mode_
= options
[0].value
;
200 switch(options
[0].value
[0]) {
207 status
= U_ILLEGAL_ARGUMENT_ERROR
;
211 status
= U_ILLEGAL_ARGUMENT_ERROR
;
214 m_file_
= getBuffer(m_fileLen_
, status
);
216 if(status
== U_ILLEGAL_ARGUMENT_ERROR
){
217 fprintf(stderr
, gUsageString
, "ubrkperf");
218 fprintf(stderr
, "\t-m or --mode Required mode for breakiterator: char, word, line or sentence\n");
223 if(U_FAILURE(status
)){
224 fprintf(stderr
, "FAILED to create UPerfTest object. Error: %s\n", u_errorName(status
));
229 BreakIteratorPerformanceTest::~BreakIteratorPerformanceTest()
234 //----------------------------------------------------------------------------------------
236 // Main -- process command line, read in and pre-process the test file,
237 // call other functions to do the actual tests.
239 //----------------------------------------------------------------------------------------
240 int main(int argc
, const char** argv
) {
241 UErrorCode status
= U_ZERO_ERROR
;
242 BreakIteratorPerformanceTest
test(argc
, argv
, status
);
243 if(U_FAILURE(status
)){
246 if(test
.run()==FALSE
){
247 fprintf(stderr
,"FAILED: Tests could not be run please check the arguments.\n");