/** * @file test/timertest.c * @brief testcase for util/timer.c; also measures how * precise the timers are. Expect values between 10 and 20 ms on * modern machines. */ #include "gnunet_util.h" #include "platform.h" #define VERBOSE NO static void semaphore_up(Semaphore * sem) { SEMAPHORE_UP(sem); } static int check() { cron_t now; cron_t last; TIME_T tnow; TIME_T tlast; int i; unsigned long long cumDelta; Semaphore * sem; /* test that time/cronTime are monotonically increasing; measure precision of sleep and report; test that sleep is interrupted by signals; */ last = cronTime(&now); if (last != now) return 1; tlast = TIME(&tnow); if (tlast != tnow) return 2; while (now == last) now = cronTime(NULL); while (tnow == tlast) tnow = TIME(NULL); if (now < last) return 3; if (tnow < tlast) return 4; cumDelta = 0; #define INCR 47 #define MAXV 1500 for (i=0;i 250 * cronMILLIS * MAXV / INCR) fprintf(stdout, "Timer precision is awful.\n"); else fprintf(stdout, "Timer precision is acceptable.\n"); sem = SEMAPHORE_NEW(0); startCron(); cumDelta = 0; #define MAXV2 1500 #define INCR2 113 for (i=50;i 250 * cronMILLIS * MAXV2 / INCR2) fprintf(stdout, "Timer precision is awful.\n"); else fprintf(stdout, "Timer precision is acceptable.\n"); stopCron(); SEMAPHORE_FREE(sem); return 0; } /** * Perform option parsing from the command line. */ static int parseCommandLine(int argc, char * argv[]) { char c; while (1) { int option_index = 0; static struct GNoption long_options[] = { { "config", 1, 0, 'c' }, { 0,0,0,0 } }; c = GNgetopt_long(argc, argv, "c:", long_options, &option_index); if (c == -1) break; /* No more flags to process */ switch(c) { case 'c': FREENONNULL(setConfigurationString("FILES", "gnunet.conf", GNoptarg)); break; } /* end of parsing commandline */ } FREENONNULL(setConfigurationString("GNUNETD", "LOGFILE", NULL)); FREENONNULL(setConfigurationString("GNUNETD", "LOGLEVEL", "WARNING")); return OK; } int main(int argc, char * argv[]){ int ret; initUtil(argc, argv, &parseCommandLine); ret = check(); if (ret != 0) fprintf(stderr, "ERROR %d\n", ret); doneUtil(); return ret; } /* end of timertest.c */