static void do_one_test (json_ctx_t *json_ctx, proto_t test_fn, volatile double *arr, size_t len, const char *testname) { size_t iters = 500; timing_t start, stop, cur; json_attr_object_begin (json_ctx, testname); TIMING_NOW (start); test_fn (arr, len, iters); TIMING_NOW (stop); TIMING_DIFF (cur, start, stop); json_attr_double (json_ctx, "duration", cur); json_attr_double (json_ctx, "iterations", iters); json_attr_double (json_ctx, "mean", cur / iters); json_attr_object_end (json_ctx); }
int main (int argc, char **argv) { unsigned long i, k; struct timespec runtime; timing_t start, end; bool detailed = false; json_ctx_t json_ctx; if (argc == 2 && !strcmp (argv[1], "-d")) detailed = true; bench_start (); memset (&runtime, 0, sizeof (runtime)); unsigned long iters, res; #ifdef BENCH_INIT BENCH_INIT (); #endif TIMING_INIT (res); iters = 1000 * res; json_init (&json_ctx, 2, stdout); /* Begin function. */ json_attr_object_begin (&json_ctx, FUNCNAME); for (int v = 0; v < NUM_VARIANTS; v++) { /* Run for approximately DURATION seconds. */ clock_gettime (CLOCK_MONOTONIC_RAW, &runtime); runtime.tv_sec += DURATION; double d_total_i = 0; timing_t total = 0, max = 0, min = 0x7fffffffffffffff; int64_t c = 0; while (1) { for (i = 0; i < NUM_SAMPLES (v); i++) { uint64_t cur; TIMING_NOW (start); for (k = 0; k < iters; k++) BENCH_FUNC (v, i); TIMING_NOW (end); TIMING_DIFF (cur, start, end); if (cur > max) max = cur; if (cur < min) min = cur; TIMING_ACCUM (total, cur); /* Accumulate timings for the value. In the end we will divide by the total iterations. */ RESULT_ACCUM (cur, v, i, c * iters, (c + 1) * iters); d_total_i += iters; } c++; 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; double d_iters; done: d_total_s = total; d_iters = iters; /* Begin variant. */ json_attr_object_begin (&json_ctx, VARIANT (v)); json_attr_double (&json_ctx, "duration", d_total_s); json_attr_double (&json_ctx, "iterations", d_total_i); json_attr_double (&json_ctx, "max", max / d_iters); json_attr_double (&json_ctx, "min", min / d_iters); json_attr_double (&json_ctx, "mean", d_total_s / d_total_i); if (detailed) { json_array_begin (&json_ctx, "timings"); for (int i = 0; i < NUM_SAMPLES (v); i++) json_element_double (&json_ctx, RESULT (v, i)); json_array_end (&json_ctx); } /* End variant. */ json_attr_object_end (&json_ctx); } /* End function. */ json_attr_object_end (&json_ctx); return 0; }
int main (int argc, char **argv) { timing_t cur; size_t iters = 0, num_threads = 1; unsigned long res; json_ctx_t json_ctx; double d_total_s, d_total_i; struct sigaction act; if (argc == 1) num_threads = 1; else if (argc == 2) { long ret; errno = 0; ret = strtol(argv[1], NULL, 10); if (errno || ret == 0) usage(argv[0]); num_threads = ret; } else usage(argv[0]); init_random_values (); json_init (&json_ctx, 0, stdout); json_document_begin (&json_ctx); json_attr_string (&json_ctx, "timing_type", TIMING_TYPE); json_attr_object_begin (&json_ctx, "functions"); json_attr_object_begin (&json_ctx, "malloc"); json_attr_object_begin (&json_ctx, ""); TIMING_INIT (res); (void) res; memset (&act, 0, sizeof (act)); act.sa_handler = &alarm_handler; sigaction (SIGALRM, &act, NULL); alarm (BENCHMARK_DURATION); cur = do_benchmark (num_threads, &iters); struct rusage usage; getrusage(RUSAGE_SELF, &usage); d_total_s = cur; d_total_i = iters; json_attr_double (&json_ctx, "duration", d_total_s); json_attr_double (&json_ctx, "iterations", d_total_i); json_attr_double (&json_ctx, "time_per_iteration", d_total_s / d_total_i); json_attr_double (&json_ctx, "max_rss", usage.ru_maxrss); json_attr_double (&json_ctx, "threads", num_threads); json_attr_double (&json_ctx, "min_size", MIN_ALLOCATION_SIZE); json_attr_double (&json_ctx, "max_size", MAX_ALLOCATION_SIZE); json_attr_double (&json_ctx, "random_seed", RAND_SEED); json_attr_object_end (&json_ctx); json_attr_object_end (&json_ctx); json_attr_object_end (&json_ctx); json_document_end (&json_ctx); return 0; }