intl/icu/source/tools/ctestfw/unicode/utimer.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 ************************************************************************
michael@0 3 * Copyright (c) 1997-2012, International Business Machines
michael@0 4 * Corporation and others. All Rights Reserved.
michael@0 5 ************************************************************************
michael@0 6 */
michael@0 7
michael@0 8 #ifndef _UTIMER_H
michael@0 9 #define _UTIMER_H
michael@0 10
michael@0 11 #include "unicode/utypes.h"
michael@0 12
michael@0 13 #if U_PLATFORM_HAS_WIN32_API
michael@0 14 # define VC_EXTRALEAN
michael@0 15 # define WIN32_LEAN_AND_MEAN
michael@0 16 # include <windows.h>
michael@0 17 #else
michael@0 18 # if U_PLATFORM == U_PF_OS390 && !defined(__UU)
michael@0 19 # define __UU /* Universal Unix - for struct timeval */
michael@0 20 # endif
michael@0 21 # include <time.h>
michael@0 22 # include <sys/time.h>
michael@0 23 # include <unistd.h>
michael@0 24 #endif
michael@0 25
michael@0 26 /**
michael@0 27 * This API provides functions for performing performance measurement
michael@0 28 * There are 3 main usage scenarios.
michael@0 29 * i) Loop until a threshold time is reached:
michael@0 30 * Example:
michael@0 31 * <code>
michael@0 32 * typedef Params Params;
michael@0 33 * struct Params{
michael@0 34 * UChar* target;
michael@0 35 * int32_t targetLen;
michael@0 36 * const UChar* source;
michael@0 37 * int32_t sourceLen;
michael@0 38 * UNormalizationMode mode;
michael@0 39 * }
michael@0 40 * void NormFn( void* param){
michael@0 41 * Params* parameters = ( Params*) param;
michael@0 42 * UErrorCode error = U_ZERO_ERROR;
michael@0 43 * unorm_normalize(parameters->source, parameters->sourceLen, parameters->mode, 0, parameters->target, parameters->targetLen, &error);
michael@0 44 * if(U_FAILURE(error)){
michael@0 45 * printf("Normalization failed\n");
michael@0 46 * }
michael@0 47 * }
michael@0 48 *
michael@0 49 * int main(){
michael@0 50 * // time the normalization function
michael@0 51 * double timeTaken = 0;
michael@0 52 * Params param;
michael@0 53 * param.source // set up the source buffer
michael@0 54 * param.target // set up the target buffer
michael@0 55 * .... so on ...
michael@0 56 * UTimer timer;
michael@0 57 * // time the loop for 10 seconds at least and find out the loop count and time taken
michael@0 58 * timeTaken = utimer_loopUntilDone((double)10,(void*) param, NormFn, &loopCount);
michael@0 59 * }
michael@0 60 * </code>
michael@0 61 *
michael@0 62 * ii) Measure the time taken
michael@0 63 * Example:
michael@0 64 * <code>
michael@0 65 * double perfNormalization(NormFn fn,const char* mode,Line* fileLines,int32_t loopCount){
michael@0 66 * int line;
michael@0 67 * int loops;
michael@0 68 * UErrorCode error = U_ZERO_ERROR;
michael@0 69 * UChar* dest=NULL;
michael@0 70 * int32_t destCapacity=0;
michael@0 71 * int len =-1;
michael@0 72 * double elapsedTime = 0;
michael@0 73 * int retVal=0;
michael@0 74 *
michael@0 75 * UChar arr[5000];
michael@0 76 * dest=arr;
michael@0 77 * destCapacity = 5000;
michael@0 78 * UTimer start;
michael@0 79 *
michael@0 80 * // Initialize cache and ensure the data is loaded.
michael@0 81 * // This loop checks for errors in Normalization. Once we pass the initialization
michael@0 82 * // without errors we can safelly assume that there are no errors while timing the
michael@0 83 * // funtion
michael@0 84 * for (loops=0; loops<10; loops++) {
michael@0 85 * for (line=0; line < gNumFileLines; line++) {
michael@0 86 * if (opt_uselen) {
michael@0 87 * len = fileLines[line].len;
michael@0 88 * }
michael@0 89 *
michael@0 90 * retVal= fn(fileLines[line].name,len,dest,destCapacity,&error);
michael@0 91 * #if U_PLATFORM_HAS_WIN32_API
michael@0 92 * if(retVal==0 ){
michael@0 93 * fprintf(stderr,"Normalization of string in Windows API failed for mode %s. ErrorNo: %i at line number %i\n",mode,GetLastError(),line);
michael@0 94 * return 0;
michael@0 95 * }
michael@0 96 * #endif
michael@0 97 * if(U_FAILURE(error)){
michael@0 98 * fprintf(stderr,"Normalization of string in ICU API failed for mode %s. Error: %s at line number %i\n",mode,u_errorName(error),line);
michael@0 99 * return 0;
michael@0 100 * }
michael@0 101 *
michael@0 102 * }
michael@0 103 * }
michael@0 104 *
michael@0 105 * //compute the time
michael@0 106 *
michael@0 107 * utimer_getTime(&start);
michael@0 108 * for (loops=0; loops<loopCount; loops++) {
michael@0 109 * for (line=0; line < gNumFileLines; line++) {
michael@0 110 * if (opt_uselen) {
michael@0 111 * len = fileLines[line].len;
michael@0 112 * }
michael@0 113 *
michael@0 114 * retVal= fn(fileLines[line].name,len,dest,destCapacity,&error);
michael@0 115 *
michael@0 116 * }
michael@0 117 * }
michael@0 118 *
michael@0 119 * return utimer_getElapsedSeconds(&start);
michael@0 120 * }
michael@0 121 * </code>
michael@0 122 *
michael@0 123 * iii) Let a higher level function do the calculation of confidence levels etc.
michael@0 124 * Example:
michael@0 125 * <code>
michael@0 126 * void perf(UTimer* timer, UChar* source, int32_t sourceLen, UChar* target, int32_t targetLen, int32_t loopCount,UNormalizationMode mode, UErrorCode* error){
michael@0 127 * int32_t loops;
michael@0 128 * for (loops=0; loops<loopCount; loops++) {
michael@0 129 * unorm_normalize(source,sourceLen,target, targetLen,mode,error);
michael@0 130 * }
michael@0 131 * utimer_getTime(timer);
michael@0 132 * }
michael@0 133 * void main(const char* argsc, int argv){
michael@0 134 * // read the file and setup the data
michael@0 135 * // set up options
michael@0 136 * UTimer start,timer1, timer2, timer3, timer4;
michael@0 137 * double NFDTimeTaken, NFCTimeTaken, FCDTimeTaken;
michael@0 138 * switch(opt){
michael@0 139 * case 0:
michael@0 140 * utimer_getTime(start);
michael@0 141 * perf(timer1, source,sourceLen, target, targetLen,loopCount,UNORM_NFD,&error);
michael@0 142 * NFDTimeTaken = utimer_getDeltaSeconds(start,timer1);
michael@0 143 * case 1:
michael@0 144 * timer_getTime(start);
michael@0 145 * perf(timer2,source,sourceLen,target,targetLen,loopCount,UNORM_NFC,&error);
michael@0 146 * NFCTimeTaken = utimer_getDeltaSeconds(start,timer2);
michael@0 147 * perf(timer3, source, sourceLen, target,targetLen, loopCount, UNORM_FCD,&error);
michael@0 148 * // ........so on .............
michael@0 149 * }
michael@0 150 * // calculate confidence levels etc and print
michael@0 151 *
michael@0 152 * }
michael@0 153 *
michael@0 154 * </code>
michael@0 155 *
michael@0 156 */
michael@0 157
michael@0 158 typedef struct UTimer UTimer;
michael@0 159
michael@0 160 typedef void FuntionToBeTimed(void* param);
michael@0 161
michael@0 162
michael@0 163 #if U_PLATFORM_HAS_WIN32_API
michael@0 164
michael@0 165 struct UTimer{
michael@0 166 LARGE_INTEGER start;
michael@0 167 LARGE_INTEGER placeHolder;
michael@0 168 };
michael@0 169
michael@0 170 static int uprv_initFrequency(UTimer* timer)
michael@0 171 {
michael@0 172 return QueryPerformanceFrequency(&timer->placeHolder);
michael@0 173 }
michael@0 174 static void uprv_start(UTimer* timer)
michael@0 175 {
michael@0 176 QueryPerformanceCounter(&timer->start);
michael@0 177 }
michael@0 178 static double uprv_delta(UTimer* timer1, UTimer* timer2){
michael@0 179 return ((double)(timer2->start.QuadPart - timer1->start.QuadPart))/((double)timer1->placeHolder.QuadPart);
michael@0 180 }
michael@0 181 static UBool uprv_compareFrequency(UTimer* timer1, UTimer* timer2){
michael@0 182 return (timer1->placeHolder.QuadPart == timer2->placeHolder.QuadPart);
michael@0 183 }
michael@0 184
michael@0 185 #else
michael@0 186
michael@0 187 struct UTimer{
michael@0 188 struct timeval start;
michael@0 189 struct timeval placeHolder;
michael@0 190 };
michael@0 191
michael@0 192 static int32_t uprv_initFrequency(UTimer* /*timer*/)
michael@0 193 {
michael@0 194 return 0;
michael@0 195 }
michael@0 196 static void uprv_start(UTimer* timer)
michael@0 197 {
michael@0 198 gettimeofday(&timer->start, 0);
michael@0 199 }
michael@0 200 static double uprv_delta(UTimer* timer1, UTimer* timer2){
michael@0 201 double t1, t2;
michael@0 202
michael@0 203 t1 = (double)timer1->start.tv_sec + (double)timer1->start.tv_usec/(1000*1000);
michael@0 204 t2 = (double)timer2->start.tv_sec + (double)timer2->start.tv_usec/(1000*1000);
michael@0 205 return (t2-t1);
michael@0 206 }
michael@0 207 static UBool uprv_compareFrequency(UTimer* /*timer1*/, UTimer* /*timer2*/){
michael@0 208 return TRUE;
michael@0 209 }
michael@0 210
michael@0 211 #endif
michael@0 212 /**
michael@0 213 * Intializes the timer with the current time
michael@0 214 *
michael@0 215 * @param timer A pointer to UTimer struct to recieve the current time
michael@0 216 */
michael@0 217 static inline void U_EXPORT2
michael@0 218 utimer_getTime(UTimer* timer){
michael@0 219 uprv_initFrequency(timer);
michael@0 220 uprv_start(timer);
michael@0 221 }
michael@0 222
michael@0 223 /**
michael@0 224 * Returns the difference in times between timer1 and timer2 by subtracting
michael@0 225 * timer1's time from timer2's time
michael@0 226 *
michael@0 227 * @param timer1 A pointer to UTimer struct to be used as starting time
michael@0 228 * @param timer2 A pointer to UTimer struct to be used as end time
michael@0 229 * @return Time in seconds
michael@0 230 */
michael@0 231 static inline double U_EXPORT2
michael@0 232 utimer_getDeltaSeconds(UTimer* timer1, UTimer* timer2){
michael@0 233 if(uprv_compareFrequency(timer1,timer2)){
michael@0 234 return uprv_delta(timer1,timer2);
michael@0 235 }
michael@0 236 /* got error return -1 */
michael@0 237 return -1;
michael@0 238 }
michael@0 239
michael@0 240 /**
michael@0 241 * Returns the time elapsed from the starting time represented by the
michael@0 242 * UTimer struct pointer passed
michael@0 243 * @param timer A pointer to UTimer struct to be used as starting time
michael@0 244 * @return Time elapsed in seconds
michael@0 245 */
michael@0 246 static inline double U_EXPORT2
michael@0 247 utimer_getElapsedSeconds(UTimer* timer){
michael@0 248 UTimer temp;
michael@0 249 utimer_getTime(&temp);
michael@0 250 return uprv_delta(timer,&temp);
michael@0 251 }
michael@0 252
michael@0 253 /**
michael@0 254 * Executes the function pointed to for a given time and returns exact time
michael@0 255 * taken and number of iterations of the loop
michael@0 256 * @param thresholTimeVal
michael@0 257 * @param loopCount output param to recieve the number of iterations
michael@0 258 * @param fn The funtion to be executed
michael@0 259 * @param param Parameters to be passed to the fn
michael@0 260 * @return the time elapsed in seconds
michael@0 261 */
michael@0 262 static inline double U_EXPORT2
michael@0 263 utimer_loopUntilDone(double thresholdTimeVal,
michael@0 264 int32_t* loopCount,
michael@0 265 FuntionToBeTimed fn,
michael@0 266 void* param){
michael@0 267 UTimer timer;
michael@0 268 double currentVal=0;
michael@0 269 *loopCount = 0;
michael@0 270 utimer_getTime(&timer);
michael@0 271 for(;currentVal<thresholdTimeVal;){
michael@0 272 fn(param);
michael@0 273 currentVal = utimer_getElapsedSeconds(&timer);
michael@0 274 (*loopCount)++;
michael@0 275 }
michael@0 276 return currentVal;
michael@0 277 }
michael@0 278
michael@0 279 #endif
michael@0 280

mercurial