1 /* Measure hash functions runtime.
2 Copyright (C) 2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
21 # error "No TEST_FUNC provided!"
23 #ifndef SIMPLE_TEST_FUNC
24 # error "No SIMPLE_TEST_FUNC provided!"
28 # define STRINGIFY_PRIMITIVE(x) # x
29 # define STRINGIFY(x) STRINGIFY_PRIMITIVE (x)
31 # define TEST_NAME STRINGIFY (TEST_FUNC)
35 #include "bench-timing.h"
43 NFIXED_ITERS
= 1048576,
46 RAND_BENCH_MAX_LEN
= 128
49 #include "bench-hash-funcs-kernel.h"
51 #include "bench-hash-funcs-kernel.h"
54 do_one_test (json_ctx_t
*json_ctx
, size_t len
)
57 memset (buf
, -1, len
);
60 json_element_object_begin (json_ctx
);
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
));
67 json_element_object_end (json_ctx
);
70 static void __attribute__ ((noinline
, noclone
))
71 do_rand_test (json_ctx_t
*json_ctx
)
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
)
81 fprintf (stderr
, "Failed to allocate bufs for random test\n");
85 for (sz
= 2; sz
<= RAND_BENCH_MAX_LEN
; sz
+= sz
)
87 json_element_object_begin (json_ctx
);
88 json_attr_string (json_ctx
, "type", "random");
89 json_attr_uint (json_ctx
, "length", sz
);
91 for (i
= 0, offset
= 0; i
< NRAND_BUFS
;
92 ++i
, offset
+= RAND_BENCH_MAX_LEN
)
94 sizes
[i
] = random () % sz
;
95 memset (bufs
+ offset
, -1, sizes
[i
]);
96 bufs
[offset
+ sizes
[i
]] = '\0';
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
);
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");
127 for (i
= 0; i
< 16; ++i
)
128 do_one_test (&json_ctx
, i
);
130 for (i
= 16; i
<= 256; i
+= i
)
131 do_one_test (&json_ctx
, i
);
133 do_rand_test (&json_ctx
);
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
);
143 #include <support/test-driver.c>