1 /* 2 ********************************************************************** 3 * Copyright (c) 2002-2011,International Business Machines 4 * Corporation and others. All Rights Reserved. 5 ********************************************************************** 6 ********************************************************************** 7 */ 8 9 #include "DateFmtPerf.h" 10 #include "uoptions.h" 11 #include <stdio.h> 12 #include <fstream> 13 14 #include <iostream> 15 using namespace std; 16 17 DateFormatPerfTest::DateFormatPerfTest(int32_t argc, const char* argv[], UErrorCode& status) 18 : UPerfTest(argc,argv,status) { 19 20 if (locale == NULL){ 21 locale = "en_US"; // set default locale 22 } 23 } 24 25 DateFormatPerfTest::~DateFormatPerfTest() 26 { 27 } 28 29 UPerfFunction* DateFormatPerfTest::runIndexedTest(int32_t index, UBool exec,const char* &name, char* par) { 30 31 //exec = true; 32 33 switch (index) { 34 TESTCASE(0,DateFmt250); 35 TESTCASE(1,DateFmt10000); 36 TESTCASE(2,DateFmt100000); 37 TESTCASE(3,BreakItWord250); 38 TESTCASE(4,BreakItWord10000); 39 TESTCASE(5,BreakItChar250); 40 TESTCASE(6,BreakItChar10000); 41 TESTCASE(7,NumFmt10000); 42 TESTCASE(8,NumFmt100000); 43 TESTCASE(9,Collation10000); 44 TESTCASE(10,Collation100000); 45 46 default: 47 name = ""; 48 return NULL; 49 } 50 return NULL; 51 } 52 53 54 UPerfFunction* DateFormatPerfTest::DateFmt250(){ 55 DateFmtFunction* func= new DateFmtFunction(1, locale); 56 return func; 57 } 58 59 UPerfFunction* DateFormatPerfTest::DateFmt10000(){ 60 DateFmtFunction* func= new DateFmtFunction(40, locale); 61 return func; 62 } 63 64 UPerfFunction* DateFormatPerfTest::DateFmt100000(){ 65 DateFmtFunction* func= new DateFmtFunction(400, locale); 66 return func; 67 } 68 69 UPerfFunction* DateFormatPerfTest::BreakItWord250(){ 70 BreakItFunction* func= new BreakItFunction(250, true); 71 return func; 72 } 73 74 UPerfFunction* DateFormatPerfTest::BreakItWord10000(){ 75 BreakItFunction* func= new BreakItFunction(10000, true); 76 return func; 77 } 78 79 UPerfFunction* DateFormatPerfTest::BreakItChar250(){ 80 BreakItFunction* func= new BreakItFunction(250, false); 81 return func; 82 } 83 84 UPerfFunction* DateFormatPerfTest::BreakItChar10000(){ 85 BreakItFunction* func= new BreakItFunction(10000, false); 86 return func; 87 } 88 89 UPerfFunction* DateFormatPerfTest::NumFmt10000(){ 90 NumFmtFunction* func= new NumFmtFunction(10000, locale); 91 return func; 92 } 93 94 UPerfFunction* DateFormatPerfTest::NumFmt100000(){ 95 NumFmtFunction* func= new NumFmtFunction(100000, locale); 96 return func; 97 } 98 99 UPerfFunction* DateFormatPerfTest::Collation10000(){ 100 CollationFunction* func= new CollationFunction(40, locale); 101 return func; 102 } 103 104 UPerfFunction* DateFormatPerfTest::Collation100000(){ 105 CollationFunction* func= new CollationFunction(400, locale); 106 return func; 107 } 108 109 110 111 int main(int argc, const char* argv[]){ 112 113 // -x Filename.xml 114 if((argc>1)&&(strcmp(argv[1],"-x") == 0)) 115 { 116 if(argc < 3) { 117 fprintf(stderr, "Usage: %s -x <outfile>.xml\n", argv[0]); 118 return 1; 119 // not enough arguments 120 } 121 122 cout << "ICU version - " << U_ICU_VERSION << endl; 123 UErrorCode status = U_ZERO_ERROR; 124 125 #define FUNCTION_COUNT 6 126 // Declare functions 127 UPerfFunction *functions[FUNCTION_COUNT]; 128 129 functions[0] = new DateFmtFunction(40, "en"); 130 functions[1] = new BreakItFunction(10000, true); // breakIterator word 131 functions[2] = new BreakItFunction(10000, false); // breakIterator char 132 functions[3] = new NumFmtFunction(100000, "en"); 133 functions[4] = new CollationFunction(400, "en"); 134 functions[5] = new StdioNumFmtFunction(100000, "en"); 135 136 // Perform time recording 137 double t[FUNCTION_COUNT]; 138 for(int i = 0; i < FUNCTION_COUNT; i++) t[i] = 0; 139 140 #define ITER_COUNT 10 141 #ifdef U_DEBUG 142 cout << "Doing " << ITER_COUNT << " iterations:" << endl; 143 cout << "__________| Running...\r"; 144 cout.flush(); 145 #endif 146 for(int i = 0; i < ITER_COUNT; i++) { 147 #ifdef U_DEBUG 148 cout << '*' << flush; 149 #endif 150 for(int j = 0; U_SUCCESS(status)&& j < FUNCTION_COUNT; j++) 151 t[j] += (functions[j]->time(1, &status) / ITER_COUNT); 152 } 153 #ifdef U_DEBUG 154 cout << " Done " << endl; 155 #endif 156 157 if(U_SUCCESS(status)) { 158 159 // Output results as .xml 160 ofstream out; 161 out.open(argv[2]); 162 163 out << "<perfTestResults icu=\"c\" version=\"" << U_ICU_VERSION << "\">" << endl; 164 165 for(int i = 0; i < FUNCTION_COUNT; i++) 166 { 167 out << " <perfTestResult" << endl; 168 out << " test=\""; 169 switch(i) 170 { 171 case 0: out << "DateFormat"; break; 172 case 1: out << "BreakIterator Word"; break; 173 case 2: out << "BreakIterator Char"; break; 174 case 3: out << "NumbFormat"; break; 175 case 4: out << "Collation"; break; 176 case 5: out << "StdioNumbFormat"; break; 177 default: out << "Unknown " << i; break; 178 } 179 out << "\"" << endl; 180 out << " iterations=\"" << functions[i]->getOperationsPerIteration() << "\"" << endl; 181 out << " time=\"" << t[i] << "\" />" << endl; 182 } 183 out << "</perfTestResults>" << endl; 184 out.close(); 185 cout << " Wrote to " << argv[2] << endl; 186 } 187 188 if(U_FAILURE(status)) { 189 cout << "Error! " << u_errorName(status) << endl; 190 return 1; 191 } 192 193 return 0; 194 } 195 196 197 // Normal performance test mode 198 UErrorCode status = U_ZERO_ERROR; 199 200 DateFormatPerfTest test(argc, argv, status); 201 202 203 if(U_FAILURE(status)){ // ERROR HERE!!! 204 cout << "initialize failed! " << status << endl; 205 return status; 206 } 207 //cout << "Done initializing!\n" << endl; 208 209 if(test.run()==FALSE){ 210 cout << "run failed!" << endl; 211 fprintf(stderr,"FAILED: Tests could not be run please check the arguments.\n"); 212 return -1; 213 } 214 cout << "done!" << endl; 215 216 return 0; 217 } 218