Debian Codegraph @

benchmark/bench-between-sequential.c

raw symbols SCIP
1 /*
2 Copyright (C) 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 Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
21
22 CFLAGS: -O2 -g
23
24 Groonga: e2971d9a555a90724b76964cc8c8805373500b4a
25 % make --quiet -C benchmark run-bench-between-sequential
26 run-bench-between-sequential:
27 Process 10 times in each pattern
28 (total) (average) (median)
29 ( 500, 600] ( 1000): between: (0.0528s) (0.0053s) (0.0043s)
30 ( 500, 600] ( 1000): range: (0.0120s) (0.0012s) (0.2500ms)
31 ( 5000, 5100] ( 10000): between: (0.4052s) (0.0405s) (0.0395s)
32 ( 5000, 5100] ( 10000): range: (0.0197s) (0.0020s) (0.0010s)
33 ( 50000, 50100] ( 100000): between: (3.9343s) (0.3934s) (0.3900s)
34 ( 50000, 50100] ( 100000): range: (0.0969s) (0.0097s) (0.0088s)
35 (500000, 500100] (1000000): between: (38.2969s) (3.8297s) (3.7983s)
36 (500000, 500100] (1000000): range: (0.9158s) (0.0916s) (0.0900s)
37
38 Groonga: 35e4e431bb7660b3170e98c329f7219bd6723f05
39 % make --quiet -C benchmark run-bench-between-sequential
40 run-bench-between-sequential:
41 Process 10 times in each pattern
42 (total) (average) (median)
43 ( 500, 600] ( 1000): between: (0.0130s) (0.0013s) (0.2590ms)
44 ( 500, 600] ( 1000): range: (0.0124s) (0.0012s) (0.2530ms)
45 ( 5000, 5100] ( 10000): between: (0.0163s) (0.0016s) (0.6440ms)
46 ( 5000, 5100] ( 10000): range: (0.0205s) (0.0021s) (0.0011s)
47 ( 50000, 50100] ( 100000): between: (0.0611s) (0.0061s) (0.0051s)
48 ( 50000, 50100] ( 100000): range: (0.1004s) (0.0100s) (0.0091s)
49 (500000, 500100] (1000000): between: (0.4518s) (0.0452s) (0.0442s)
50 (500000, 500100] (1000000): range: (0.8866s) (0.0887s) (0.0878s)
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 const gchar *command;
69 } BenchmarkData;
70
71 static void
72 run_command(grn_ctx *context, const gchar *command)
73 {
74 gchar *response;
75 unsigned int response_length;
76 int flags;
77
78 grn_ctx_send(context, command, strlen(command), 0);
79 grn_ctx_recv(context, &response, &response_length, &flags);
80 }
81
82 static void
83 bench(gpointer user_data)
84 {
85 BenchmarkData *data = user_data;
86 grn_ctx *context = &(data->context);
87
88 run_command(context, data->command);
89 }
90
91 static gchar *
92 get_tmp_dir(void)
93 {
94 gchar *current_dir;
95 gchar *tmp_dir;
96
97 current_dir = g_get_current_dir();
98 tmp_dir = g_build_filename(current_dir, "tmp", NULL);
99 g_free(current_dir);
100
101 return tmp_dir;
102 }
103
104 static void
105 setup_database(BenchmarkData *data)
106 {
107 grn_ctx *context = &(data->context);
108 gchar *tmp_dir;
109 gchar *database_last_component_name;
110 gchar *database_path;
111 guint i;
112
113 tmp_dir = get_tmp_dir();
114 database_last_component_name = g_strdup_printf("db-%d", data->n_records);
115 database_path = g_build_filename(tmp_dir,
116 "between-sequential",
117 database_last_component_name,
118 NULL);
119 g_free(database_last_component_name);
120
121 if (g_file_test(database_path, G_FILE_TEST_EXISTS)) {
122 data->database = grn_db_open(context, database_path);
123 run_command(context, "dump");
124 } else {
125 data->database = grn_db_create(context, database_path, NULL);
126
127 run_command(context, "table_create Entries TABLE_NO_KEY");
128 run_command(context, "column_create Entries rank COLUMN_SCALAR Int32");
129
130 run_command(context, "load --table Entries");
131 run_command(context, "[");
132 for (i = 0; i < data->n_records; i++) {
133 #define BUFFER_SIZE 4096
134 gchar buffer[BUFFER_SIZE];
135 const gchar *separator;
136 if (i == (data->n_records - 1)) {
137 separator = "";
138 } else {
139 separator = ",";
140 }
141 snprintf(buffer, BUFFER_SIZE, "{\"rank\": %u}%s", i, separator);
142 run_command(context, buffer);
143 #undef BUFFER_SIZE
144 }
145 run_command(context, "]");
146 }
147
148 g_free(database_path);
149 }
150
151 static void
152 bench_startup(BenchmarkData *data)
153 {
154 grn_ctx_init(&(data->context), 0);
155 setup_database(data);
156 }
157
158 static void
159 bench_shutdown(BenchmarkData *data)
160 {
161 grn_ctx *context = &(data->context);
162
163 grn_obj_close(context, data->database);
164 grn_ctx_fin(context);
165 }
166
167 int
168 main(int argc, gchar **argv)
169 {
170 grn_rc rc;
171 BenchReporter *reporter;
172 gint n = 10;
173
174 rc = grn_init();
175 if (rc != GRN_SUCCESS) {
176 g_print("failed to initialize Groonga: <%d>: %s\n",
177 rc, grn_get_global_error_message());
178 return EXIT_FAILURE;
179 }
180
181 g_print("Process %d times in each pattern\n", n);
182
183 bench_init(&argc, &argv);
184 reporter = bench_reporter_new();
185
186 {
187 BenchmarkData data_small_between;
188 BenchmarkData data_small_range;
189 BenchmarkData data_medium_between;
190 BenchmarkData data_medium_range;
191 BenchmarkData data_large_between;
192 BenchmarkData data_large_range;
193 BenchmarkData data_very_large_between;
194 BenchmarkData data_very_large_range;
195
196 #define REGISTER(data, n_records_, min, max, is_between) \
197 do { \
198 gchar *label; \
199 label = \
200 g_strdup_printf("(%6d, %6d] (%7d): %7s", \
201 min, max, n_records_, \
202 is_between ? "between" : "range"); \
203 data.n_records = n_records_; \
204 if (is_between) { \
205 data.command = \
206 "select Entries --cache no " \
207 "--filter " \
208 "'between(rank, " #min ", \"exclude\"," \
209 " " #max ", \"include\")'"; \
210 } else { \
211 data.command = \
212 "select Entries --cache no " \
213 "--filter 'rank > " #min " && rank <= " #max "'"; \
214 } \
215 bench_startup(&data); \
216 bench_reporter_register(reporter, label, \
217 n, \
218 NULL, \
219 bench, \
220 NULL, \
221 &data); \
222 g_free(label); \
223 } while(FALSE)
224
225 REGISTER(data_small_between,
226 1000,
227 500, 600,
228 TRUE);
229 REGISTER(data_small_range,
230 1000,
231 500, 600,
232 FALSE);
233 REGISTER(data_medium_between,
234 10000,
235 5000, 5100,
236 TRUE);
237 REGISTER(data_medium_range,
238 10000,
239 5000, 5100,
240 FALSE);
241 REGISTER(data_large_between,
242 100000,
243 50000, 50100,
244 TRUE);
245 REGISTER(data_large_range,
246 100000,
247 50000, 50100,
248 FALSE);
249 REGISTER(data_very_large_between,
250 1000000,
251 500000, 500100,
252 TRUE);
253 REGISTER(data_very_large_range,
254 1000000,
255 500000, 500100,
256 FALSE);
257
258 #undef REGISTER
259
260 bench_reporter_run(reporter);
261
262 bench_shutdown(&data_small_between);
263 bench_shutdown(&data_small_range);
264 bench_shutdown(&data_medium_between);
265 bench_shutdown(&data_medium_range);
266 bench_shutdown(&data_large_between);
267 bench_shutdown(&data_large_range);
268 bench_shutdown(&data_very_large_between);
269 bench_shutdown(&data_very_large_range);
270 }
271 g_object_unref(reporter);
272
273 grn_fin();
274
275 return EXIT_SUCCESS;
276 }