xref: /aosp_15_r20/external/libxkbcommon/bench/key-proc.c (revision 2b949d0487e80d67f1fda82db69e101e761f8064)
1*2b949d04SAndroid Build Coastguard Worker /*
2*2b949d04SAndroid Build Coastguard Worker  * Copyright © 2012 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 <stdlib.h>
27*2b949d04SAndroid Build Coastguard Worker #include <time.h>
28*2b949d04SAndroid Build Coastguard Worker 
29*2b949d04SAndroid Build Coastguard Worker #include "../test/test.h"
30*2b949d04SAndroid Build Coastguard Worker #include "bench.h"
31*2b949d04SAndroid Build Coastguard Worker 
32*2b949d04SAndroid Build Coastguard Worker #define BENCHMARK_ITERATIONS 20000000
33*2b949d04SAndroid Build Coastguard Worker 
34*2b949d04SAndroid Build Coastguard Worker static void
bench_key_proc(struct xkb_state * state)35*2b949d04SAndroid Build Coastguard Worker bench_key_proc(struct xkb_state *state)
36*2b949d04SAndroid Build Coastguard Worker {
37*2b949d04SAndroid Build Coastguard Worker     int8_t keys[256] = { 0 };
38*2b949d04SAndroid Build Coastguard Worker     xkb_keycode_t keycode;
39*2b949d04SAndroid Build Coastguard Worker     xkb_keysym_t keysym;
40*2b949d04SAndroid Build Coastguard Worker     int i;
41*2b949d04SAndroid Build Coastguard Worker 
42*2b949d04SAndroid Build Coastguard Worker     for (i = 0; i < BENCHMARK_ITERATIONS; i++) {
43*2b949d04SAndroid Build Coastguard Worker         keycode = (rand() % (255 - 9)) + 9;
44*2b949d04SAndroid Build Coastguard Worker         if (keys[keycode]) {
45*2b949d04SAndroid Build Coastguard Worker             xkb_state_update_key(state, keycode, XKB_KEY_UP);
46*2b949d04SAndroid Build Coastguard Worker             keys[keycode] = 0;
47*2b949d04SAndroid Build Coastguard Worker             keysym = xkb_state_key_get_one_sym(state, keycode);
48*2b949d04SAndroid Build Coastguard Worker             (void) keysym;
49*2b949d04SAndroid Build Coastguard Worker         } else {
50*2b949d04SAndroid Build Coastguard Worker             xkb_state_update_key(state, keycode, XKB_KEY_DOWN);
51*2b949d04SAndroid Build Coastguard Worker             keys[keycode] = 1;
52*2b949d04SAndroid Build Coastguard Worker         }
53*2b949d04SAndroid Build Coastguard Worker     }
54*2b949d04SAndroid Build Coastguard Worker }
55*2b949d04SAndroid Build Coastguard Worker 
56*2b949d04SAndroid Build Coastguard Worker int
main(void)57*2b949d04SAndroid Build Coastguard Worker main(void)
58*2b949d04SAndroid Build Coastguard Worker {
59*2b949d04SAndroid Build Coastguard Worker     struct xkb_context *ctx;
60*2b949d04SAndroid Build Coastguard Worker     struct xkb_keymap *keymap;
61*2b949d04SAndroid Build Coastguard Worker     struct xkb_state *state;
62*2b949d04SAndroid Build Coastguard Worker     struct bench bench;
63*2b949d04SAndroid Build Coastguard Worker     char *elapsed;
64*2b949d04SAndroid Build Coastguard Worker 
65*2b949d04SAndroid Build Coastguard Worker     ctx = test_get_context(0);
66*2b949d04SAndroid Build Coastguard Worker     assert(ctx);
67*2b949d04SAndroid Build Coastguard Worker 
68*2b949d04SAndroid Build Coastguard Worker     keymap = test_compile_rules(ctx, "evdev", "pc104", "us,ru,il,de",
69*2b949d04SAndroid Build Coastguard Worker                                 ",,,neo", "grp:menu_toggle");
70*2b949d04SAndroid Build Coastguard Worker     assert(keymap);
71*2b949d04SAndroid Build Coastguard Worker 
72*2b949d04SAndroid Build Coastguard Worker     state = xkb_state_new(keymap);
73*2b949d04SAndroid Build Coastguard Worker     assert(state);
74*2b949d04SAndroid Build Coastguard Worker 
75*2b949d04SAndroid Build Coastguard Worker     xkb_context_set_log_level(ctx, XKB_LOG_LEVEL_CRITICAL);
76*2b949d04SAndroid Build Coastguard Worker     xkb_context_set_log_verbosity(ctx, 0);
77*2b949d04SAndroid Build Coastguard Worker 
78*2b949d04SAndroid Build Coastguard Worker     srand((unsigned) time(NULL));
79*2b949d04SAndroid Build Coastguard Worker 
80*2b949d04SAndroid Build Coastguard Worker     bench_start(&bench);
81*2b949d04SAndroid Build Coastguard Worker     bench_key_proc(state);
82*2b949d04SAndroid Build Coastguard Worker     bench_stop(&bench);
83*2b949d04SAndroid Build Coastguard Worker 
84*2b949d04SAndroid Build Coastguard Worker     elapsed = bench_elapsed_str(&bench);
85*2b949d04SAndroid Build Coastguard Worker     fprintf(stderr, "ran %d iterations in %ss\n",
86*2b949d04SAndroid Build Coastguard Worker             BENCHMARK_ITERATIONS, elapsed);
87*2b949d04SAndroid Build Coastguard Worker     free(elapsed);
88*2b949d04SAndroid Build Coastguard Worker 
89*2b949d04SAndroid Build Coastguard Worker     xkb_state_unref(state);
90*2b949d04SAndroid Build Coastguard Worker     xkb_keymap_unref(keymap);
91*2b949d04SAndroid Build Coastguard Worker     xkb_context_unref(ctx);
92*2b949d04SAndroid Build Coastguard Worker 
93*2b949d04SAndroid Build Coastguard Worker     return 0;
94*2b949d04SAndroid Build Coastguard Worker }
95