Debian Codegraph @

benchmark/bench-range-select.c

raw symbols SCIP
1 /*
2 Copyright (C) 2014-2016 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: 09a4c4e00832fb90dee74c5b97b7cf0f5952f85b
21
22 CPU Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz (fam: 06, model: 1e, stepping: 05)
23
24 CFLAGS: -O0 -g3
25 % make --quiet -C benchmark run-bench-range-select
26 run-bench-range-select:
27 Process 10 times in each pattern
28 (total) (average) (median)
29 ( 500, 600] ( 1000): with mruby: (0.0058s) (0.5773ms) (0.5830ms)
30 ( 500, 600] ( 1000): without mruby: (0.0062s) (0.6203ms) (0.6200ms)
31 ( 5000, 5100] ( 10000): with mruby: (0.0058s) (0.5827ms) (0.5800ms)
32 ( 5000, 5100] ( 10000): without mruby: (0.0473s) (0.0047s) (0.0048s)
33 ( 50000, 50100] ( 100000): with mruby: (0.0064s) (0.6397ms) (0.6370ms)
34 ( 50000, 50100] ( 100000): without mruby: (0.4498s) (0.0450s) (0.0442s)
35 (500000, 500100] (1000000): with mruby: (0.0057s) (0.5710ms) (0.5190ms)
36 (500000, 500100] (1000000): without mruby: (4.3193s) (0.4319s) (0.4306s)
37
38 CFLAGS: -O2 -g
39 % make --quiet -C benchmark run-bench-range-select
40 run-bench-range-select:
41 Process 10 times in each pattern
42 (total) (average) (median)
43 ( 500, 600] ( 1000): with mruby: (0.0031s) (0.3058ms) (0.2890ms)
44 ( 500, 600] ( 1000): without mruby: (0.0031s) (0.3132ms) (0.3090ms)
45 ( 5000, 5100] ( 10000): with mruby: (0.0031s) (0.3063ms) (0.3100ms)
46 ( 5000, 5100] ( 10000): without mruby: (0.0239s) (0.0024s) (0.0023s)
47 ( 50000, 50100] ( 100000): with mruby: (0.0028s) (0.2825ms) (0.2660ms)
48 ( 50000, 50100] ( 100000): without mruby: (0.2117s) (0.0212s) (0.0211s)
49 (500000, 500100] (1000000): with mruby: (0.0028s) (0.2757ms) (0.2650ms)
50 (500000, 500100] (1000000): without mruby: (2.0874s) (0.2087s) (0.2092s)
51 */
52
53 #include <stdio.h>
54 #include <string.h>
55
56 #include <grn_db.h>
57 #include <groonga.h>
58
59 #include "lib/benchmark.h"
60
61 #define GET(context, name) (grn_ctx_get(context, name, strlen(name)))
62
63 typedef struct _BenchmarkData
64 {
65 grn_ctx context;
66 grn_obj *database;
67 guint n_records;
68 bool use_mruby;
69 const gchar *command;
70 } BenchmarkData;
71
72 static void
73 run_command(grn_ctx *context, const gchar *command)
74 {
75 gchar *response;
76 unsigned int response_length;
77 int flags;
78
79 grn_ctx_send(context, command, strlen(command), 0);
80 grn_ctx_recv(context, &response, &response_length, &flags);
81 }
82
83 static void
84 bench(gpointer user_data)
85 {
86 BenchmarkData *data = user_data;
87 grn_ctx *context = &(data->context);
88
89 run_command(context, data->command);
90 }
91
92 static gchar *
93 get_tmp_dir(void)
94 {
95 gchar *current_dir;
96 gchar *tmp_dir;
97
98 current_dir = g_get_current_dir();
99 tmp_dir = g_build_filename(current_dir, "tmp", NULL);
100 g_free(current_dir);
101
102 return tmp_dir;
103 }
104
105 static void
106 setup_database(BenchmarkData *data)
107 {
108 grn_ctx *context = &(data->context);
109 gchar *tmp_dir;
110 gchar *database_last_component_name;
111 gchar *database_path;
112 guint i;
113
114 tmp_dir = get_tmp_dir();
115 database_last_component_name =
116 g_strdup_printf("db-%d-%s-mruby",
117 data->n_records,
118 data->use_mruby ? "with" : "without");
119 database_path = g_build_filename(tmp_dir,
120 "range-select",
121 database_last_component_name,
122 NULL);
123 g_free(database_last_component_name);
124
125 if (g_file_test(database_path, G_FILE_TEST_EXISTS)) {
126 data->database = grn_db_open(context, database_path);
127 run_command(context, "dump");
128 } else {
129 data->database = grn_db_create(context, database_path, NULL);
130
131 run_command(context, "table_create Entries TABLE_NO_KEY");
132 run_command(context, "column_create Entries rank COLUMN_SCALAR Int32");
133 run_command(context, "table_create Ranks TABLE_PAT_KEY Int32");
134 run_command(context,
135 "column_create Ranks entries_rank COLUMN_INDEX Entries rank");
136
137 run_command(context, "load --table Entries");
138 run_command(context, "[");
139 for (i = 0; i < data->n_records; i++) {
140 #define BUFFER_SIZE 4096
141 gchar buffer[BUFFER_SIZE];
142 const gchar *separator;
143 if (i == (data->n_records - 1)) {
144 separator = "";
145 } else {
146 separator = ",";
147 }
148 snprintf(buffer, BUFFER_SIZE, "{\"rank\": %u}%s", i, separator);
149 run_command(context, buffer);
150 #undef BUFFER_SIZE
151 }
152 run_command(context, "]");
153 }
154
155 g_free(database_path);
156 }
157
158 static void
159 bench_startup(BenchmarkData *data)
160 {
161 if (data->use_mruby) {
162 g_setenv("GRN_MRUBY_ENABLED", "yes", TRUE);
163 } else {
164 g_setenv("GRN_MRUBY_ENABLED", "no", TRUE);
165 }
166 grn_ctx_init(&(data->context), 0);
167 setup_database(data);
168 }
169
170 static void
171 bench_shutdown(BenchmarkData *data)
172 {
173 grn_ctx *context = &(data->context);
174
175 grn_obj_close(context, data->database);
176 grn_ctx_fin(context);
177 }
178
179 int
180 main(int argc, gchar **argv)
181 {
182 grn_rc rc;
183 BenchReporter *reporter;
184 gint n = 10;
185
186 rc = grn_init();
187 if (rc != GRN_SUCCESS) {
188 g_print("failed to initialize Groonga: <%d>: %s\n",
189 rc, grn_get_global_error_message());
190 return EXIT_FAILURE;
191 }
192
193 g_print("Process %d times in each pattern\n", n);
194
195 bench_init(&argc, &argv);
196 reporter = bench_reporter_new();
197
198 {
199 BenchmarkData data_small_with_mruby;
200 BenchmarkData data_small_without_mruby;
201 BenchmarkData data_medium_with_mruby;
202 BenchmarkData data_medium_without_mruby;
203 BenchmarkData data_large_with_mruby;
204 BenchmarkData data_large_without_mruby;
205 BenchmarkData data_very_large_with_mruby;
206 BenchmarkData data_very_large_without_mruby;
207
208 #define REGISTER(data, n_records_, min, max, use_mruby_) \
209 do { \
210 gchar *label; \
211 label = g_strdup_printf("(%6d, %6d] (%7d): %7s mruby", \
212 min, max, n_records_, \
213 use_mruby_ ? "with" : "without"); \
214 data.use_mruby = use_mruby_; \
215 data.n_records = n_records_; \
216 data.command = \
217 "select Entries --cache no " \
218 "--filter 'rank > " #min " && rank <= " #max "'"; \
219 bench_startup(&data); \
220 bench_reporter_register(reporter, label, \
221 n, \
222 NULL, \
223 bench, \
224 NULL, \
225 &data); \
226 g_free(label); \
227 } while(FALSE)
228
229 REGISTER(data_small_with_mruby,
230 1000,
231 500, 600,
232 true);
233 REGISTER(data_small_without_mruby,
234 1000,
235 500, 600,
236 false);
237 REGISTER(data_medium_with_mruby,
238 10000,
239 5000, 5100,
240 true);
241 REGISTER(data_medium_without_mruby,
242 10000,
243 5000, 5100,
244 false);
245 REGISTER(data_large_with_mruby,
246 100000,
247 50000, 50100,
248 true);
249 REGISTER(data_large_without_mruby,
250 100000,
251 50000, 50100,
252 false);
253 REGISTER(data_very_large_with_mruby,
254 1000000,
255 500000, 500100,
256 true);
257 REGISTER(data_very_large_without_mruby,
258 1000000,
259 500000, 500100,
260 false);
261
262 #undef REGISTER
263
264 bench_reporter_run(reporter);
265
266 bench_shutdown(&data_small_with_mruby);
267 bench_shutdown(&data_small_without_mruby);
268 bench_shutdown(&data_medium_with_mruby);
269 bench_shutdown(&data_medium_without_mruby);
270 bench_shutdown(&data_large_with_mruby);
271 bench_shutdown(&data_large_without_mruby);
272 bench_shutdown(&data_very_large_with_mruby);
273 bench_shutdown(&data_very_large_without_mruby);
274 }
275 g_object_unref(reporter);
276
277 grn_fin();
278
279 return EXIT_SUCCESS;
280 }