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