]> git.saurik.com Git - apple/icu.git/blob - icuSources/tools/toolutil/uperf.h
ICU-6.2.10.tar.gz
[apple/icu.git] / icuSources / tools / toolutil / uperf.h
1 /*
2 **********************************************************************
3 * Copyright (c) 2002-2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 **********************************************************************
7 */
8 #ifndef _UPERF_H
9 #define _UPERF_H
10
11 #include "unicode/utypes.h"
12 #include "unicode/unistr.h"
13 #include "unicode/ustring.h"
14 #include "utimer.h"
15 #include "uoptions.h"
16 #include "ucbuf.h"
17
18 U_NAMESPACE_USE
19 // Use the TESTCASE macro in subclasses of IntlTest. Define the
20 // runIndexedTest method in this fashion:
21 //
22 //| void MyTest::runIndexedTest(int32_t index, UBool exec,
23 //| const char* &name, char* /*par*/) {
24 //| switch (index) {
25 //| TESTCASE(0,TestSomething);
26 //| TESTCASE(1,TestSomethingElse);
27 //| TESTCASE(2,TestAnotherThing);
28 //| default:
29 //| name = "";
30 //| return NULL;
31 //| }
32 //| }
33 #if 0
34 #define TESTCASE(id,test) \
35 case id: \
36 name = #test; \
37 if (exec) { \
38 fprintf(stdout,#test "---"); \
39 fprintf(stdout,"\n"); \
40 return test(); \
41 } \
42 break
43
44 #endif
45 #define TESTCASE(id,test) \
46 case id: \
47 name = #test; \
48 if (exec) { \
49 return test(); \
50 } \
51 break
52
53 /**
54 * Subclasses of PerfTest will need to create subclasses of
55 * Function that define a call() method which contains the code to
56 * be timed. They then call setTestFunction() in their "Test..."
57 * method to establish this as the current test functor.
58 */
59 class U_EXPORT UPerfFunction {
60 public:
61 /**
62 * Subclasses must implement this method to do the action to be
63 * measured.
64 */
65 virtual void call(UErrorCode* status)=0;
66
67 /**
68 * Subclasses must implement this method to return positive
69 * integer indicating the number of operations in a single
70 * call to this object's call() method.
71 */
72 virtual long getOperationsPerIteration()=0;
73 /**
74 * Subclasses should override this method to return either positive
75 * or negative integer indicating the number of events in a single
76 * call to this object's call() method, if applicable
77 * e.g: Number of breaks / iterations for break iterator
78 */
79 virtual long getEventsPerIteration(){
80 return -1;
81 }
82
83 /**
84 * Call call() n times in a tight loop and return the elapsed
85 * milliseconds. If n is small and call() is fast the return
86 * result may be zero. Small return values have limited
87 * meaningfulness, depending on the underlying CPU and OS.
88 */
89 double time(int32_t n, UErrorCode* status) {
90 UTimer start, stop;
91 utimer_getTime(&start);
92 while (n-- > 0) {
93 call(status);
94 }
95 utimer_getTime(&stop);
96 return utimer_getDeltaSeconds(&start,&stop); // ms
97 }
98
99 };
100
101
102 class U_EXPORT UPerfTest {
103 public:
104 UBool run();
105 UBool runTest( char* name = NULL, char* par = NULL ); // not to be overidden
106
107 virtual void usage( void ) ;
108
109 virtual ~UPerfTest();
110
111 void setCaller( UPerfTest* callingTest ); // for internal use only
112
113 void setPath( char* path ); // for internal use only
114
115 ULine* getLines(UErrorCode& status);
116
117 const UChar* getBuffer(int32_t& len,UErrorCode& status);
118
119 protected:
120 UPerfTest(int32_t argc, const char* argv[], UErrorCode& status);
121
122 virtual UPerfFunction* runIndexedTest( int32_t index, UBool exec, const char* &name, char* par = NULL ); // overide !
123
124 virtual UBool runTestLoop( char* testname, char* par );
125
126 virtual UBool callTest( UPerfTest& testToBeCalled, char* par );
127
128 UBool verbose;
129 const char* sourceDir;
130 const char* fileName;
131 char* resolvedFileName;
132 const char* encoding;
133 UBool uselen;
134 int32_t iterations;
135 int32_t passes;
136 int32_t time;
137 const char** _argv;
138 int32_t _argc;
139 int32_t _remainingArgc;
140 ULine* lines;
141 int32_t numLines;
142 UCHARBUF* ucharBuf;
143 UBool line_mode;
144 UBool bulk_mode;
145 UChar* buffer;
146 int32_t bufferLen;
147 const char* locale;
148 private:
149 UPerfTest* caller;
150 char* path; // specifies subtests
151
152 // static members
153 public:
154 static UPerfTest* gTest;
155 static const char gUsageString[];
156 };
157
158 #endif
159