1 /********************************************************************
3 * Copyright (C) 2001 IBM, Inc. All Rights Reserved.
5 ********************************************************************/
6 /********************************************************************************
10 * Modification History:
12 * Vladimir Weinstein First Version, based on collperf
14 *********************************************************************************
22 #include <ApplicationServices/ApplicationServices.h>
24 kUCTextBreakAllMask
= (kUCTextBreakClusterMask
| kUCTextBreakWordMask
| kUCTextBreakLineMask
)
26 UCTextBreakType breakTypes
[4] = {kUCTextBreakCharMask
, kUCTextBreakClusterMask
, kUCTextBreakWordMask
, kUCTextBreakLineMask
};
27 TextBreakLocatorRef breakRef
;
28 UCTextBreakType macBreakType
;
30 void createMACBrkIt() {
31 OSStatus status
= noErr
;
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
) {
43 // brkit = BreakIterator::createSentenceInstance(opt_locale, status);
45 // default is character iterator
46 macBreakType
= kUCTextBreakClusterMask
;
52 void doForwardTest() {
53 if (opt_terse
== FALSE
) {
54 printf("Doing the forward test\n");
58 unsigned long startTime
= timeGetTime();
59 unsigned long elapsedTime
= 0;
64 if (opt_terse
== FALSE
) {
67 while(brkit
->next() != BreakIterator::DONE
) {
71 if (opt_terse
== FALSE
) {
74 startTime
= timeGetTime();
75 for(i
= 0; i
< opt_loopCount
; i
++) {
77 while(brkit
->next() != BreakIterator::DONE
) {
81 elapsedTime
= timeGetTime()-startTime
;
85 UniChar
* filePtr
= text
;
86 OSStatus status
= noErr
;
87 UniCharCount startOffset
= 0, breakOffset
= 0, numUniChars
= textSize
;
89 //printf("\t---Search forward--\n");
91 while (startOffset
< numUniChars
)
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));
99 //printf("\t%d\n", (int)breakOffset);
101 // Increment counters
103 startOffset
= breakOffset
;
105 startTime
= timeGetTime();
106 for(i
= 0; i
< opt_loopCount
; i
++) {
109 while (startOffset
< numUniChars
)
111 status
= UCFindTextBreak(breakRef
, macBreakType
, kUCTextBreakLeadingEdgeMask
, filePtr
, numUniChars
,
112 startOffset
, &breakOffset
);
113 // Increment counters
114 startOffset
= breakOffset
;
117 elapsedTime
= timeGetTime()-startTime
;
118 UCDisposeTextBreakLocator(&breakRef
);
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
);
133 printf("time=%d\nevents=%d\nsize=%d\n", elapsedTime
, noBreaks
, textSize
);
144 UPerfFunction
* BreakIteratorPerformanceTest::TestICUForward()
146 return new ICUForward(locale
, m_mode_
, m_file_
, m_fileLen_
);
149 UPerfFunction
* BreakIteratorPerformanceTest::TestICUIsBound()
151 return new ICUIsBound(locale
, m_mode_
, m_file_
, m_fileLen_
);
154 UPerfFunction
* BreakIteratorPerformanceTest::TestDarwinForward()
159 UPerfFunction
* BreakIteratorPerformanceTest::TestDarwinIsBound()
164 UPerfFunction
* BreakIteratorPerformanceTest::runIndexedTest(int32_t index
, UBool exec
,
169 TESTCASE(0, TestICUForward
);
170 TESTCASE(1, TestICUIsBound
);
171 TESTCASE(2, TestDarwinForward
);
172 TESTCASE(3, TestDarwinIsBound
);
181 UOPTION_DEF( "mode", 'm', UOPT_REQUIRES_ARG
)
185 BreakIteratorPerformanceTest::BreakIteratorPerformanceTest(int32_t argc
, const char* argv
[], UErrorCode
& status
)
186 : UPerfTest(argc
,argv
,status
),
192 _remainingArgc
= u_parseArgs(_remainingArgc
, (char**)argv
, (int32_t)(sizeof(options
)/sizeof(options
[0])), options
);
195 if(options
[0].doesOccur
) {
196 m_mode_
= options
[0].value
;
197 switch(options
[0].value
[0]) {
204 status
= U_ILLEGAL_ARGUMENT_ERROR
;
208 status
= U_ILLEGAL_ARGUMENT_ERROR
;
211 m_file_
= getBuffer(m_fileLen_
, status
);
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");
220 if(U_FAILURE(status
)){
221 fprintf(stderr
, "FAILED to create UPerfTest object. Error: %s\n", u_errorName(status
));
226 BreakIteratorPerformanceTest::~BreakIteratorPerformanceTest()
231 //----------------------------------------------------------------------------------------
233 // Main -- process command line, read in and pre-process the test file,
234 // call other functions to do the actual tests.
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
)){
243 if(test
.run()==FALSE
){
244 fprintf(stderr
,"FAILED: Tests could not be run please check the arguments.\n");