]> git.ipfire.org Git - thirdparty/glibc.git/blob - benchtests/bench-skeleton.c
Update nss tests to new skeleton
[thirdparty/glibc.git] / benchtests / bench-skeleton.c
1 /* Skeleton for benchmark programs.
2 Copyright (C) 2013-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
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"
28
29 #include "bench-util.c"
30
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)
37 {
38 unsigned long i, k;
39 struct timespec runtime;
40 timing_t start, end;
41 bool detailed = false;
42 json_ctx_t json_ctx;
43
44 if (argc == 2 && !strcmp (argv[1], "-d"))
45 detailed = true;
46
47 bench_start ();
48
49 memset (&runtime, 0, sizeof (runtime));
50
51 unsigned long iters, res;
52
53 #ifdef BENCH_INIT
54 BENCH_INIT ();
55 #endif
56 TIMING_INIT (res);
57
58 iters = 1000 * res;
59
60 json_init (&json_ctx, 2, stdout);
61
62 /* Begin function. */
63 json_attr_object_begin (&json_ctx, FUNCNAME);
64
65 for (int v = 0; v < NUM_VARIANTS; v++)
66 {
67 /* Run for approximately DURATION seconds. */
68 clock_gettime (CLOCK_MONOTONIC_RAW, &runtime);
69 runtime.tv_sec += DURATION;
70
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 timing_t throughput = 0, latency = 0;
75 int64_t c = 0;
76 uint64_t cur;
77 BENCH_VARS;
78 while (1)
79 {
80 if (is_bench)
81 {
82 /* Benchmark a real trace of calls - all samples are iterated
83 over once before repeating. This models actual use more
84 accurately than repeating the same sample many times. */
85 TIMING_NOW (start);
86 for (k = 0; k < iters; k++)
87 for (i = 0; i < NUM_SAMPLES (v); i++)
88 BENCH_FUNC (v, i);
89 TIMING_NOW (end);
90 TIMING_DIFF (cur, start, end);
91 TIMING_ACCUM (throughput, cur);
92
93 TIMING_NOW (start);
94 for (k = 0; k < iters; k++)
95 for (i = 0; i < NUM_SAMPLES (v); i++)
96 BENCH_FUNC_LAT (v, i);
97 TIMING_NOW (end);
98 TIMING_DIFF (cur, start, end);
99 TIMING_ACCUM (latency, cur);
100
101 d_total_i += iters * NUM_SAMPLES (v);
102 }
103 else
104 for (i = 0; i < NUM_SAMPLES (v); i++)
105 {
106 TIMING_NOW (start);
107 for (k = 0; k < iters; k++)
108 BENCH_FUNC (v, i);
109 TIMING_NOW (end);
110
111 TIMING_DIFF (cur, start, end);
112
113 if (cur > max)
114 max = cur;
115
116 if (cur < min)
117 min = cur;
118
119 TIMING_ACCUM (total, cur);
120 /* Accumulate timings for the value. In the end we will divide
121 by the total iterations. */
122 RESULT_ACCUM (cur, v, i, c * iters, (c + 1) * iters);
123
124 d_total_i += iters;
125 }
126 c++;
127 struct timespec curtime;
128
129 memset (&curtime, 0, sizeof (curtime));
130 clock_gettime (CLOCK_MONOTONIC_RAW, &curtime);
131 if (TIMESPEC_AFTER (curtime, runtime))
132 goto done;
133 }
134
135 double d_total_s;
136 double d_iters;
137
138 done:
139 d_total_s = total;
140 d_iters = iters;
141
142 /* Begin variant. */
143 json_attr_object_begin (&json_ctx, VARIANT (v));
144
145 if (is_bench)
146 {
147 json_attr_double (&json_ctx, "reciprocal-throughput",
148 throughput / d_total_i);
149 json_attr_double (&json_ctx, "latency", latency / d_total_i);
150 json_attr_double (&json_ctx, "max-throughput",
151 d_total_i / throughput * 1000000000.0);
152 json_attr_double (&json_ctx, "min-throughput",
153 d_total_i / latency * 1000000000.0);
154 }
155 else
156 {
157 json_attr_double (&json_ctx, "duration", d_total_s);
158 json_attr_double (&json_ctx, "iterations", d_total_i);
159 json_attr_double (&json_ctx, "max", max / d_iters);
160 json_attr_double (&json_ctx, "min", min / d_iters);
161 json_attr_double (&json_ctx, "mean", d_total_s / d_total_i);
162 }
163
164 if (detailed && !is_bench)
165 {
166 json_array_begin (&json_ctx, "timings");
167
168 for (int i = 0; i < NUM_SAMPLES (v); i++)
169 json_element_double (&json_ctx, RESULT (v, i));
170
171 json_array_end (&json_ctx);
172 }
173
174 /* End variant. */
175 json_attr_object_end (&json_ctx);
176 }
177
178 /* End function. */
179 json_attr_object_end (&json_ctx);
180
181 return 0;
182 }