eal: replace libc-based random generation with LFSR
[dpdk.git] / app / test / test_rand_perf.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019 Ericsson AB
3  */
4
5 #include <inttypes.h>
6 #include <stdio.h>
7
8 #include <rte_common.h>
9 #include <rte_cycles.h>
10 #include <rte_random.h>
11
12 #include "test.h"
13
14 static volatile uint64_t vsum;
15
16 #define ITERATIONS (100000000)
17
18 enum rand_type {
19         rand_type_64
20 };
21
22 static const char *
23 rand_type_desc(enum rand_type rand_type)
24 {
25         switch (rand_type) {
26         case rand_type_64:
27                 return "Full 64-bit [rte_rand()]";
28         default:
29                 return NULL;
30         }
31 }
32
33 static __rte_always_inline void
34 test_rand_perf_type(enum rand_type rand_type)
35 {
36         uint64_t start;
37         uint32_t i;
38         uint64_t end;
39         uint64_t sum = 0;
40         uint64_t op_latency;
41
42         start = rte_rdtsc();
43
44         for (i = 0; i < ITERATIONS; i++) {
45                 switch (rand_type) {
46                 case rand_type_64:
47                         sum += rte_rand();
48                         break;
49                 }
50         }
51
52         end = rte_rdtsc();
53
54         /* to avoid an optimizing compiler removing the whole loop */
55         vsum = sum;
56
57         op_latency = (end - start) / ITERATIONS;
58
59         printf("%s: %"PRId64" TSC cycles/op\n", rand_type_desc(rand_type),
60                op_latency);
61 }
62
63 static int
64 test_rand_perf(void)
65 {
66         rte_srand(42);
67
68         printf("Pseudo-random number generation latencies:\n");
69
70         test_rand_perf_type(rand_type_64);
71
72         return 0;
73 }
74
75 REGISTER_TEST_COMMAND(rand_perf_autotest, test_rand_perf);