1 /* arc4random benchmarks.
2 Copyright (C) 2022 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 <https://www.gnu.org/licenses/>. */
19 #include "bench-timing.h"
20 #include "bench-util.h"
22 #include <array_length.h>
28 #include <support/support.h>
29 #include <support/timespec.h>
30 #include <support/xthread.h>
32 static volatile sig_atomic_t timer_finished
;
34 static void timer_callback (int unused
)
41 /* Run for approximately DURATION seconds, and it does not matter who
42 receive the signal (so not need to mask it on main thread). */
47 timer
= support_create_timer (DURATION
, 0, false, timer_callback
);
52 support_delete_timer (timer
);
55 static const uint32_t sizes
[] = { 0, 16, 32, 48, 64, 80, 96, 112, 128 };
58 bench_throughput (void)
62 struct timespec start
, end
;
63 clock_gettime (CLOCK_MONOTONIC
, &start
);
66 DO_NOT_OPTIMIZE_OUT (arc4random ());
69 if (timer_finished
== 1)
72 clock_gettime (CLOCK_MONOTONIC
, &end
);
73 struct timespec diff
= timespec_sub (end
, start
);
75 double total
= (double) n
* sizeof (uint32_t);
76 double duration
= (double) diff
.tv_sec
77 + (double) diff
.tv_nsec
/ TIMESPEC_HZ
;
79 return total
/ duration
;
85 timing_t start
, stop
, cur
;
86 const size_t iters
= 1024;
89 for (size_t i
= 0; i
< iters
; i
++)
90 DO_NOT_OPTIMIZE_OUT (arc4random ());
93 TIMING_DIFF (cur
, start
, stop
);
95 return (double) (cur
) / (double) iters
;
99 bench_buf_throughput (size_t len
)
104 struct timespec start
, end
;
105 clock_gettime (CLOCK_MONOTONIC
, &start
);
108 arc4random_buf (buf
, len
);
111 if (timer_finished
== 1)
114 clock_gettime (CLOCK_MONOTONIC
, &end
);
115 struct timespec diff
= timespec_sub (end
, start
);
117 double total
= (double) n
* len
;
118 double duration
= (double) diff
.tv_sec
119 + (double) diff
.tv_nsec
/ TIMESPEC_HZ
;
121 return total
/ duration
;
125 bench_buf_latency (size_t len
)
127 timing_t start
, stop
, cur
;
128 const size_t iters
= 1024;
133 for (size_t i
= 0; i
< iters
; i
++)
134 arc4random_buf (buf
, len
);
137 TIMING_DIFF (cur
, start
, stop
);
139 return (double) (cur
) / (double) iters
;
143 bench_singlethread (json_ctx_t
*json_ctx
)
145 json_element_object_begin (json_ctx
);
147 json_array_begin (json_ctx
, "throughput");
148 for (int i
= 0; i
< array_length (sizes
); i
++)
151 double r
= sizes
[i
] == 0
152 ? bench_throughput () : bench_buf_throughput (sizes
[i
]);
155 json_element_double (json_ctx
, r
);
157 json_array_end (json_ctx
);
159 json_array_begin (json_ctx
, "latency");
160 for (int i
= 0; i
< array_length (sizes
); i
++)
163 double r
= sizes
[i
] == 0
164 ? bench_latency () : bench_buf_latency (sizes
[i
]);
167 json_element_double (json_ctx
, r
);
169 json_array_end (json_ctx
);
171 json_element_object_end (json_ctx
);
175 run_bench (json_ctx_t
*json_ctx
, const char *name
,
176 char *const*fnames
, size_t fnameslen
,
177 void (*bench
) (json_ctx_t
*ctx
))
179 json_attr_object_begin (json_ctx
, name
);
180 json_array_begin (json_ctx
, "functions");
181 for (int i
= 0; i
< fnameslen
; i
++)
182 json_element_string (json_ctx
, fnames
[i
]);
183 json_array_end (json_ctx
);
185 json_array_begin (json_ctx
, "results");
187 json_array_end (json_ctx
);
188 json_attr_object_end (json_ctx
);
194 char *fnames
[array_length (sizes
)];
195 for (int i
= 0; i
< array_length (sizes
); i
++)
197 fnames
[i
] = xasprintf ("arc4random");
199 fnames
[i
] = xasprintf ("arc4random_buf(%u)", sizes
[i
]);
202 json_init (&json_ctx
, 0, stdout
);
204 json_document_begin (&json_ctx
);
205 json_attr_string (&json_ctx
, "timing_type", TIMING_TYPE
);
207 run_bench (&json_ctx
, "single-thread", fnames
, array_length (fnames
),
210 json_document_end (&json_ctx
);
212 for (int i
= 0; i
< array_length (sizes
); i
++)
218 #include <support/test-driver.c>