summaryrefslogtreecommitdiffstats
path: root/src/spdk/dpdk/app/test/test_rand_perf.c
blob: fe797ebfa1ca8c1089d79590c5abb8f4d606bd9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2019 Ericsson AB
 */

#include <inttypes.h>
#include <stdio.h>

#include <rte_common.h>
#include <rte_cycles.h>
#include <rte_random.h>

#include "test.h"

static volatile uint64_t vsum;

#define ITERATIONS (100000000)

#define BEST_CASE_BOUND (1<<16)
#define WORST_CASE_BOUND (BEST_CASE_BOUND + 1)

enum rand_type {
	rand_type_64,
	rand_type_bounded_best_case,
	rand_type_bounded_worst_case
};

static const char *
rand_type_desc(enum rand_type rand_type)
{
	switch (rand_type) {
	case rand_type_64:
		return "Full 64-bit [rte_rand()]";
	case rand_type_bounded_best_case:
		return "Bounded average best-case [rte_rand_max()]";
	case rand_type_bounded_worst_case:
		return "Bounded average worst-case [rte_rand_max()]";
	default:
		return NULL;
	}
}

static __rte_always_inline void
test_rand_perf_type(enum rand_type rand_type)
{
	uint64_t start;
	uint32_t i;
	uint64_t end;
	uint64_t sum = 0;
	uint64_t op_latency;

	start = rte_rdtsc();

	for (i = 0; i < ITERATIONS; i++) {
		switch (rand_type) {
		case rand_type_64:
			sum += rte_rand();
			break;
		case rand_type_bounded_best_case:
			sum += rte_rand_max(BEST_CASE_BOUND);
			break;
		case rand_type_bounded_worst_case:
			sum += rte_rand_max(WORST_CASE_BOUND);
			break;
		}
	}

	end = rte_rdtsc();

	/* to avoid an optimizing compiler removing the whole loop */
	vsum = sum;

	op_latency = (end - start) / ITERATIONS;

	printf("%s: %"PRId64" TSC cycles/op\n", rand_type_desc(rand_type),
	       op_latency);
}

static int
test_rand_perf(void)
{
	rte_srand(42);

	printf("Pseudo-random number generation latencies:\n");

	test_rand_perf_type(rand_type_64);
	test_rand_perf_type(rand_type_bounded_best_case);
	test_rand_perf_type(rand_type_bounded_worst_case);

	return 0;
}

REGISTER_TEST_COMMAND(rand_perf_autotest, test_rand_perf);