Home | History | Annotate | Download | only in DateFmtPerf
      1 /*
      2 ***********************************************************************
      3 *  2016 and later: Unicode, Inc. and others.
      4 * License & terms of use: http://www.unicode.org/copyright.html#License
      5 ***********************************************************************
      6 ***********************************************************************
      7 * Copyright (c) 2002-2014,International Business Machines
      8 * Corporation and others.  All Rights Reserved.
      9 ***********************************************************************
     10 ***********************************************************************
     11 */
     12 
     13 #include "DateFmtPerf.h"
     14 #include "uoptions.h"
     15 #include <stdio.h>
     16 #include <fstream>
     17 
     18 #include <iostream>
     19 using namespace std;
     20 
     21 DateFormatPerfTest::DateFormatPerfTest(int32_t argc, const char* argv[], UErrorCode& status)
     22 : UPerfTest(argc,argv,status) {
     23 
     24     if (locale == NULL){
     25         locale = "en_US";   // set default locale
     26     }
     27 }
     28 
     29 DateFormatPerfTest::~DateFormatPerfTest()
     30 {
     31 }
     32 
     33 UPerfFunction* DateFormatPerfTest::runIndexedTest(int32_t index, UBool exec,const char* &name, char* par) {
     34 
     35 	//exec = true;
     36 
     37     switch (index) {
     38         TESTCASE(0,DateFmt250);
     39         TESTCASE(1,DateFmt10000);
     40         TESTCASE(2,DateFmt100000);
     41         TESTCASE(3,BreakItWord250);
     42         TESTCASE(4,BreakItWord10000);
     43         TESTCASE(5,BreakItChar250);
     44         TESTCASE(6,BreakItChar10000);
     45         TESTCASE(7,NumFmt10000);
     46         TESTCASE(8,NumFmt100000);
     47         TESTCASE(9,Collation10000);
     48         TESTCASE(10,Collation100000);
     49         TESTCASE(11, DIFCreate250);
     50         TESTCASE(12, DIFCreate10000);
     51         TESTCASE(13, TimeZoneCreate250);
     52         TESTCASE(14, TimeZoneCreate10000);
     53         TESTCASE(15, DTPatternGeneratorCreate250);
     54         TESTCASE(16, DTPatternGeneratorCreate10000);
     55         TESTCASE(17, DTPatternGeneratorCopy250);
     56         TESTCASE(18, DTPatternGeneratorCopy10000);
     57         TESTCASE(19, DTPatternGeneratorBestValue250);
     58         TESTCASE(20, DTPatternGeneratorBestValue10000);
     59         TESTCASE(21,DateFmtCopy250);
     60         TESTCASE(22,DateFmtCopy10000);
     61         TESTCASE(23,DateFmtCreate250);
     62         TESTCASE(24,DateFmtCreate10000);
     63 
     64 
     65         default:
     66             name = "";
     67             return NULL;
     68     }
     69     return NULL;
     70 }
     71 
     72 
     73 UPerfFunction* DateFormatPerfTest::DateFmt250(){
     74     DateFmtFunction* func= new DateFmtFunction(1, locale);
     75     return func;
     76 }
     77 
     78 UPerfFunction* DateFormatPerfTest::DateFmt10000(){
     79     DateFmtFunction* func= new DateFmtFunction(40, locale);
     80     return func;
     81 }
     82 
     83 UPerfFunction* DateFormatPerfTest::DateFmt100000(){
     84     DateFmtFunction* func= new DateFmtFunction(400, locale);
     85     return func;
     86 }
     87 
     88 UPerfFunction* DateFormatPerfTest::BreakItWord250(){
     89     BreakItFunction* func= new BreakItFunction(250, true);
     90     return func;
     91 }
     92 
     93 UPerfFunction* DateFormatPerfTest::BreakItWord10000(){
     94     BreakItFunction* func= new BreakItFunction(10000, true);
     95     return func;
     96 }
     97 
     98 UPerfFunction* DateFormatPerfTest::BreakItChar250(){
     99     BreakItFunction* func= new BreakItFunction(250, false);
    100     return func;
    101 }
    102 
    103 UPerfFunction* DateFormatPerfTest::BreakItChar10000(){
    104     BreakItFunction* func= new BreakItFunction(10000, false);
    105     return func;
    106 }
    107 
    108 UPerfFunction* DateFormatPerfTest::NumFmt10000(){
    109     NumFmtFunction* func= new NumFmtFunction(10000, locale);
    110     return func;
    111 }
    112 
    113 UPerfFunction* DateFormatPerfTest::NumFmt100000(){
    114     NumFmtFunction* func= new NumFmtFunction(100000, locale);
    115     return func;
    116 }
    117 
    118 UPerfFunction* DateFormatPerfTest::Collation10000(){
    119     CollationFunction* func= new CollationFunction(40, locale);
    120     return func;
    121 }
    122 
    123 UPerfFunction* DateFormatPerfTest::Collation100000(){
    124     CollationFunction* func= new CollationFunction(400, locale);
    125     return func;
    126 }
    127 
    128 
    129 UPerfFunction *DateFormatPerfTest::DIFCreate250() {
    130     DIFCreateFunction* func = new DIFCreateFunction(250, locale);
    131     return func;
    132 }
    133 
    134 UPerfFunction *DateFormatPerfTest::DIFCreate10000() {
    135     DIFCreateFunction* func = new DIFCreateFunction(10000, locale);
    136     return func;
    137 }
    138 
    139 UPerfFunction *DateFormatPerfTest::TimeZoneCreate250() {
    140     return new TimeZoneCreateFunction(250, locale);
    141 }
    142 
    143 UPerfFunction *DateFormatPerfTest::TimeZoneCreate10000() {
    144     return new TimeZoneCreateFunction(10000, locale);
    145 }
    146 
    147 UPerfFunction *DateFormatPerfTest::DTPatternGeneratorCreate250() {
    148     return new DTPatternGeneratorCreateFunction(250, locale);
    149 }
    150 
    151 UPerfFunction *DateFormatPerfTest::DTPatternGeneratorCreate10000() {
    152     return new DTPatternGeneratorCreateFunction(10000, locale);
    153 }
    154 
    155 UPerfFunction *DateFormatPerfTest::DTPatternGeneratorCopy250() {
    156     return new DTPatternGeneratorCopyFunction(250, locale);
    157 }
    158 
    159 UPerfFunction *DateFormatPerfTest::DTPatternGeneratorCopy10000() {
    160     return new DTPatternGeneratorCopyFunction(10000, locale);
    161 }
    162 
    163 UPerfFunction *DateFormatPerfTest::DTPatternGeneratorBestValue250() {
    164     return new DTPatternGeneratorBestValueFunction(250, locale);
    165 }
    166 
    167 UPerfFunction *DateFormatPerfTest::DTPatternGeneratorBestValue10000() {
    168     return new DTPatternGeneratorBestValueFunction(10000, locale);
    169 }
    170 
    171 UPerfFunction* DateFormatPerfTest::DateFmtCopy250(){
    172     return new DateFmtCopyFunction(250, locale);
    173 }
    174 
    175 UPerfFunction* DateFormatPerfTest::DateFmtCopy10000(){
    176     return new DateFmtCopyFunction(10000, locale);
    177 }
    178 
    179 UPerfFunction* DateFormatPerfTest::DateFmtCreate250(){
    180     return new DateFmtCreateFunction(250, locale);
    181 }
    182 
    183 UPerfFunction* DateFormatPerfTest::DateFmtCreate10000(){
    184     return new DateFmtCreateFunction(10000, locale);
    185 }
    186 
    187 
    188 int main(int argc, const char* argv[]){
    189 
    190     // -x Filename.xml
    191     if((argc>1)&&(strcmp(argv[1],"-x") == 0))
    192     {
    193         if(argc < 3) {
    194 			fprintf(stderr, "Usage: %s -x <outfile>.xml\n", argv[0]);
    195 			return 1;
    196 			// not enough arguments
    197 		}
    198 
    199 		cout << "ICU version - " << U_ICU_VERSION << endl;
    200         UErrorCode status = U_ZERO_ERROR;
    201 
    202 #define FUNCTION_COUNT 6
    203         // Declare functions
    204         UPerfFunction *functions[FUNCTION_COUNT];
    205 
    206         functions[0] = new DateFmtFunction(40, "en");
    207         functions[1] = new BreakItFunction(10000, true); // breakIterator word
    208         functions[2] = new BreakItFunction(10000, false); // breakIterator char
    209         functions[3] = new NumFmtFunction(100000, "en");
    210         functions[4] = new CollationFunction(400, "en");
    211         functions[5] = new StdioNumFmtFunction(100000, "en");
    212 
    213         // Perform time recording
    214         double t[FUNCTION_COUNT];
    215         for(int i = 0; i < FUNCTION_COUNT; i++) t[i] = 0;
    216 
    217 #define ITER_COUNT 10
    218 #ifdef U_DEBUG
    219         cout << "Doing " << ITER_COUNT << " iterations:" << endl;
    220         cout << "__________| Running...\r";
    221         cout.flush();
    222 #endif
    223         for(int i = 0; i < ITER_COUNT; i++) {
    224 #ifdef U_DEBUG
    225           cout << '*' << flush;
    226 #endif
    227           for(int j = 0; U_SUCCESS(status)&& j < FUNCTION_COUNT; j++)
    228             t[j] += (functions[j]->time(1, &status) / ITER_COUNT);
    229         }
    230 #ifdef U_DEBUG
    231         cout << " Done                   " << endl;
    232 #endif
    233 
    234         if(U_SUCCESS(status)) {
    235 
    236           // Output results as .xml
    237           ofstream out;
    238           out.open(argv[2]);
    239 
    240           out << "<perfTestResults icu=\"c\" version=\"" << U_ICU_VERSION << "\">" << endl;
    241 
    242           for(int i = 0; i < FUNCTION_COUNT; i++)
    243             {
    244               out << "    <perfTestResult" << endl;
    245               out << "        test=\"";
    246               switch(i)
    247                 {
    248                 case 0: out << "DateFormat"; break;
    249                 case 1: out << "BreakIterator Word"; break;
    250                 case 2: out << "BreakIterator Char"; break;
    251                 case 3: out << "NumbFormat"; break;
    252                 case 4: out << "Collation"; break;
    253                 case 5: out << "StdioNumbFormat"; break;
    254                 default: out << "Unknown "  << i; break;
    255                 }
    256               out << "\"" << endl;
    257               out << "        iterations=\"" << functions[i]->getOperationsPerIteration() << "\"" << endl;
    258               out << "        time=\"" << t[i] << "\" />" << endl;
    259             }
    260           out << "</perfTestResults>" << endl;
    261           out.close();
    262           cout << " Wrote to " << argv[2] << endl;
    263         }
    264 
    265         if(U_FAILURE(status)) {
    266           cout << "Error! " << u_errorName(status) << endl;
    267           return 1;
    268         }
    269 
    270         return 0;
    271     }
    272 
    273 
    274     // Normal performance test mode
    275     UErrorCode status = U_ZERO_ERROR;
    276 
    277     DateFormatPerfTest test(argc, argv, status);
    278 
    279 
    280     if(U_FAILURE(status)){   // ERROR HERE!!!
    281 		cout << "initialize failed! " << u_errorName(status) << endl;
    282         return status;
    283     }
    284 	//cout << "Done initializing!\n" << endl;
    285 
    286     if(test.run()==FALSE){
    287 		cout << "run failed!" << endl;
    288         fprintf(stderr,"FAILED: Tests could not be run please check the arguments.\n");
    289         return -1;
    290     }
    291 	cout << "done!" << endl;
    292 
    293     return 0;
    294 }
    295