1 /********************************************************************
3 * Copyright (C) 2001-2016 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 #if U_PLATFORM_IS_DARWIN_BASED
26 #include <ApplicationServices/ApplicationServices.h>
28 kUCTextBreakAllMask
= (kUCTextBreakClusterMask
| kUCTextBreakWordMask
| kUCTextBreakLineMask
)
30 UCTextBreakType breakTypes
[4] = {kUCTextBreakCharMask
, kUCTextBreakClusterMask
, kUCTextBreakWordMask
, kUCTextBreakLineMask
};
31 TextBreakLocatorRef breakRef
;
32 UCTextBreakType macBreakType
;
34 void createMACBrkIt() {
35 OSStatus status
= noErr
;
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
) {
47 // brkit = BreakIterator::createSentenceInstance(opt_locale, status);
49 // default is character iterator
50 macBreakType
= kUCTextBreakClusterMask
;
56 void doForwardTest() {
57 if (opt_terse
== FALSE
) {
58 printf("Doing the forward test\n");
62 unsigned long startTime
= timeGetTime();
63 unsigned long elapsedTime
= 0;
68 if (opt_terse
== FALSE
) {
71 while(brkit
->next() != BreakIterator::DONE
) {
75 if (opt_terse
== FALSE
) {
78 startTime
= timeGetTime();
79 for(i
= 0; i
< opt_loopCount
; i
++) {
81 while(brkit
->next() != BreakIterator::DONE
) {
85 elapsedTime
= timeGetTime()-startTime
;
87 #if U_PLATFORM_IS_DARWIN_BASED
89 UniChar
* filePtr
= text
;
90 OSStatus status
= noErr
;
91 UniCharCount startOffset
= 0, breakOffset
= 0, numUniChars
= textSize
;
93 //printf("\t---Search forward--\n");
95 while (startOffset
< numUniChars
)
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));
103 //printf("\t%d\n", (int)breakOffset);
105 // Increment counters
107 startOffset
= breakOffset
;
109 startTime
= timeGetTime();
110 for(i
= 0; i
< opt_loopCount
; i
++) {
113 while (startOffset
< numUniChars
)
115 status
= UCFindTextBreak(breakRef
, macBreakType
, kUCTextBreakLeadingEdgeMask
, filePtr
, numUniChars
,
116 startOffset
, &breakOffset
);
117 // Increment counters
118 startOffset
= breakOffset
;
121 elapsedTime
= timeGetTime()-startTime
;
122 UCDisposeTextBreakLocator(&breakRef
);
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
);
137 printf("time=%d\nevents=%d\nsize=%d\n", elapsedTime
, noBreaks
, textSize
);
148 UPerfFunction
* BreakIteratorPerformanceTest::TestICUForward()
150 return new ICUForward(locale
, m_mode_
, m_file_
, m_fileLen_
);
153 UPerfFunction
* BreakIteratorPerformanceTest::TestICUIsBound()
155 return new ICUIsBound(locale
, m_mode_
, m_file_
, m_fileLen_
);
158 UPerfFunction
* BreakIteratorPerformanceTest::TestDarwinForward()
163 UPerfFunction
* BreakIteratorPerformanceTest::TestDarwinIsBound()
168 UPerfFunction
* BreakIteratorPerformanceTest::runIndexedTest(int32_t index
, UBool exec
,
173 TESTCASE(0, TestICUForward
);
174 TESTCASE(1, TestICUIsBound
);
175 TESTCASE(2, TestDarwinForward
);
176 TESTCASE(3, TestDarwinIsBound
);
185 UOPTION_DEF( "mode", 'm', UOPT_REQUIRES_ARG
)
189 BreakIteratorPerformanceTest::BreakIteratorPerformanceTest(int32_t argc
, const char* argv
[], UErrorCode
& status
)
190 : UPerfTest(argc
,argv
,status
),
196 _remainingArgc
= u_parseArgs(_remainingArgc
, (char**)argv
, UPRV_LENGTHOF(options
), options
);
199 if(options
[0].doesOccur
) {
200 m_mode_
= options
[0].value
;
201 switch(options
[0].value
[0]) {
208 status
= U_ILLEGAL_ARGUMENT_ERROR
;
212 status
= U_ILLEGAL_ARGUMENT_ERROR
;
215 m_file_
= getBuffer(m_fileLen_
, status
);
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");
224 if(U_FAILURE(status
)){
225 fprintf(stderr
, "FAILED to create UPerfTest object. Error: %s\n", u_errorName(status
));
230 BreakIteratorPerformanceTest::~BreakIteratorPerformanceTest()
235 //----------------------------------------------------------------------------------------
237 // Main -- process command line, read in and pre-process the test file,
238 // call other functions to do the actual tests.
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
)){
247 if(test
.run()==FALSE
){
248 fprintf(stderr
,"FAILED: Tests could not be run please check the arguments.\n");