app/test: improve accuracy on hash measurements
[dpdk.git] / app / test / test_hash_functions.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <stdint.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <stdarg.h>
39 #include <errno.h>
40 #include <sys/queue.h>
41
42 #include <rte_cycles.h>
43 #include <rte_random.h>
44 #include <rte_hash.h>
45 #include <rte_jhash.h>
46 #include <rte_hash_crc.h>
47
48 #include "test.h"
49
50 /*******************************************************************************
51  * Hash function performance test configuration section. Each performance test
52  * will be performed HASHTEST_ITERATIONS times.
53  *
54  * The three arrays below control what tests are performed. Every combination
55  * from the array entries is tested.
56  */
57 #define HASHTEST_ITERATIONS 1000000
58
59 static rte_hash_function hashtest_funcs[] = {rte_jhash, rte_hash_crc};
60 static uint32_t hashtest_initvals[] = {0};
61 static uint32_t hashtest_key_lens[] = {2, 4, 5, 6, 7, 8, 10, 11, 15, 16, 21, 31, 32, 33, 63, 64};
62 /******************************************************************************/
63
64 /*
65  * To help print out name of hash functions.
66  */
67 static const char *
68 get_hash_name(rte_hash_function f)
69 {
70         if (f == rte_jhash)
71                 return "jhash";
72
73         if (f == rte_hash_crc)
74                 return "rte_hash_crc";
75
76         return "UnknownHash";
77 }
78
79 /*
80  * Test a hash function.
81  */
82 static void
83 run_hash_func_perf_test(rte_hash_function f, uint32_t init_val,
84                 uint32_t key_len)
85 {
86         static uint8_t key[HASHTEST_ITERATIONS][RTE_HASH_KEY_LENGTH_MAX];
87         uint64_t ticks, start, end;
88         unsigned i, j;
89
90         for (i = 0; i < HASHTEST_ITERATIONS; i++) {
91                 for (j = 0; j < key_len; j++)
92                         key[i][j] = (uint8_t) rte_rand();
93         }
94
95         start = rte_rdtsc();
96         for (i = 0; i < HASHTEST_ITERATIONS; i++)
97                 f(key[i], key_len, init_val);
98         end = rte_rdtsc();
99         ticks = end - start;
100
101         printf("%-12s, %-18u, %-13u, %.02f\n", get_hash_name(f), (unsigned) key_len,
102                         (unsigned) init_val, (double)ticks / HASHTEST_ITERATIONS);
103 }
104
105 /*
106  * Test all hash functions.
107  */
108 static void
109 run_hash_func_perf_tests(void)
110 {
111         unsigned i, j, k;
112
113         printf(" *** Hash function performance test results ***\n");
114         printf(" Number of iterations for each test = %d\n",
115                         HASHTEST_ITERATIONS);
116         printf("Hash Func.  , Key Length (bytes), Initial value, Ticks/Op.\n");
117
118         for (i = 0;
119              i < sizeof(hashtest_funcs) / sizeof(rte_hash_function);
120              i++) {
121                 for (j = 0;
122                      j < sizeof(hashtest_initvals) / sizeof(uint32_t);
123                      j++) {
124                         for (k = 0;
125                              k < sizeof(hashtest_key_lens) / sizeof(uint32_t);
126                              k++) {
127                                 run_hash_func_perf_test(hashtest_funcs[i],
128                                                 hashtest_initvals[j],
129                                                 hashtest_key_lens[k]);
130                         }
131                 }
132         }
133 }
134
135 static int
136 test_hash_functions(void)
137 {
138         run_hash_func_perf_tests();
139
140         return 0;
141 }
142
143 static struct test_command hash_functions_cmd = {
144         .command = "hash_functions_autotest",
145         .callback = test_hash_functions,
146 };
147 REGISTER_TEST_COMMAND(hash_functions_cmd);