1 /* Measure hash functions runtime.
2 Copyright (C) 2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
19 #define TEST_MAIN
20 #ifndef TEST_FUNC
21 # error "No TEST_FUNC provided!"
22 #endif
23 #ifndef SIMPLE_TEST_FUNC
24 # error "No SIMPLE_TEST_FUNC provided!"
25 #endif
26
27 #ifndef TEST_NAME
28 # define STRINGIFY_PRIMITIVE(x) # x
29 # define STRINGIFY(x) STRINGIFY_PRIMITIVE (x)
30
31 # define TEST_NAME STRINGIFY (TEST_FUNC)
32 #endif
33
34 #include "json-lib.h"
35 #include "bench-timing.h"
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 enum
42 {
43 NFIXED_ITERS = 1048576,
44 NRAND_BUFS = 16384,
45 NRAND_ITERS = 2048,
46 RAND_BENCH_MAX_LEN = 128
47 };
48
49 #include "bench-hash-funcs-kernel.h"
50 #define SIMPLE
51 #include "bench-hash-funcs-kernel.h"
52
53 static void
do_one_test(json_ctx_t * json_ctx,size_t len)54 do_one_test (json_ctx_t *json_ctx, size_t len)
55 {
56 char buf[len + 1];
57 memset (buf, -1, len);
58 buf[len] = '\0';
59
60 json_element_object_begin (json_ctx);
61
62 json_attr_string (json_ctx, "type", "fixed");
63 json_attr_uint (json_ctx, "length", len);
64 json_attr_double (json_ctx, "time_simple", do_one_test_kernel_simple (buf, len));
65 json_attr_double (json_ctx, "time_optimized", do_one_test_kernel_optimized (buf, len));
66
67 json_element_object_end (json_ctx);
68 }
69
70 static void __attribute__ ((noinline, noclone))
do_rand_test(json_ctx_t * json_ctx)71 do_rand_test (json_ctx_t *json_ctx)
72 {
73 size_t i, sz, offset;
74 char *bufs;
75 unsigned int *sizes;
76
77 bufs = (char *) calloc (NRAND_BUFS, RAND_BENCH_MAX_LEN);
78 sizes = (unsigned int *) calloc (NRAND_BUFS, sizeof (unsigned int));
79 if (bufs == NULL || sizes == NULL)
80 {
81 fprintf (stderr, "Failed to allocate bufs for random test\n");
82 goto done;
83 }
84
85 for (sz = 2; sz <= RAND_BENCH_MAX_LEN; sz += sz)
86 {
87 json_element_object_begin (json_ctx);
88 json_attr_string (json_ctx, "type", "random");
89 json_attr_uint (json_ctx, "length", sz);
90
91 for (i = 0, offset = 0; i < NRAND_BUFS;
92 ++i, offset += RAND_BENCH_MAX_LEN)
93 {
94 sizes[i] = random () % sz;
95 memset (bufs + offset, -1, sizes[i]);
96 bufs[offset + sizes[i]] = '\0';
97 }
98
99 json_attr_double (json_ctx, "time_simple",
100 do_rand_test_kernel_simple (bufs, sizes));
101 json_attr_double (json_ctx, "time_optimized",
102 do_rand_test_kernel_optimized (bufs, sizes));
103 json_element_object_end (json_ctx);
104 }
105
106 done:
107 if (bufs)
108 free (bufs);
109
110 if (sizes)
111 free (sizes);
112 }
113
114 static int
do_test(void)115 do_test (void)
116 {
117 int i;
118 json_ctx_t json_ctx;
119
120 json_init (&json_ctx, 0, stdout);
121 json_document_begin (&json_ctx);
122 json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
123 json_attr_object_begin (&json_ctx, "functions");
124 json_attr_object_begin (&json_ctx, TEST_NAME);
125 json_array_begin (&json_ctx, "results");
126
127 for (i = 0; i < 16; ++i)
128 do_one_test (&json_ctx, i);
129
130 for (i = 16; i <= 256; i += i)
131 do_one_test (&json_ctx, i);
132
133 do_rand_test (&json_ctx);
134
135 json_array_end (&json_ctx);
136 json_attr_object_end (&json_ctx);
137 json_attr_object_end (&json_ctx);
138 json_document_end (&json_ctx);
139
140 return 0;
141 }
142
143 #include <support/test-driver.c>
144