1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 Ericsson AB
8 #include <rte_common.h>
9 #include <rte_cycles.h>
10 #include <rte_random.h>
14 static volatile uint64_t vsum;
16 #define ITERATIONS (100000000)
18 #define BEST_CASE_BOUND (1<<16)
19 #define WORST_CASE_BOUND (BEST_CASE_BOUND + 1)
24 rand_type_bounded_best_case,
25 rand_type_bounded_worst_case
29 rand_type_desc(enum rand_type rand_type)
33 return "Full 64-bit [rte_rand()]";
35 return "Floating point [rte_drand()]";
36 case rand_type_bounded_best_case:
37 return "Bounded average best-case [rte_rand_max()]";
38 case rand_type_bounded_worst_case:
39 return "Bounded average worst-case [rte_rand_max()]";
45 static __rte_always_inline void
46 test_rand_perf_type(enum rand_type rand_type)
56 for (i = 0; i < ITERATIONS; i++) {
62 sum += 1000. * rte_drand();
64 case rand_type_bounded_best_case:
65 sum += rte_rand_max(BEST_CASE_BOUND);
67 case rand_type_bounded_worst_case:
68 sum += rte_rand_max(WORST_CASE_BOUND);
75 /* to avoid an optimizing compiler removing the whole loop */
78 op_latency = (end - start) / ITERATIONS;
80 printf("%s: %"PRId64" TSC cycles/op\n", rand_type_desc(rand_type),
89 printf("Pseudo-random number generation latencies:\n");
91 test_rand_perf_type(rand_type_64);
92 test_rand_perf_type(rand_type_float);
93 test_rand_perf_type(rand_type_bounded_best_case);
94 test_rand_perf_type(rand_type_bounded_worst_case);
99 REGISTER_TEST_COMMAND(rand_perf_autotest, test_rand_perf);