NaCl: Fix thinko in last change.
[glibc.git] / benchtests / bench-strcoll.c
blobc3d9a08e1b12bc90d62b47bfb63a84a4d6d79703
1 /* Measure strcoll execution time in different locales.
2 Copyright (C) 2015 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 <http://www.gnu.org/licenses/>. */
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 "json-lib.h"
26 #include "bench-timing.h"
28 /* Many thanks to http://generator.lorem-ipsum.info/ */
29 #define INPUT_PREFIX "strcoll-inputs/"
31 static const char *const input_files[] = {
32 "filelist#C",
33 "filelist#en_US.UTF-8",
34 "lorem_ipsum#vi_VN.UTF-8",
35 "lorem_ipsum#ar_SA.UTF-8",
36 "lorem_ipsum#en_US.UTF-8",
37 "lorem_ipsum#zh_CN.UTF-8",
38 "lorem_ipsum#cs_CZ.UTF-8",
39 "lorem_ipsum#en_GB.UTF-8",
40 "lorem_ipsum#da_DK.UTF-8",
41 "lorem_ipsum#pl_PL.UTF-8",
42 "lorem_ipsum#fr_FR.UTF-8",
43 "lorem_ipsum#pt_PT.UTF-8",
44 "lorem_ipsum#el_GR.UTF-8",
45 "lorem_ipsum#ru_RU.UTF-8",
46 "lorem_ipsum#iw_IL.UTF-8",
47 "lorem_ipsum#es_ES.UTF-8",
48 "lorem_ipsum#hi_IN.UTF-8",
49 "lorem_ipsum#sv_SE.UTF-8",
50 "lorem_ipsum#hu_HU.UTF-8",
51 "lorem_ipsum#tr_TR.UTF-8",
52 "lorem_ipsum#is_IS.UTF-8",
53 "lorem_ipsum#it_IT.UTF-8",
54 "lorem_ipsum#sr_RS.UTF-8",
55 "lorem_ipsum#ja_JP.UTF-8"
58 #define TEXTFILE_DELIMITER " \n\r\t.,?!"
60 static char *
61 read_file (const char *filename)
63 struct stat stats;
64 char *buffer = NULL;
65 int fd = open (filename, O_CLOEXEC);
67 if (fd >= 0)
69 if (fstat (fd, &stats) == 0)
71 buffer = malloc (stats.st_size + 1);
72 if (buffer)
74 if (read (fd, buffer, stats.st_size) == stats.st_size)
75 buffer[stats.st_size] = '\0';
76 else
78 free (buffer);
79 buffer = NULL;
83 close (fd);
86 return buffer;
89 static size_t
90 count_words (const char *text, const char *delim)
92 size_t wordcount = 0;
93 char *tmp = strdup (text);
95 char *token = strtok (tmp, delim);
96 while (token != NULL)
98 if (*token != '\0')
99 wordcount++;
100 token = strtok (NULL, delim);
103 free (tmp);
104 return wordcount;
107 typedef struct
109 size_t size;
110 char **words;
111 } word_list;
113 static word_list *
114 new_word_list (size_t size)
116 word_list *list = malloc (sizeof (word_list));
117 assert (list != NULL);
118 list->size = size;
119 list->words = malloc (size * sizeof (char *));
120 assert (list->words != NULL);
121 return list;
124 static word_list *
125 str_word_list (const char *str, const char *delim)
127 size_t n = 0;
128 word_list *list = new_word_list (count_words (str, delim));
130 char *toks = strdup (str);
131 char *word = strtok (toks, delim);
132 while (word != NULL && n < list->size)
134 if (*word != '\0')
135 list->words[n++] = strdup (word);
136 word = strtok (NULL, delim);
139 free (toks);
140 return list;
143 static word_list *
144 copy_word_list (const word_list *list)
146 size_t i;
147 word_list *copy = new_word_list (list->size);
149 for (i = 0; i < list->size; i++)
150 copy->words[i] = strdup (list->words[i]);
152 return copy;
155 static void
156 free_word_list (word_list *list)
158 size_t i;
159 for (i = 0; i < list->size; i++)
160 free (list->words[i]);
162 free (list->words);
163 free (list);
166 static int
167 compare_words (const void *a, const void *b)
169 const char *s1 = *(char **) a;
170 const char *s2 = *(char **) b;
171 return strcoll (s1, s2);
174 #undef INNER_LOOP_ITERS
175 #define INNER_LOOP_ITERS 16
177 static void
178 bench_list (json_ctx_t *json_ctx, word_list *list)
180 size_t i;
181 timing_t start, stop, cur;
183 word_list **tests = malloc (INNER_LOOP_ITERS * sizeof (word_list *));
184 assert (tests != NULL);
185 for (i = 0; i < INNER_LOOP_ITERS; i++)
186 tests[i] = copy_word_list (list);
188 TIMING_NOW (start);
189 for (i = 0; i < INNER_LOOP_ITERS; i++)
190 qsort (tests[i]->words, tests[i]->size, sizeof (char *), compare_words);
191 TIMING_NOW (stop);
193 TIMING_DIFF (cur, start, stop);
194 setlocale (LC_ALL, "en_US.UTF-8");
195 json_attr_double (json_ctx, "duration", cur);
196 json_attr_double (json_ctx, "iterations", i);
197 json_attr_double (json_ctx, "mean", (double) cur / i);
199 for (i = 0; i < INNER_LOOP_ITERS; i++)
200 free_word_list (tests[i]);
201 free (tests);
204 typedef enum
207 ERROR_FILENAME,
208 ERROR_LOCALE,
209 ERROR_IO
210 } result_t;
212 static result_t
213 bench_file (json_ctx_t *json_ctx, const char *testname, const char *filename,
214 const char *locale)
216 if (setlocale (LC_ALL, locale) == NULL)
217 return ERROR_LOCALE;
219 char *text = read_file (filename);
220 if (text == NULL)
221 return ERROR_IO;
223 word_list *list = str_word_list (text, TEXTFILE_DELIMITER);
225 json_attr_object_begin (json_ctx, testname);
226 bench_list (json_ctx, list);
227 json_attr_object_end (json_ctx);
229 free_word_list (list);
230 free (text);
231 return OK;
235 main (void)
237 timing_t res;
238 TIMING_INIT (res);
240 json_ctx_t *json_ctx = malloc (sizeof (json_ctx_t));
241 assert (json_ctx != NULL);
242 json_init (json_ctx, 2, stdout);
243 json_attr_object_begin (json_ctx, "strcoll");
245 size_t i;
246 result_t result = OK;
247 for (i = 0; i < (sizeof (input_files) / sizeof (input_files[0])); i++)
249 char *locale = strchr (input_files[i], '#');
250 if (locale == NULL)
252 printf ("Failed to get locale from filename %s, aborting!\n",
253 input_files[i]);
254 return ERROR_FILENAME;
257 char *filename;
258 asprintf (&filename, INPUT_PREFIX "%s", input_files[i]);
259 result = bench_file (json_ctx, input_files[i], filename, locale + 1);
261 if (result != OK)
263 if (result == ERROR_LOCALE)
264 printf ("Failed to set locale %s, aborting!\n", locale);
265 else if (result == ERROR_IO)
266 printf ("Failed to read file %s, aborting!\n", filename);
267 free (filename);
268 goto out;
270 free (filename);
273 out:
274 json_attr_object_end (json_ctx);
275 free (json_ctx);
276 return result;