Move getlogin, getlogin_r, setlogin to login/ subdir.
[glibc.git] / benchtests / bench-skeleton.c
blob7359184ba8f4d7da1b64e896df668386f75bd1c3
1 /* Skeleton for benchmark programs.
2 Copyright (C) 2013 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 <stdio.h>
22 #include <time.h>
23 #include <inttypes.h>
25 #define TIMESPEC_AFTER(a, b) \
26 (((a).tv_sec == (b).tv_sec) ? \
27 ((a).tv_nsec > (b).tv_nsec) : \
28 ((a).tv_sec > (b).tv_sec))
29 int
30 main (int argc, char **argv)
32 unsigned long i, k;
33 struct timespec start, end, runtime;
35 memset (&runtime, 0, sizeof (runtime));
36 memset (&start, 0, sizeof (start));
37 memset (&end, 0, sizeof (end));
39 clock_getres (CLOCK_PROCESS_CPUTIME_ID, &start);
41 /* Measure 1000 times the resolution of the clock. So for a 1ns resolution
42 clock, we measure 1000 iterations of the function call at a time.
43 Measurements close to the minimum clock resolution won't make much sense,
44 but it's better than having nothing at all. */
45 unsigned long iters = 1000 * start.tv_nsec;
47 for (int v = 0; v < NUM_VARIANTS; v++)
49 /* Run for approximately DURATION seconds. */
50 clock_gettime (CLOCK_MONOTONIC_RAW, &runtime);
51 runtime.tv_sec += DURATION;
53 double d_total_i = 0;
54 uint64_t total = 0, max = 0, min = 0x7fffffffffffffff;
55 while (1)
57 for (i = 0; i < NUM_SAMPLES (v); i++)
59 clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &start);
60 for (k = 0; k < iters; k++)
61 BENCH_FUNC (v, i);
62 clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &end);
64 uint64_t cur = (end.tv_nsec - start.tv_nsec
65 + ((end.tv_sec - start.tv_sec)
66 * (uint64_t) 1000000000));
68 if (cur > max)
69 max = cur;
71 if (cur < min)
72 min = cur;
74 total += cur;
76 d_total_i += iters;
78 struct timespec curtime;
80 memset (&curtime, 0, sizeof (curtime));
81 clock_gettime (CLOCK_MONOTONIC_RAW, &curtime);
82 if (TIMESPEC_AFTER (curtime, runtime))
83 goto done;
86 double d_total_s;
87 double d_iters;
89 done:
90 d_total_s = total * 1e-9;
91 d_iters = iters;
93 printf ("%s: ITERS:%g: TOTAL:%gs, MAX:%gns, MIN:%gns, %g iter/s\n",
94 VARIANT (v),
95 d_total_i, d_total_s, max / d_iters, min / d_iters,
96 d_total_i / d_total_s);
99 return 0;