1 /***********************************************************************
2 * © 2016 and later: Unicode, Inc. and others.
3 * License & terms of use: http://www.unicode.org/copyright.html#License
5 ***********************************************************************
6 ***********************************************************************
8 * Copyright (C) 2001-2016 International Business Machines Corporation
9 * and others. All Rights Reserved.
11 ***********************************************************************/
12 /********************************************************************************
16 * Modification History:
18 * Vladimir Weinstein First Version, based on collperf
20 *********************************************************************************
30 #if U_PLATFORM_IS_DARWIN_BASED
31 #include <ApplicationServices/ApplicationServices.h>
33 kUCTextBreakAllMask
= (kUCTextBreakClusterMask
| kUCTextBreakWordMask
| kUCTextBreakLineMask
)
35 UCTextBreakType breakTypes
[4] = {kUCTextBreakCharMask
, kUCTextBreakClusterMask
, kUCTextBreakWordMask
, kUCTextBreakLineMask
};
36 TextBreakLocatorRef breakRef
;
37 UCTextBreakType macBreakType
;
39 void createMACBrkIt() {
40 OSStatus status
= noErr
;
42 status
= LocaleRefFromLocaleString(opt_locale
, &lref
);
43 status
= UCCreateTextBreakLocator(lref
, 0, kUCTextBreakAllMask
, (TextBreakLocatorRef
*)&breakRef
);
44 if(opt_char
== TRUE
) {
45 macBreakType
= kUCTextBreakClusterMask
;
46 } else if(opt_word
== TRUE
) {
47 macBreakType
= kUCTextBreakWordMask
;
48 } else if(opt_line
== TRUE
) {
49 macBreakType
= kUCTextBreakLineMask
;
50 } else if(opt_sentence
== TRUE
) {
52 // brkit = BreakIterator::createSentenceInstance(opt_locale, status);
54 // default is character iterator
55 macBreakType
= kUCTextBreakClusterMask
;
61 void doForwardTest() {
62 if (opt_terse
== FALSE
) {
63 printf("Doing the forward test\n");
67 unsigned long startTime
= timeGetTime();
68 unsigned long elapsedTime
= 0;
73 if (opt_terse
== FALSE
) {
76 while(brkit
->next() != BreakIterator::DONE
) {
80 if (opt_terse
== FALSE
) {
83 startTime
= timeGetTime();
84 for(i
= 0; i
< opt_loopCount
; i
++) {
86 while(brkit
->next() != BreakIterator::DONE
) {
90 elapsedTime
= timeGetTime()-startTime
;
92 #if U_PLATFORM_IS_DARWIN_BASED
94 UniChar
* filePtr
= text
;
95 OSStatus status
= noErr
;
96 UniCharCount startOffset
= 0, breakOffset
= 0, numUniChars
= textSize
;
98 //printf("\t---Search forward--\n");
100 while (startOffset
< numUniChars
)
102 status
= UCFindTextBreak(breakRef
, macBreakType
, kUCTextBreakLeadingEdgeMask
, filePtr
, numUniChars
,
103 startOffset
, &breakOffset
);
104 //require_action(status == noErr, EXIT, printf( "**UCFindTextBreak failed: startOffset %d, status %d\n", (int)startOffset, (int)status));
105 //require_action((breakOffset <= numUniChars),EXIT, printf("**UCFindTextBreak breakOffset too big: startOffset %d, breakOffset %d\n", (int)startOffset, (int)breakOffset));
108 //printf("\t%d\n", (int)breakOffset);
110 // Increment counters
112 startOffset
= breakOffset
;
114 startTime
= timeGetTime();
115 for(i
= 0; i
< opt_loopCount
; i
++) {
118 while (startOffset
< numUniChars
)
120 status
= UCFindTextBreak(breakRef
, macBreakType
, kUCTextBreakLeadingEdgeMask
, filePtr
, numUniChars
,
121 startOffset
, &breakOffset
);
122 // Increment counters
123 startOffset
= breakOffset
;
126 elapsedTime
= timeGetTime()-startTime
;
127 UCDisposeTextBreakLocator(&breakRef
);
134 if (opt_terse
== FALSE
) {
135 int32_t loopTime
= (int)(float(1000) * ((float)elapsedTime
/(float)opt_loopCount
));
136 int32_t timePerCU
= (int)(float(1000) * ((float)loopTime
/(float)textSize
));
137 int32_t timePerBreak
= (int)(float(1000) * ((float)loopTime
/(float)noBreaks
));
138 printf("forward break iteration average loop time %d\n", loopTime
);
139 printf("number of code units %d average time per code unit %d\n", textSize
, timePerCU
);
140 printf("number of breaks %d average time per break %d\n", noBreaks
, timePerBreak
);
142 printf("time=%d\nevents=%d\nsize=%d\n", elapsedTime
, noBreaks
, textSize
);
153 UPerfFunction
* BreakIteratorPerformanceTest::TestICUForward()
155 return new ICUForward(locale
, m_mode_
, m_file_
, m_fileLen_
);
158 UPerfFunction
* BreakIteratorPerformanceTest::TestICUIsBound()
160 return new ICUIsBound(locale
, m_mode_
, m_file_
, m_fileLen_
);
163 UPerfFunction
* BreakIteratorPerformanceTest::TestDarwinForward()
168 UPerfFunction
* BreakIteratorPerformanceTest::TestDarwinIsBound()
173 UPerfFunction
* BreakIteratorPerformanceTest::runIndexedTest(int32_t index
, UBool exec
,
178 TESTCASE(0, TestICUForward
);
179 TESTCASE(1, TestICUIsBound
);
180 TESTCASE(2, TestDarwinForward
);
181 TESTCASE(3, TestDarwinIsBound
);
190 UOPTION_DEF( "mode", 'm', UOPT_REQUIRES_ARG
)
194 BreakIteratorPerformanceTest::BreakIteratorPerformanceTest(int32_t argc
, const char* argv
[], UErrorCode
& status
)
195 : UPerfTest(argc
,argv
,status
),
201 _remainingArgc
= u_parseArgs(_remainingArgc
, (char**)argv
, UPRV_LENGTHOF(options
), options
);
204 if(options
[0].doesOccur
) {
205 m_mode_
= options
[0].value
;
206 switch(options
[0].value
[0]) {
213 status
= U_ILLEGAL_ARGUMENT_ERROR
;
217 status
= U_ILLEGAL_ARGUMENT_ERROR
;
220 m_file_
= getBuffer(m_fileLen_
, status
);
222 if(status
== U_ILLEGAL_ARGUMENT_ERROR
){
223 fprintf(stderr
, gUsageString
, "ubrkperf");
224 fprintf(stderr
, "\t-m or --mode Required mode for breakiterator: char, word, line or sentence\n");
229 if(U_FAILURE(status
)){
230 fprintf(stderr
, "FAILED to create UPerfTest object. Error: %s\n", u_errorName(status
));
235 BreakIteratorPerformanceTest::~BreakIteratorPerformanceTest()
240 //----------------------------------------------------------------------------------------
242 // Main -- process command line, read in and pre-process the test file,
243 // call other functions to do the actual tests.
245 //----------------------------------------------------------------------------------------
246 int main(int argc
, const char** argv
) {
247 UErrorCode status
= U_ZERO_ERROR
;
248 BreakIteratorPerformanceTest
test(argc
, argv
, status
);
249 if(U_FAILURE(status
)){
252 if(test
.run()==FALSE
){
253 fprintf(stderr
,"FAILED: Tests could not be run please check the arguments.\n");