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