1 /* Measure strcoll execution time in different locales.
2 Copyright (C) 2015-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 #include <stdio.h>
20 #include <fcntl.h>
21 #include <assert.h>
22 #include <stdlib.h>
23 #include <locale.h>
24 #include <unistd.h>
25 #include <sys/stat.h>
26 #include "json-lib.h"
27 #include "bench-timing.h"
28 #include <string.h>
29
30 /* Many thanks to http://generator.lorem-ipsum.info/ */
31 #define INPUT_PREFIX "strcoll-inputs/"
32
33 static const char *const input_files[] = {
34 "filelist#C",
35 "filelist#en_US.UTF-8",
36 "lorem_ipsum#vi_VN.UTF-8",
37 "lorem_ipsum#ar_SA.UTF-8",
38 "lorem_ipsum#en_US.UTF-8",
39 "lorem_ipsum#zh_CN.UTF-8",
40 "lorem_ipsum#cs_CZ.UTF-8",
41 "lorem_ipsum#en_GB.UTF-8",
42 "lorem_ipsum#da_DK.UTF-8",
43 "lorem_ipsum#pl_PL.UTF-8",
44 "lorem_ipsum#fr_FR.UTF-8",
45 "lorem_ipsum#pt_PT.UTF-8",
46 "lorem_ipsum#el_GR.UTF-8",
47 "lorem_ipsum#ru_RU.UTF-8",
48 "lorem_ipsum#he_IL.UTF-8",
49 "lorem_ipsum#es_ES.UTF-8",
50 "lorem_ipsum#hi_IN.UTF-8",
51 "lorem_ipsum#sv_SE.UTF-8",
52 "lorem_ipsum#hu_HU.UTF-8",
53 "lorem_ipsum#tr_TR.UTF-8",
54 "lorem_ipsum#is_IS.UTF-8",
55 "lorem_ipsum#it_IT.UTF-8",
56 "lorem_ipsum#sr_RS.UTF-8",
57 "lorem_ipsum#ja_JP.UTF-8"
58 };
59
60 #define TEXTFILE_DELIMITER " \n\r\t.,?!"
61
62 static char *
read_file(const char * filename)63 read_file (const char *filename)
64 {
65 struct stat stats;
66 char *buffer = NULL;
67 int fd = open (filename, O_CLOEXEC);
68
69 if (fd >= 0)
70 {
71 if (fstat (fd, &stats) == 0)
72 {
73 buffer = malloc (stats.st_size + 1);
74 if (buffer)
75 {
76 if (read (fd, buffer, stats.st_size) == stats.st_size)
77 buffer[stats.st_size] = '\0';
78 else
79 {
80 free (buffer);
81 buffer = NULL;
82 }
83 }
84 }
85 close (fd);
86 }
87
88 return buffer;
89 }
90
91 static size_t
count_words(const char * text,const char * delim)92 count_words (const char *text, const char *delim)
93 {
94 size_t wordcount = 0;
95 char *tmp = strdup (text);
96
97 char *token = strtok (tmp, delim);
98 while (token != NULL)
99 {
100 if (*token != '\0')
101 wordcount++;
102 token = strtok (NULL, delim);
103 }
104
105 free (tmp);
106 return wordcount;
107 }
108
109 typedef struct
110 {
111 size_t size;
112 char **words;
113 } word_list;
114
115 static word_list *
new_word_list(size_t size)116 new_word_list (size_t size)
117 {
118 word_list *list = malloc (sizeof (word_list));
119 assert (list != NULL);
120 list->size = size;
121 list->words = malloc (size * sizeof (char *));
122 assert (list->words != NULL);
123 return list;
124 }
125
126 static word_list *
str_word_list(const char * str,const char * delim)127 str_word_list (const char *str, const char *delim)
128 {
129 size_t n = 0;
130 word_list *list = new_word_list (count_words (str, delim));
131
132 char *toks = strdup (str);
133 char *word = strtok (toks, delim);
134 while (word != NULL && n < list->size)
135 {
136 if (*word != '\0')
137 list->words[n++] = strdup (word);
138 word = strtok (NULL, delim);
139 }
140
141 free (toks);
142 return list;
143 }
144
145 static word_list *
copy_word_list(const word_list * list)146 copy_word_list (const word_list *list)
147 {
148 size_t i;
149 word_list *copy = new_word_list (list->size);
150
151 for (i = 0; i < list->size; i++)
152 copy->words[i] = strdup (list->words[i]);
153
154 return copy;
155 }
156
157 static void
free_word_list(word_list * list)158 free_word_list (word_list *list)
159 {
160 size_t i;
161 for (i = 0; i < list->size; i++)
162 free (list->words[i]);
163
164 free (list->words);
165 free (list);
166 }
167
168 static int
compare_words(const void * a,const void * b)169 compare_words (const void *a, const void *b)
170 {
171 const char *s1 = *(char **) a;
172 const char *s2 = *(char **) b;
173 return strcoll (s1, s2);
174 }
175
176 #undef INNER_LOOP_ITERS
177 #define INNER_LOOP_ITERS 16
178
179 static void
bench_list(json_ctx_t * json_ctx,word_list * list)180 bench_list (json_ctx_t *json_ctx, word_list *list)
181 {
182 size_t i;
183 timing_t start, stop, cur;
184
185 word_list **tests = malloc (INNER_LOOP_ITERS * sizeof (word_list *));
186 assert (tests != NULL);
187 for (i = 0; i < INNER_LOOP_ITERS; i++)
188 tests[i] = copy_word_list (list);
189
190 TIMING_NOW (start);
191 for (i = 0; i < INNER_LOOP_ITERS; i++)
192 qsort (tests[i]->words, tests[i]->size, sizeof (char *), compare_words);
193 TIMING_NOW (stop);
194
195 TIMING_DIFF (cur, start, stop);
196 setlocale (LC_ALL, "en_US.UTF-8");
197 json_attr_double (json_ctx, "duration", cur);
198 json_attr_double (json_ctx, "iterations", i);
199 json_attr_double (json_ctx, "mean", (double) cur / i);
200
201 for (i = 0; i < INNER_LOOP_ITERS; i++)
202 free_word_list (tests[i]);
203 free (tests);
204 }
205
206 typedef enum
207 {
208 OK,
209 ERROR_FILENAME,
210 ERROR_LOCALE,
211 ERROR_IO
212 } result_t;
213
214 static result_t
bench_file(json_ctx_t * json_ctx,const char * testname,const char * filename,const char * locale)215 bench_file (json_ctx_t *json_ctx, const char *testname, const char *filename,
216 const char *locale)
217 {
218 if (setlocale (LC_ALL, locale) == NULL)
219 return ERROR_LOCALE;
220
221 char *text = read_file (filename);
222 if (text == NULL)
223 return ERROR_IO;
224
225 word_list *list = str_word_list (text, TEXTFILE_DELIMITER);
226
227 json_attr_object_begin (json_ctx, testname);
228 bench_list (json_ctx, list);
229 json_attr_object_end (json_ctx);
230
231 free_word_list (list);
232 free (text);
233 return OK;
234 }
235
236 int
main(void)237 main (void)
238 {
239 json_ctx_t *json_ctx = malloc (sizeof (json_ctx_t));
240 assert (json_ctx != NULL);
241 json_init (json_ctx, 2, stdout);
242 json_attr_object_begin (json_ctx, "strcoll");
243
244 size_t i;
245 result_t result = OK;
246 for (i = 0; i < (sizeof (input_files) / sizeof (input_files[0])); i++)
247 {
248 char *locale = strchr (input_files[i], '#');
249 if (locale == NULL)
250 {
251 printf ("Failed to get locale from filename %s, aborting!\n",
252 input_files[i]);
253 return ERROR_FILENAME;
254 }
255
256 char *filename;
257 asprintf (&filename, INPUT_PREFIX "%s", input_files[i]);
258 result = bench_file (json_ctx, input_files[i], filename, locale + 1);
259
260 if (result != OK)
261 {
262 if (result == ERROR_LOCALE)
263 printf ("Failed to set locale %s, aborting!\n", locale);
264 else if (result == ERROR_IO)
265 printf ("Failed to read file %s, aborting!\n", filename);
266 free (filename);
267 goto out;
268 }
269 free (filename);
270 }
271
272 out:
273 json_attr_object_end (json_ctx);
274 free (json_ctx);
275 return result;
276 }
277