diff options
Diffstat (limited to 'src/benchmark.c')
-rw-r--r-- | src/benchmark.c | 186 |
1 files changed, 90 insertions, 96 deletions
diff --git a/src/benchmark.c b/src/benchmark.c index 6cc79e2404..f8a9562529 100644 --- a/src/benchmark.c +++ b/src/benchmark.c @@ -32,130 +32,124 @@ int benchmark_must_finish = 0; #if defined(_WIN32) #include <windows.h> -DWORD WINAPI -alarm_handler (LPVOID lpParameter) +DWORD WINAPI alarm_handler(LPVOID lpParameter) { - HANDLE wtimer = *((HANDLE *) lpParameter); - WaitForSingleObject (wtimer, INFINITE); - benchmark_must_finish = 1; - return 0; + HANDLE wtimer = *((HANDLE *) lpParameter); + WaitForSingleObject(wtimer, INFINITE); + benchmark_must_finish = 1; + return 0; } #else -static void -alarm_handler (int signo) +static void alarm_handler(int signo) { - benchmark_must_finish = 1; + benchmark_must_finish = 1; } #endif static void -value2human (unsigned long bytes, double time, double *data, double *speed, - char *metric) +value2human(unsigned long bytes, double time, double *data, double *speed, + char *metric) { - if (bytes > 1000 && bytes < 1000 * 1000) - { - *data = ((double) bytes) / 1000; - *speed = *data / time; - strcpy (metric, "KB"); - return; - } - else if (bytes >= 1000 * 1000 && bytes < 1000 * 1000 * 1000) - { - *data = ((double) bytes) / (1000 * 1000); - *speed = *data / time; - strcpy (metric, "MB"); - return; - } - else if (bytes >= 1000 * 1000 * 1000) - { - *data = ((double) bytes) / (1000 * 1000 * 1000); - *speed = *data / time; - strcpy (metric, "GB"); - return; - } - else - { - *data = (double) bytes; - *speed = *data / time; - strcpy (metric, "bytes"); - return; - } + if (bytes > 1000 && bytes < 1000 * 1000) { + *data = ((double) bytes) / 1000; + *speed = *data / time; + strcpy(metric, "KB"); + return; + } else if (bytes >= 1000 * 1000 && bytes < 1000 * 1000 * 1000) { + *data = ((double) bytes) / (1000 * 1000); + *speed = *data / time; + strcpy(metric, "MB"); + return; + } else if (bytes >= 1000 * 1000 * 1000) { + *data = ((double) bytes) / (1000 * 1000 * 1000); + *speed = *data / time; + strcpy(metric, "GB"); + return; + } else { + *data = (double) bytes; + *speed = *data / time; + strcpy(metric, "bytes"); + return; + } } -void start_benchmark(struct benchmark_st * st) +void start_benchmark(struct benchmark_st *st) { - memset(st, 0, sizeof(*st)); + memset(st, 0, sizeof(*st)); #ifndef _WIN32 - st->old_handler = signal (SIGALRM, alarm_handler); + st->old_handler = signal(SIGALRM, alarm_handler); #endif - gettime (&st->start); - benchmark_must_finish = 0; + gettime(&st->start); + benchmark_must_finish = 0; #if defined(_WIN32) - st->wtimer = CreateWaitableTimer (NULL, TRUE, NULL); - if (st->wtimer == NULL) - { - fprintf (stderr, "error: CreateWaitableTimer %u\n", GetLastError ()); - exit(1); - } - st->wthread = CreateThread (NULL, 0, alarm_handler, &st->wtimer, 0, NULL); - if (st->wthread == NULL) - { - fprintf (stderr, "error: CreateThread %u\n", GetLastError ()); - exit(1); - } - st->alarm_timeout.QuadPart = (BSECS) * 10000000; - if (SetWaitableTimer (st->wtimer, &st->alarm_timeout, 0, NULL, NULL, FALSE) == 0) - { - fprintf (stderr, "error: SetWaitableTimer %u\n", GetLastError ()); - exit(1); - } + st->wtimer = CreateWaitableTimer(NULL, TRUE, NULL); + if (st->wtimer == NULL) { + fprintf(stderr, "error: CreateWaitableTimer %u\n", + GetLastError()); + exit(1); + } + st->wthread = + CreateThread(NULL, 0, alarm_handler, &st->wtimer, 0, NULL); + if (st->wthread == NULL) { + fprintf(stderr, "error: CreateThread %u\n", + GetLastError()); + exit(1); + } + st->alarm_timeout.QuadPart = (BSECS) * 10000000; + if (SetWaitableTimer + (st->wtimer, &st->alarm_timeout, 0, NULL, NULL, FALSE) == 0) { + fprintf(stderr, "error: SetWaitableTimer %u\n", + GetLastError()); + exit(1); + } #else - alarm (BSECS); + alarm(BSECS); #endif - + } /* returns the elapsed time */ -double stop_benchmark(struct benchmark_st * st, const char* metric, int quiet) +double stop_benchmark(struct benchmark_st *st, const char *metric, + int quiet) { - double secs; - unsigned long lsecs; - struct timespec stop; - double dspeed, ddata; - char imetric[16]; + double secs; + unsigned long lsecs; + struct timespec stop; + double dspeed, ddata; + char imetric[16]; #if defined(_WIN32) - if (st->wtimer != NULL) - CloseHandle (st->wtimer); - if (st->wthread != NULL) - CloseHandle (st->wthread); + if (st->wtimer != NULL) + CloseHandle(st->wtimer); + if (st->wthread != NULL) + CloseHandle(st->wthread); #else - signal(SIGALRM, st->old_handler); + signal(SIGALRM, st->old_handler); #endif - gettime (&stop); + gettime(&stop); - lsecs = (stop.tv_sec * 1000 + stop.tv_nsec / (1000 * 1000) - - (st->start.tv_sec * 1000 + st->start.tv_nsec / (1000 * 1000))); - secs = lsecs; - secs /= 1000; + lsecs = (stop.tv_sec * 1000 + stop.tv_nsec / (1000 * 1000) - + (st->start.tv_sec * 1000 + + st->start.tv_nsec / (1000 * 1000))); + secs = lsecs; + secs /= 1000; - if (metric == NULL) - { /* assume bytes/sec */ - value2human (st->size, secs, &ddata, &dspeed, imetric); - if (quiet == 0) - printf (" Processed %.2f %s in %.2f secs: ", ddata, imetric, secs); - printf ("%.2f %s/sec\n", dspeed, imetric); - } - else - { - ddata = (double) st->size; - dspeed = ddata / secs; - if (quiet == 0) - printf (" Processed %.2f %s in %.2f secs: ", ddata, metric, secs); - printf ("%.2f %s/sec\n", dspeed, metric); - } + if (metric == NULL) { /* assume bytes/sec */ + value2human(st->size, secs, &ddata, &dspeed, imetric); + if (quiet == 0) + printf(" Processed %.2f %s in %.2f secs: ", ddata, + imetric, secs); + printf("%.2f %s/sec\n", dspeed, imetric); + } else { + ddata = (double) st->size; + dspeed = ddata / secs; + if (quiet == 0) + printf(" Processed %.2f %s in %.2f secs: ", ddata, + metric, secs); + printf("%.2f %s/sec\n", dspeed, metric); + } - return secs; + return secs; } |