Debian Codegraph @

benchmark/bench-cache.c

raw symbols SCIP
1 /*
2 Copyright (C) 2017 Kouhei Sutou <[email protected]>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 /*
20 Groonga: eb65125330b3a8f920693ef3ad53011c7412f2c9
21 CFLAGS: -O2 -g3
22 CPU: Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
23
24 % make --silent -C benchmark run-bench-cache
25 run-bench-cache:
26 (total) (average) (median)
27 1000: (0.0458s) (0.4576ms) (0.4170ms)
28 10000: (0.3464s) (0.0035s) (0.0034s)
29 % GRN_CACHE_TYPE=persistent make --silent -C benchmark run-bench-cache
30 run-bench-cache:
31 (total) (average) (median)
32 1000: (0.0480s) (0.4801ms) (0.4700ms)
33 10000: (0.4033s) (0.0040s) (0.0040s)
34 */
35
36 #include <string.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39
40 #include <grn_cache.h>
41
42 #include "lib/benchmark.h"
43
44 typedef struct _BenchmarkData
45 {
46 grn_ctx *context;
47 grn_cache *cache;
48 grn_obj value;
49 } BenchmarkData;
50
51 static void
52 bench_n(BenchmarkData *data, gint64 n)
53 {
54 gint64 i;
55 grn_ctx *ctx;
56 grn_cache *cache;
57 grn_obj *value;
58 grn_obj fetch_buffer;
59
60 ctx = data->context;
61 cache = data->cache;
62 value = &(data->value);
63 GRN_TEXT_INIT(&fetch_buffer, 0);
64 for (i = 0; i < n; i++) {
65 char key[GRN_TABLE_MAX_KEY_SIZE];
66 grn_snprintf(key,
67 GRN_TABLE_MAX_KEY_SIZE,
68 GRN_TABLE_MAX_KEY_SIZE,
69 "key:%" GRN_FMT_INT64D,
70 i);
71 GRN_BULK_REWIND(&fetch_buffer);
72 grn_cache_fetch(ctx, cache, key, strlen(key), &fetch_buffer);
73 grn_cache_update(ctx, cache, key, strlen(key), value);
74 }
75 GRN_OBJ_FIN(ctx, &fetch_buffer);
76 }
77
78 static void
79 bench_1000(gpointer user_data)
80 {
81 BenchmarkData *data = user_data;
82 bench_n(data, 1000);
83 }
84
85 static void
86 bench_10000(gpointer user_data)
87 {
88 BenchmarkData *data = user_data;
89 bench_n(data, 10000);
90 }
91
92 static void
93 bench_setup(gpointer user_data)
94 {
95 BenchmarkData *data = user_data;
96
97 data->cache = grn_cache_open(data->context);
98 GRN_TEXT_INIT(&(data->value), 0);
99 while (GRN_TEXT_LEN(&(data->value)) < 1024) {
100 GRN_TEXT_PUTS(data->context, &(data->value), "XXXXXXXXXXX");
101 }
102 }
103
104 static void
105 bench_teardown(gpointer user_data)
106 {
107 BenchmarkData *data = user_data;
108
109 grn_obj_close(data->context, &(data->value));
110 grn_cache_close(data->context, data->cache);
111 }
112
113 int
114 main(int argc, gchar **argv)
115 {
116 grn_rc rc;
117 BenchmarkData data;
118 BenchReporter *reporter;
119 gchar *base_dir;
120 grn_ctx ctx;
121 gint n = 100;
122
123 rc = grn_init();
124 if (rc != GRN_SUCCESS) {
125 g_print("failed to initialize Groonga: <%d>: %s\n",
126 rc, grn_get_global_error_message());
127 return EXIT_FAILURE;
128 }
129 bench_init(&argc, &argv);
130
131 grn_ctx_init(&ctx, 0);
132
133 data.context = &ctx;
134
135 base_dir = g_build_filename(g_get_tmp_dir(), "groonga-bench", NULL);
136 bench_utils_remove_path_recursive_force(base_dir);
137 g_mkdir_with_parents(base_dir, 0755);
138
139 reporter = bench_reporter_new();
140 bench_reporter_register(reporter, "1000", n,
141 bench_setup, bench_1000, bench_teardown, &data);
142 bench_reporter_register(reporter, "10000", n,
143 bench_setup, bench_10000, bench_teardown, &data);
144 bench_reporter_run(reporter);
145 g_object_unref(reporter);
146
147 grn_ctx_fin(&ctx);
148
149 bench_utils_remove_path_recursive_force(base_dir);
150
151 bench_quit();
152 grn_fin();
153
154 return EXIT_SUCCESS;
155 }