Reorganize and revise NEWS for 2.26.
[glibc.git] / benchtests / bench-skeleton.c
blob3c6dad705594ac0a53edcb4e09686252c13127cf
1 /* Skeleton for benchmark programs.
2 Copyright (C) 2013-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <string.h>
20 #include <stdint.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <time.h>
24 #include <inttypes.h>
25 #include "bench-timing.h"
26 #include "json-lib.h"
27 #include "bench-util.h"
29 #include "bench-util.c"
31 #define TIMESPEC_AFTER(a, b) \
32 (((a).tv_sec == (b).tv_sec) ? \
33 ((a).tv_nsec > (b).tv_nsec) : \
34 ((a).tv_sec > (b).tv_sec))
35 int
36 main (int argc, char **argv)
38 unsigned long i, k;
39 struct timespec runtime;
40 timing_t start, end;
41 bool detailed = false;
42 json_ctx_t json_ctx;
44 if (argc == 2 && !strcmp (argv[1], "-d"))
45 detailed = true;
47 bench_start ();
49 memset (&runtime, 0, sizeof (runtime));
51 unsigned long iters, res;
53 #ifdef BENCH_INIT
54 BENCH_INIT ();
55 #endif
56 TIMING_INIT (res);
58 iters = 1000 * res;
60 json_init (&json_ctx, 2, stdout);
62 /* Begin function. */
63 json_attr_object_begin (&json_ctx, FUNCNAME);
65 for (int v = 0; v < NUM_VARIANTS; v++)
67 /* Run for approximately DURATION seconds. */
68 clock_gettime (CLOCK_MONOTONIC_RAW, &runtime);
69 runtime.tv_sec += DURATION;
71 bool is_bench = strncmp (VARIANT (v), "workload-", 9) == 0;
72 double d_total_i = 0;
73 timing_t total = 0, max = 0, min = 0x7fffffffffffffff;
74 int64_t c = 0;
75 uint64_t cur;
76 while (1)
78 if (is_bench)
80 /* Benchmark a real trace of calls - all samples are iterated
81 over once before repeating. This models actual use more
82 accurately than repeating the same sample many times. */
83 TIMING_NOW (start);
84 for (k = 0; k < iters; k++)
85 for (i = 0; i < NUM_SAMPLES (v); i++)
86 BENCH_FUNC (v, i);
87 TIMING_NOW (end);
88 TIMING_DIFF (cur, start, end);
89 TIMING_ACCUM (total, cur);
90 d_total_i += iters * NUM_SAMPLES (v);
92 else
93 for (i = 0; i < NUM_SAMPLES (v); i++)
95 TIMING_NOW (start);
96 for (k = 0; k < iters; k++)
97 BENCH_FUNC (v, i);
98 TIMING_NOW (end);
100 TIMING_DIFF (cur, start, end);
102 if (cur > max)
103 max = cur;
105 if (cur < min)
106 min = cur;
108 TIMING_ACCUM (total, cur);
109 /* Accumulate timings for the value. In the end we will divide
110 by the total iterations. */
111 RESULT_ACCUM (cur, v, i, c * iters, (c + 1) * iters);
113 d_total_i += iters;
115 c++;
116 struct timespec curtime;
118 memset (&curtime, 0, sizeof (curtime));
119 clock_gettime (CLOCK_MONOTONIC_RAW, &curtime);
120 if (TIMESPEC_AFTER (curtime, runtime))
121 goto done;
124 double d_total_s;
125 double d_iters;
127 done:
128 d_total_s = total;
129 d_iters = iters;
131 /* Begin variant. */
132 json_attr_object_begin (&json_ctx, VARIANT (v));
134 json_attr_double (&json_ctx, "duration", d_total_s);
135 json_attr_double (&json_ctx, "iterations", d_total_i);
136 if (is_bench)
137 json_attr_double (&json_ctx, "throughput", d_total_s / d_total_i);
138 else
140 json_attr_double (&json_ctx, "max", max / d_iters);
141 json_attr_double (&json_ctx, "min", min / d_iters);
142 json_attr_double (&json_ctx, "mean", d_total_s / d_total_i);
145 if (detailed && !is_bench)
147 json_array_begin (&json_ctx, "timings");
149 for (int i = 0; i < NUM_SAMPLES (v); i++)
150 json_element_double (&json_ctx, RESULT (v, i));
152 json_array_end (&json_ctx);
155 /* End variant. */
156 json_attr_object_end (&json_ctx);
159 /* End function. */
160 json_attr_object_end (&json_ctx);
162 return 0;