| 1 |
#include <protoTimer.h> |
| 2 |
#include <stdio.h> |
| 3 |
#include <stdlib.h> // for rand() |
| 4 |
|
| 5 |
// This test program benchmarks the ProtoTimer insertion cost |
| 6 |
// as increasing numbers of timers are activated. |
| 7 |
|
| 8 |
|
| 9 |
// These functions measure CPU time used |
| 10 |
|
| 11 |
#ifdef WIN32 |
| 12 |
|
| 13 |
#include <Windows.h> |
| 14 |
double get_cpu_time() |
| 15 |
{ |
| 16 |
FILETIME a,b,c,d; |
| 17 |
if (GetProcessTimes(GetCurrentProcess(),&a,&b,&c,&d) != 0){ |
| 18 |
// Returns total user time. |
| 19 |
// Can be tweaked to include kernel times as well. |
| 20 |
return |
| 21 |
(double)(d.dwLowDateTime | |
| 22 |
((unsigned long long)d.dwHighDateTime << 32)) * 0.0000001; |
| 23 |
}else{ |
| 24 |
// Handle error |
| 25 |
return 0; |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
#else // UNIX |
| 30 |
|
| 31 |
#include <time.h> |
| 32 |
#include <sys/time.h> |
| 33 |
double get_cpu_time() |
| 34 |
{ |
| 35 |
return (double)clock() / CLOCKS_PER_SEC; |
| 36 |
} |
| 37 |
|
| 38 |
#endif // if/else WIN32/UNIX |
| 39 |
|
| 40 |
double UniformRand(double min, double max) |
| 41 |
{ |
| 42 |
double range = max - min; |
| 43 |
return (((((double)rand()) * range) / ((double)RAND_MAX)) + min); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
class TestTimerMgr : public ProtoTimerMgr |
| 48 |
{ |
| 49 |
public: |
| 50 |
bool UpdateSystemTimer(ProtoTimer::Command command, |
| 51 |
double delay) {return true;} |
| 52 |
|
| 53 |
void OnTimeout(ProtoTimer& theTimer) |
| 54 |
{ |
| 55 |
//fprintf(stderr, "timer timeout interval: %lf\n", theTimer.GetInterval()); |
| 56 |
count++; |
| 57 |
} |
| 58 |
|
| 59 |
unsigned int count; |
| 60 |
}; |
| 61 |
|
| 62 |
int main(int argc, char* argv[]) |
| 63 |
{ |
| 64 |
TestTimerMgr mgr; |
| 65 |
|
| 66 |
mgr.count = 0; |
| 67 |
|
| 68 |
fprintf(stderr, "sizeof(ProtoTimer) = %lu bytes\n", (unsigned long)sizeof(ProtoTimer)); |
| 69 |
ProtoTime t1, t2; |
| 70 |
|
| 71 |
/*for (int size = 2; size < 5000; size *= 1.5) |
| 72 |
{ |
| 73 |
double total = 0.0; |
| 74 |
ProtoTimer* array = new ProtoTimer[size]; |
| 75 |
int trials = 5000; |
| 76 |
for (int k = 0; k < trials; k++) |
| 77 |
{ |
| 78 |
t1.GetCurrentTime(); |
| 79 |
for (int i = 0; i < size; i++) |
| 80 |
{ |
| 81 |
double interval = UniformRand(0.0, 8.0); |
| 82 |
array[i].SetInterval(interval); |
| 83 |
mgr.ActivateTimer(array[i]); |
| 84 |
} |
| 85 |
t2.GetCurrentTime(); |
| 86 |
total += ProtoTime::Delta(t2, t1) / (double)size; |
| 87 |
for (int i = 0; i < size; i++) |
| 88 |
array[i].Deactivate(); |
| 89 |
} |
| 90 |
delete[] array; |
| 91 |
double ave = total / (double)trials; |
| 92 |
//printf("size:%d items average time: %lf seconds\n", size, ave); |
| 93 |
printf("%d, %e\n", size/2, ave); |
| 94 |
} |
| 95 |
*/ |
| 96 |
|
| 97 |
// Run a bunch of very short timouts and benchmark |
| 98 |
|
| 99 |
int trials = 10; |
| 100 |
//int size = 10000; |
| 101 |
|
| 102 |
for (int size = 2; size < 100000; size *= 1.5) |
| 103 |
{ |
| 104 |
double total = 0.0; |
| 105 |
for (int k = 0; k < trials; k++) |
| 106 |
{ |
| 107 |
ProtoTimer* array = new ProtoTimer[size]; |
| 108 |
for (int i = 0; i < size; i++) |
| 109 |
{ |
| 110 |
double interval = UniformRand(10.0e-06, 100.0e-06); |
| 111 |
array[i].SetInterval(interval); |
| 112 |
array[i].SetRepeat(-1); |
| 113 |
array[i].SetListener(&mgr, &TestTimerMgr::OnTimeout); |
| 114 |
mgr.ActivateTimer(array[i]); |
| 115 |
} |
| 116 |
|
| 117 |
t1.GetCurrentTime(); |
| 118 |
double elapsed = 0.0; |
| 119 |
double c1 = get_cpu_time(); |
| 120 |
while (elapsed < 0.5) |
| 121 |
{ |
| 122 |
double wait = mgr.GetTimeRemaining(); |
| 123 |
struct timespec ts; |
| 124 |
if (wait < 0) |
| 125 |
{ |
| 126 |
ts.tv_sec = 1; |
| 127 |
ts.tv_nsec = 0; |
| 128 |
} |
| 129 |
else |
| 130 |
{ |
| 131 |
ts.tv_sec = (time_t)wait; |
| 132 |
ts.tv_nsec = (wait - (time_t)wait)*1.0e+09; |
| 133 |
} |
| 134 |
//fprintf(stderr, "waiting %lf sec ...\n", wait); |
| 135 |
nanosleep(&ts, NULL); |
| 136 |
mgr.OnSystemTimeout(); |
| 137 |
t2.GetCurrentTime(); |
| 138 |
elapsed = ProtoTime::Delta(t2, t1); |
| 139 |
} |
| 140 |
double c2 = get_cpu_time(); |
| 141 |
total += c2 - c1; |
| 142 |
|
| 143 |
for (int i = 0; i < size; i++) |
| 144 |
array[i].Deactivate(); |
| 145 |
delete[] array; |
| 146 |
} |
| 147 |
printf("%d, %lf\n", size, total / (double)trials); |
| 148 |
fprintf(stderr, "%d, %lf\n", size, total / (double)trials); |
| 149 |
} |
| 150 |
|
| 151 |
} // end main() |