diff options
author | Siddhesh Poyarekar <siddhesh@redhat.com> | 2013-04-30 14:10:20 +0530 |
---|---|---|
committer | Siddhesh Poyarekar <siddhesh@redhat.com> | 2013-04-30 14:10:20 +0530 |
commit | d569c6eeb48219993063f956e516704281602f7d (patch) | |
tree | ee4b52c88456865a025c5943ff6f0bb5b741ff0a /benchtests/bench-skeleton.c | |
parent | a6a242fe7cc0d7fcac1c9741d8be2ca8c2a5c744 (diff) | |
download | glibc-d569c6eeb48219993063f956e516704281602f7d.tar.gz glibc-d569c6eeb48219993063f956e516704281602f7d.tar.xz glibc-d569c6eeb48219993063f956e516704281602f7d.zip |
Maintain runtime of each benchmark at ~10 seconds
The idea to run benchmarks for a constant number of iterations is problematic. While the benchmarks may run for 10 seconds on x86_64, they could run for about 30 seconds on powerpc and worse, over 3 minutes on arm. Besides that, adding a new benchmark is cumbersome since one needs to find out the number of iterations needed for a sufficient runtime. A better idea would be to run each benchmark for a specific amount of time. This patch does just that. The run time defaults to 10 seconds and it is configurable at command line: make BENCH_DURATION=5 bench
Diffstat (limited to 'benchtests/bench-skeleton.c')
-rw-r--r-- | benchtests/bench-skeleton.c | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/benchtests/bench-skeleton.c b/benchtests/bench-skeleton.c index 13f986d817..bbd151b0ec 100644 --- a/benchtests/bench-skeleton.c +++ b/benchtests/bench-skeleton.c @@ -22,13 +22,18 @@ #include <time.h> #include <inttypes.h> +#define TIMESPEC_AFTER(a, b) \ + (((a).tv_sec == (b).tv_sec) ? \ + ((a).tv_nsec > (b).tv_nsec) : \ + ((a).tv_sec > (b).tv_sec)) int main (int argc, char **argv) { - unsigned long i, j, k; + unsigned long i, k; uint64_t total = 0, max = 0, min = 0x7fffffffffffffff; - struct timespec start, end; + struct timespec start, end, runtime; + memset (&runtime, 0, sizeof (runtime)); memset (&start, 0, sizeof (start)); memset (&end, 0, sizeof (end)); @@ -39,11 +44,15 @@ main (int argc, char **argv) Measurements close to the minimum clock resolution won't make much sense, but it's better than having nothing at all. */ unsigned long iters = 1000 * start.tv_nsec; - unsigned long total_iters = ITER / iters; - for (i = 0; i < NUM_SAMPLES; i++) + /* Run for approxmately DURATION seconds. */ + clock_gettime (CLOCK_MONOTONIC_RAW, &runtime); + runtime.tv_sec += DURATION; + + double d_total_i = 0; + while (1) { - for (j = 0; j < total_iters; j ++) + for (i = 0; i < NUM_SAMPLES; i++) { clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &start); for (k = 0; k < iters; k++) @@ -61,12 +70,25 @@ main (int argc, char **argv) min = cur; total += cur; + + d_total_i += iters; } + + struct timespec curtime; + + memset (&curtime, 0, sizeof (curtime)); + clock_gettime (CLOCK_MONOTONIC_RAW, &curtime); + if (TIMESPEC_AFTER (curtime, runtime)) + goto done; } - double d_total_s = total * 1e-9; - double d_iters = iters; - double d_total_i = (double)ITER * NUM_SAMPLES; + double d_total_s; + double d_iters; + + done: + d_total_s = total * 1e-9; + d_iters = iters; + printf (FUNCNAME ": ITERS:%g: TOTAL:%gs, MAX:%gns, MIN:%gns, %g iter/s\n", d_total_i, d_total_s, max / d_iters, min / d_iters, d_total_i / d_total_s); |