]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/perf/howExpensiveIs/howExpensiveIs.cpp
ICU-491.11.3.tar.gz
[apple/icu.git] / icuSources / test / perf / howExpensiveIs / howExpensiveIs.cpp
CommitLineData
4388f060
A
1/*
2 **********************************************************************
3 * Copyright (c) 2011,International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 */
7#include <stdio.h>
8#include "sieve.h"
9#include "unicode/utimer.h"
10#include "udbgutil.h"
11
12void runTests(void);
13
14FILE *out = NULL;
15UErrorCode setupStatus = U_ZERO_ERROR;
16
17int main(int argc, const char* argv[]){
18#if U_DEBUG
19 fprintf(stderr,"%s: warning: U_DEBUG is on.\n", argv[0]);
20#endif
21#if U_DEBUG
22 {
23 double m;
24 double s = uprv_getSieveTime(&m);
25 fprintf(stderr, "** Standard sieve time: %.9fs +/- %.9fs (%d iterations)\n", s,m, (int)U_LOTS_OF_TIMES);
26 }
27#endif
28
29 if(argc==2) {
30 out=fopen(argv[1],"w");
31 if(out==NULL) {
32 fprintf(stderr,"Err: can't open %s for writing.\n", argv[1]);
33 return 1;
34 }
35 fprintf(out, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
36 fprintf(out, "<tests icu=\"%s\">\n", U_ICU_VERSION);
37 fprintf(out, "<!-- %s -->\n", U_COPYRIGHT_STRING);
38 } else if(argc>2) {
39 fprintf(stderr, "Err: usage: %s [ output-file.xml ]\n", argv[0]);
40 return 1;
41 }
42
43 runTests();
44
45
46 if(out!=NULL) {
47 udbg_writeIcuInfo(out);
48 fprintf(out, "</tests>\n");
49 fclose(out);
50 }
51
52 if(U_FAILURE(setupStatus)) {
53 fprintf(stderr, "Error in tests: %s\n", u_errorName(setupStatus));
54 return 1;
55 }
56
57 return 0;
58}
59
60class HowExpensiveTest {
61public:
62 virtual ~HowExpensiveTest(){}
63protected:
64 HowExpensiveTest(const char *name, const char *file, int32_t line) : fName(name), fFile(file), fLine(line) {}
65protected:
66 /**
67 * @return number of iterations
68 */
69 virtual int32_t run() = 0;
70 virtual void warmup() { run(); }
71public:
72 virtual int32_t runTest(double *subTime) {
73 UTimer a,b;
74 utimer_getTime(&a);
75 int32_t iter = run();
76 utimer_getTime(&b);
77 *subTime = utimer_getDeltaSeconds(&a,&b);
78 return iter;
79 }
80
81 virtual int32_t runTests(double *subTime, double *marginOfError) {
82 warmup(); /* warmup */
83 #define ITERATIONS 5
84 double times[ITERATIONS];
85 int subIterations = 0;
86 for(int i=0;i<ITERATIONS;i++) {
87 subIterations = runTest(&times[i]);
88#if U_DEBUG
89 fprintf(stderr, "trial: %d/%d = %.9fs\n", i, ITERATIONS,times[i]);
90 fflush(stderr);
91#endif
92 }
93 *subTime = uprv_getMeanTime(times,ITERATIONS,marginOfError);
94 return subIterations;
95 }
96public:
97 const char *fName;
98 const char *fFile;
99 int32_t fLine;
100 int32_t fIterations;
101};
102
103void runTestOn(HowExpensiveTest &t) {
104 fprintf(stderr, "%s:%d: Running: %s\n", t.fFile, t.fLine, t.fName);
105 double sieveTime = uprv_getSieveTime(NULL);
106 double st;
107 double me;
108
109 fflush(stdout);
110 fflush(stderr);
111 int32_t iter = t.runTests(&st,&me);
112 fflush(stdout);
113 fflush(stderr);
114
115 double stn = st/sieveTime;
116
117 printf("%s\t%.9f\t%.9f +/- %.9f, @ %d iter\n", t.fName,stn,st,me,iter);
118
119 if(out!=NULL) {
120 fprintf(out, " <test name=\"%s\" standardizedTime=\"%f\" realDuration=\"%f\" marginOfError=\"%f\" iterations=\"%d\" />\n",
121 t.fName,stn,st,me,iter);
122 fflush(out);
123 }
124}
125
126/* ------------------- test code here --------------------- */
127
128class SieveTest : public HowExpensiveTest {
129public:
130 virtual ~SieveTest(){}
131 SieveTest():HowExpensiveTest("SieveTest",__FILE__,__LINE__){}
132 virtual int32_t run(){return 0;} // dummy
133 int32_t runTest(double *subTime) {
134 *subTime = uprv_getSieveTime(NULL);
135 return U_LOTS_OF_TIMES;
136 }
137 virtual int32_t runTests(double *subTime, double *marginOfError) {
138 *subTime = uprv_getSieveTime(marginOfError);
139 return U_LOTS_OF_TIMES;
140 }
141};
142
143
144/* ------- NumParseTest ------------- */
145#include "unicode/unum.h"
146/* open and close tests */
147#define OCName(svc,ub,testn,suffix,n) testn ## svc ## ub ## suffix ## n
148#define OCStr(svc,ub,suffix,n) "Test_" # svc # ub # suffix # n
149#define OCRun(svc,ub,suffix) svc ## ub ## suffix
150// TODO: run away screaming
151#define OpenCloseTest(n, svc,suffix,c,a,d) class OCName(svc,_,Test_,suffix,n) : public HowExpensiveTest { public: OCName(svc,_,Test_,suffix,n)():HowExpensiveTest(OCStr(svc,_,suffix,n),__FILE__,__LINE__) c int32_t run() { int32_t i; for(i=0;i<U_LOTS_OF_TIMES;i++){ OCRun(svc,_,close) ( OCRun(svc,_,suffix) a ); } return i; } void warmup() { OCRun(svc,_,close) ( OCRun(svc,_,suffix) a); } virtual ~ OCName(svc,_,Test_,suffix,n) () d };
152#define QuickTest(n,c,r,d) class n : public HowExpensiveTest { public: n():HowExpensiveTest(#n,__FILE__,__LINE__) c int32_t run() r virtual ~n () d };
153
154// TODO: move, scope.
155static UChar pattern[] = { 0x23 }; // '#'
156
157UNumberFormat *NumParseTest_fmt;
158
159// TODO: de-uglify.
160QuickTest(NumParseTest,{ static UChar pattern[] = { 0x23 }; NumParseTest_fmt = unum_open(UNUM_PATTERN_DECIMAL, pattern, 1, "en_US", 0, &setupStatus); },{ int32_t i; static UChar str[] = { 0x31 };double val; for(i=0;i<U_LOTS_OF_TIMES;i++) { val=unum_parse(NumParseTest_fmt,str,1,NULL,&setupStatus); } return i; },{unum_close(NumParseTest_fmt);})
161
162
163QuickTest(NullTest,{},{int j=U_LOTS_OF_TIMES;while(--j);return U_LOTS_OF_TIMES;},{})
164OpenCloseTest(pattern,unum,open,{},(UNUM_PATTERN_DECIMAL,pattern,1,"en_US",0,&setupStatus),{})
165OpenCloseTest(default,unum,open,{},(UNUM_DEFAULT,NULL,-1,"en_US",0,&setupStatus),{})
166#include "unicode/ucnv.h"
167OpenCloseTest(gb18030,ucnv,open,{},("gb18030",&setupStatus),{})
168#include "unicode/ures.h"
169OpenCloseTest(root,ures,open,{},(NULL,"root",&setupStatus),{})
170
171void runTests() {
172 {
173 SieveTest t;
174 runTestOn(t);
175 }
176 {
177 NullTest t;
178 runTestOn(t);
179 }
180 {
181 NumParseTest t;
182 runTestOn(t);
183 }
184 {
185 Test_unum_opendefault t;
186 runTestOn(t);
187 }
188 {
189 Test_ucnv_opengb18030 t;
190 runTestOn(t);
191 }
192 {
193 Test_unum_openpattern t;
194 runTestOn(t);
195 }
196 {
197 Test_ures_openroot t;
198 runTestOn(t);
199 }
200}