diff options
author | Ran Benita <ran234@gmail.com> | 2012-08-28 19:07:07 +0300 |
---|---|---|
committer | Ran Benita <ran234@gmail.com> | 2012-09-03 10:31:13 +0300 |
commit | 1aa6e2b1e20542683ae224a7824e62e0fba623c7 (patch) | |
tree | 2d7d0e22d61c2d7545c64afd2a2074c9c29a1683 /test | |
parent | 480f919d770d2c847c4fc2dd94ab6cf85f50c09a (diff) |
test/rules-file: add benchmark
Signed-off-by: Ran Benita <ran234@gmail.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/rules-file.c | 55 |
1 files changed, 48 insertions, 7 deletions
diff --git a/test/rules-file.c b/test/rules-file.c index 67a000e..5ddd074 100644 --- a/test/rules-file.c +++ b/test/rules-file.c @@ -25,11 +25,14 @@ #include <stdbool.h> #include <stdio.h> #include <string.h> +#include <time.h> #include "test.h" #include "xkb-priv.h" #include "rules.h" +#define BENCHMARK_ITERATIONS 20000 + struct test_data { /* Rules file */ const char *rules; @@ -89,19 +92,57 @@ test_rules(struct xkb_context *ctx, struct test_data *data) return passed; } +static void +benchmark(struct xkb_context *ctx) +{ + struct timespec start, stop, elapsed; + enum xkb_log_level old_level = xkb_get_log_level(ctx); + int old_verb = xkb_get_log_verbosity(ctx); + int i; + struct xkb_rule_names rmlvo = { + "evdev", "pc105", "us,il", ",", "ctrl:nocaps,grp:menu_toggle", + }; + struct xkb_component_names kccgst; + + xkb_set_log_level(ctx, XKB_LOG_LEVEL_CRITICAL); + xkb_set_log_verbosity(ctx, 0); + + clock_gettime(CLOCK_MONOTONIC, &start); + for (i = 0; i < BENCHMARK_ITERATIONS; i++) { + assert(xkb_components_from_rules(ctx, &rmlvo, &kccgst)); + free(kccgst.keycodes); + free(kccgst.types); + free(kccgst.compat); + free(kccgst.symbols); + } + clock_gettime(CLOCK_MONOTONIC, &stop); + + xkb_set_log_level(ctx, old_level); + xkb_set_log_verbosity(ctx, old_verb); + + elapsed.tv_sec = stop.tv_sec - start.tv_sec; + elapsed.tv_nsec = stop.tv_nsec - start.tv_nsec; + if (elapsed.tv_nsec < 0) { + elapsed.tv_nsec += 1000000000; + elapsed.tv_sec--; + } + + fprintf(stderr, "processed %d times in %ld.%09lds\n", + BENCHMARK_ITERATIONS, elapsed.tv_sec, elapsed.tv_nsec); +} + int -main(void) +main(int argc, char *argv[]) { struct xkb_context *ctx; - const char *srcdir = getenv("srcdir"); - char *path; - ctx = xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES); + ctx = test_get_context(); assert(ctx); - assert(asprintf(&path, "%s/test/data", srcdir ? srcdir : ".") > 0); - assert(xkb_context_include_path_append(ctx, test_get_path(""))); - free(path); + if (argc > 1 && streq(argv[1], "bench")) { + benchmark(ctx); + return 0; + } struct test_data test1 = { .rules = "simple", |