1 | /* Measure hash functions runtime. |
2 | Copyright (C) 2022-2024 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 = 256, |
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 |
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 (ctx: json_ctx); |
61 | |
62 | json_attr_string (ctx: json_ctx, name: "type" , s: "fixed" ); |
63 | json_attr_uint (ctx: json_ctx, name: "length" , d: len); |
64 | json_attr_double (ctx: json_ctx, name: "time_simple" , d: do_one_test_kernel_simple (s: buf, len)); |
65 | json_attr_double (ctx: json_ctx, name: "time_optimized" , d: do_one_test_kernel_optimized (s: buf, len)); |
66 | |
67 | json_element_object_end (ctx: json_ctx); |
68 | } |
69 | |
70 | static void __attribute__ ((noinline, noclone)) |
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 (nmemb: NRAND_BUFS, size: RAND_BENCH_MAX_LEN); |
78 | sizes = (unsigned int *) calloc (nmemb: NRAND_BUFS, size: 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 (ctx: json_ctx); |
88 | json_attr_string (ctx: json_ctx, name: "type" , s: "random" ); |
89 | json_attr_uint (ctx: json_ctx, name: "length" , d: 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 (ctx: json_ctx, name: "time_simple" , |
100 | d: do_rand_test_kernel_simple (bufs, sizes)); |
101 | json_attr_double (ctx: json_ctx, name: "time_optimized" , |
102 | d: do_rand_test_kernel_optimized (bufs, sizes)); |
103 | json_element_object_end (ctx: json_ctx); |
104 | } |
105 | |
106 | done: |
107 | if (bufs) |
108 | free (ptr: bufs); |
109 | |
110 | if (sizes) |
111 | free (ptr: sizes); |
112 | } |
113 | |
114 | static int |
115 | do_test (void) |
116 | { |
117 | int i; |
118 | json_ctx_t json_ctx; |
119 | |
120 | json_init (ctx: &json_ctx, indent_level: 0, stdout); |
121 | json_document_begin (ctx: &json_ctx); |
122 | json_attr_string (ctx: &json_ctx, name: "timing_type" , TIMING_TYPE); |
123 | json_attr_object_begin (ctx: &json_ctx, name: "functions" ); |
124 | json_attr_object_begin (ctx: &json_ctx, TEST_NAME); |
125 | json_array_begin (ctx: &json_ctx, name: "results" ); |
126 | |
127 | for (i = 0; i < 16; ++i) |
128 | do_one_test (json_ctx: &json_ctx, len: i); |
129 | |
130 | for (i = 16; i <= 256; i += i) |
131 | do_one_test (json_ctx: &json_ctx, len: i); |
132 | |
133 | do_rand_test (json_ctx: &json_ctx); |
134 | |
135 | json_array_end (ctx: &json_ctx); |
136 | json_attr_object_end (ctx: &json_ctx); |
137 | json_attr_object_end (ctx: &json_ctx); |
138 | json_document_end (ctx: &json_ctx); |
139 | |
140 | return 0; |
141 | } |
142 | |
143 | #include <support/test-driver.c> |
144 | |