xref: /aosp_15_r20/external/libxkbcommon/bench/atom.c (revision 2b949d0487e80d67f1fda82db69e101e761f8064)
1*2b949d04SAndroid Build Coastguard Worker /*
2*2b949d04SAndroid Build Coastguard Worker  * Copyright © 2021 Ran Benita <[email protected]>
3*2b949d04SAndroid Build Coastguard Worker  *
4*2b949d04SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a
5*2b949d04SAndroid Build Coastguard Worker  * copy of this software and associated documentation files (the "Software"),
6*2b949d04SAndroid Build Coastguard Worker  * to deal in the Software without restriction, including without limitation
7*2b949d04SAndroid Build Coastguard Worker  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*2b949d04SAndroid Build Coastguard Worker  * and/or sell copies of the Software, and to permit persons to whom the
9*2b949d04SAndroid Build Coastguard Worker  * Software is furnished to do so, subject to the following conditions:
10*2b949d04SAndroid Build Coastguard Worker  *
11*2b949d04SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice (including the next
12*2b949d04SAndroid Build Coastguard Worker  * paragraph) shall be included in all copies or substantial portions of the
13*2b949d04SAndroid Build Coastguard Worker  * Software.
14*2b949d04SAndroid Build Coastguard Worker  *
15*2b949d04SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*2b949d04SAndroid Build Coastguard Worker  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*2b949d04SAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*2b949d04SAndroid Build Coastguard Worker  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*2b949d04SAndroid Build Coastguard Worker  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*2b949d04SAndroid Build Coastguard Worker  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21*2b949d04SAndroid Build Coastguard Worker  * DEALINGS IN THE SOFTWARE.
22*2b949d04SAndroid Build Coastguard Worker  */
23*2b949d04SAndroid Build Coastguard Worker 
24*2b949d04SAndroid Build Coastguard Worker #include "config.h"
25*2b949d04SAndroid Build Coastguard Worker 
26*2b949d04SAndroid Build Coastguard Worker #include <stdbool.h>
27*2b949d04SAndroid Build Coastguard Worker #include <stdint.h>
28*2b949d04SAndroid Build Coastguard Worker #include <stdio.h>
29*2b949d04SAndroid Build Coastguard Worker #include <string.h>
30*2b949d04SAndroid Build Coastguard Worker #include <time.h>
31*2b949d04SAndroid Build Coastguard Worker 
32*2b949d04SAndroid Build Coastguard Worker #include "atom.h"
33*2b949d04SAndroid Build Coastguard Worker #include "bench.h"
34*2b949d04SAndroid Build Coastguard Worker #include "darray.h"
35*2b949d04SAndroid Build Coastguard Worker 
36*2b949d04SAndroid Build Coastguard Worker #define BENCHMARK_ITERATIONS 100
37*2b949d04SAndroid Build Coastguard Worker 
38*2b949d04SAndroid Build Coastguard Worker int
main(void)39*2b949d04SAndroid Build Coastguard Worker main(void)
40*2b949d04SAndroid Build Coastguard Worker {
41*2b949d04SAndroid Build Coastguard Worker     FILE *file;
42*2b949d04SAndroid Build Coastguard Worker     char wordbuf[1024];
43*2b949d04SAndroid Build Coastguard Worker     darray(char *) words;
44*2b949d04SAndroid Build Coastguard Worker     char **worditer;
45*2b949d04SAndroid Build Coastguard Worker     struct atom_table *table;
46*2b949d04SAndroid Build Coastguard Worker     xkb_atom_t atom;
47*2b949d04SAndroid Build Coastguard Worker     const char *text;
48*2b949d04SAndroid Build Coastguard Worker     struct bench bench;
49*2b949d04SAndroid Build Coastguard Worker     char *elapsed;
50*2b949d04SAndroid Build Coastguard Worker 
51*2b949d04SAndroid Build Coastguard Worker     darray_init(words);
52*2b949d04SAndroid Build Coastguard Worker     file = fopen("/usr/share/dict/words", "rb");
53*2b949d04SAndroid Build Coastguard Worker     if (file == NULL) {
54*2b949d04SAndroid Build Coastguard Worker         perror("/usr/share/dict/words");
55*2b949d04SAndroid Build Coastguard Worker         return -1;
56*2b949d04SAndroid Build Coastguard Worker     }
57*2b949d04SAndroid Build Coastguard Worker     while (fgets(wordbuf, sizeof(wordbuf), file)) {
58*2b949d04SAndroid Build Coastguard Worker         size_t len = strlen(wordbuf);
59*2b949d04SAndroid Build Coastguard Worker         if (len > 0 && wordbuf[len - 1] == '\n')
60*2b949d04SAndroid Build Coastguard Worker             wordbuf[len - 1] = '\0';
61*2b949d04SAndroid Build Coastguard Worker         darray_append(words, strdup(wordbuf));
62*2b949d04SAndroid Build Coastguard Worker     }
63*2b949d04SAndroid Build Coastguard Worker     fclose(file);
64*2b949d04SAndroid Build Coastguard Worker 
65*2b949d04SAndroid Build Coastguard Worker     bench_start(&bench);
66*2b949d04SAndroid Build Coastguard Worker     for (int i = 0; i < BENCHMARK_ITERATIONS; i++) {
67*2b949d04SAndroid Build Coastguard Worker         table = atom_table_new();
68*2b949d04SAndroid Build Coastguard Worker         assert(table);
69*2b949d04SAndroid Build Coastguard Worker 
70*2b949d04SAndroid Build Coastguard Worker         darray_foreach(worditer, words) {
71*2b949d04SAndroid Build Coastguard Worker             atom = atom_intern(table, *worditer, strlen(*worditer) - 1, true);
72*2b949d04SAndroid Build Coastguard Worker             assert(atom != XKB_ATOM_NONE);
73*2b949d04SAndroid Build Coastguard Worker 
74*2b949d04SAndroid Build Coastguard Worker             text = atom_text(table, atom);
75*2b949d04SAndroid Build Coastguard Worker             assert(text != NULL);
76*2b949d04SAndroid Build Coastguard Worker         }
77*2b949d04SAndroid Build Coastguard Worker 
78*2b949d04SAndroid Build Coastguard Worker         atom_table_free(table);
79*2b949d04SAndroid Build Coastguard Worker     }
80*2b949d04SAndroid Build Coastguard Worker     bench_stop(&bench);
81*2b949d04SAndroid Build Coastguard Worker 
82*2b949d04SAndroid Build Coastguard Worker     elapsed = bench_elapsed_str(&bench);
83*2b949d04SAndroid Build Coastguard Worker     fprintf(stderr, "%d iterations in %ss\n",
84*2b949d04SAndroid Build Coastguard Worker             BENCHMARK_ITERATIONS, elapsed);
85*2b949d04SAndroid Build Coastguard Worker     free(elapsed);
86*2b949d04SAndroid Build Coastguard Worker 
87*2b949d04SAndroid Build Coastguard Worker     darray_foreach(worditer, words) {
88*2b949d04SAndroid Build Coastguard Worker         free(*worditer);
89*2b949d04SAndroid Build Coastguard Worker     }
90*2b949d04SAndroid Build Coastguard Worker     darray_free(words);
91*2b949d04SAndroid Build Coastguard Worker 
92*2b949d04SAndroid Build Coastguard Worker     return 0;
93*2b949d04SAndroid Build Coastguard Worker }
94