00001
00002 #include "timer.hh"
00003
00004
00005 #include <stdio.h>
00006 #include <signal.h>
00007 #include <stdlib.h>
00008 #include <string.h>
00009
00010 #include <sys/time.h>
00011 #include <sys/types.h>
00012 #include <unistd.h>
00013
00014
00015 extern "C" void quittimetest ();
00016
00017 int quit_timetest_flag = 0;
00018
00019 static void
00020 quittimetest (int sig)
00021 {
00022 quit_timetest_flag = 1;
00023 }
00024
00025 double period = 0.001;
00026
00027 #define MAX_HIST 10000
00028 int hist[MAX_HIST];
00029
00030 #ifdef VXWORKS
00031 extern "C" int timetest ();
00032
00033 int
00034 timetest ()
00035 #else
00036 int
00037 main (int argc, char **argv)
00038 #endif
00039 {
00040
00041 int count = 0;
00042 int hist_index;
00043 int lo_hist_index = MAX_HIST;
00044 int hi_hist_index = 0;
00045 double start_time, end_time;
00046 double avg_time, total_time;
00047 double min_time = 1E99;
00048 double max_time = -1E99;
00049 double last_time;
00050
00051 if (argc > 1)
00052 {
00053 period = strtod (argv[1], 0);
00054 }
00055 signal (SIGINT, quittimetest);
00056 start_time = etime ();
00057 last_time = start_time;
00058
00059 for (int i = 0; i < MAX_HIST; i++)
00060 {
00061 hist[i] = 0;
00062 }
00063
00064 last_time = etime ();
00065 esleep (period);
00066 while (!quit_timetest_flag)
00067 {
00068 count++;
00069 double cur_time = etime ();
00070 double diff = (cur_time - last_time);
00071 if (min_time > diff)
00072 {
00073 min_time = diff;
00074 }
00075 if (max_time < diff)
00076 {
00077 max_time = diff;
00078 }
00079 hist_index = ((int) (sqrt (MAX_HIST) * diff / period));
00080 if (hist_index < 0)
00081 {
00082 hist_index = 0;
00083 }
00084 if (hist_index > MAX_HIST - 1)
00085 {
00086 hist_index = MAX_HIST - 1;
00087 }
00088 if (hi_hist_index < hist_index)
00089 {
00090 hi_hist_index = hist_index;
00091 }
00092 if (lo_hist_index > hist_index)
00093 {
00094 lo_hist_index = hist_index;
00095 }
00096 hist[hist_index]++;
00097 if (count % ((int) (5.0 / period)) == 0 || period > 5.0)
00098 {
00099 total_time = (cur_time - start_time);
00100 avg_time = count > 0 ? total_time / count : -1;
00101 printf
00102 ("count: %d min_time:%f max_time:%f total_time:%f avg_time: %f\n",
00103 count, min_time, max_time, total_time, avg_time);
00104 }
00105 last_time = cur_time;
00106 esleep (period);
00107 }
00108
00109 end_time = etime ();
00110 total_time = end_time - start_time;
00111 avg_time = count > 0 ? total_time / count : -1;
00112 printf ("count: %d min_time:%f max_time:%f total_time:%f avg_time: %f\n",
00113 count, min_time, max_time, total_time, avg_time);
00114 printf ("\n\n");
00115 printf ("time:%f\n", total_time);
00116 printf ("count:%d\n", count);
00117 if (count > 0)
00118 {
00119 printf ("period:%f\n", total_time / count);
00120 }
00121 if (total_time > 0.0)
00122 {
00123 printf ("frequency:%f\n", count / total_time);
00124 }
00125
00126 printf ("\n\nHistogram:\n");
00127 printf ("Period \tCount \tPercent\n");
00128 for (int i = lo_hist_index; i <= hi_hist_index; i++)
00129 {
00130 if (i > lo_hist_index + 2 && i < hi_hist_index - 1 && hist[i] == 0
00131 && hist[i - 1] == 0 && hist[i + 1] == 0)
00132 {
00133 if (hist[i - 2] != 0)
00134 {
00135 printf (" . . . \n");
00136 }
00137 continue;
00138 }
00139 printf ("%6.6f \t%6.6d \t%6.6f%\n", period / sqrt (MAX_HIST) * i,
00140 hist[i], 100 * ((double) hist[i] / count));;
00141 }
00142 printf ("\n");
00143
00144
00145 #ifdef VXWORKS
00146 return (0);
00147 #endif
00148 }