Debian Codegraph @

benchmark/bench-geo-select.c

raw symbols SCIP
1 /*
2 Copyright (C) 2011-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: f56f466a05d756336f26ea5c2e54e9bdf5d3d681
21 CFLAGS: -O0 -ggdb3
22 CPU: Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz stepping 05
23 % (cd test/benchmark/ && make --quiet run-bench-geo-select)
24 run-bench-geo-select:
25 (time)
26 1st: select_in_rectangle (partial): (0.819828)
27 2nd: select_in_rectangle (partial): (0.832293)
28 1st: select_in_rectangle (all): (8.82504)
29 2nd: select_in_rectangle (all): (8.97628)
30
31 % (cd test/benchmark; GRN_GEO_CURSOR_STRICTLY=yes make --quiet run-bench-geo-select)
32 run-bench-geo-select:
33 (time)
34 1st: select_in_rectangle (partial): (0.528143)
35 2nd: select_in_rectangle (partial): (0.518647)
36 1st: select_in_rectangle (all): (8.77378)
37 2nd: select_in_rectangle (all): (8.76765)
38
39 groonga: f56f466a05d756336f26ea5c2e54e9bdf5d3d681
40 CFLAGS: -O3 -ggdb3
41 CPU: Intel(R) Core(TM) i7 CPU 860 @ 2.80GHz stepping 05
42 % (cd test/benchmark/ && make --quiet run-bench-geo-select)
43 run-bench-geo-select:
44 (time)
45 1st: select_in_rectangle (partial): (0.415439)
46 2nd: select_in_rectangle (partial): (0.423479)
47 1st: select_in_rectangle (all): (4.63983)
48 2nd: select_in_rectangle (all): (4.53055)
49
50 % (cd test/benchmark; GRN_GEO_CURSOR_STRICTLY=yes make --quiet run-bench-geo-select)
51 run-bench-geo-select:
52 (time)
53 1st: select_in_rectangle (partial): (0.26974)
54 2nd: select_in_rectangle (partial): (0.250247)
55 1st: select_in_rectangle (all): (4.45263)
56 2nd: select_in_rectangle (all): (4.61558)
57 */
58
59 #include <stdlib.h>
60 #include <string.h>
61
62 #include <grn_db.h>
63 #include <groonga.h>
64
65 #include "lib/benchmark.h"
66
67 #define GET(context, name) (grn_ctx_get(context, name, strlen(name)))
68
69 typedef struct _BenchmarkData
70 {
71 gboolean report_result;
72
73 grn_ctx *context;
74 grn_obj *database;
75 grn_obj *table;
76 grn_obj *index_column;
77 grn_obj *result;
78
79 grn_obj top_left_point;
80 grn_obj bottom_right_point;
81 } BenchmarkData;
82
83 static void
84 set_geo_point(grn_ctx *context, grn_obj *geo_point, const gchar *geo_point_text)
85 {
86 grn_obj point_text;
87
88 GRN_TEXT_INIT(&point_text, 0);
89 GRN_TEXT_PUTS(context, &point_text, geo_point_text);
90 grn_obj_cast(context, &point_text, geo_point, false);
91 grn_obj_unlink(context, &point_text);
92 }
93
94 static void
95 bench_setup_common(gpointer user_data)
96 {
97 BenchmarkData *data = user_data;
98 const gchar *tokyo_station = "35.68136,139.76609";
99 const gchar *ikebukuro_station = "35.72890,139.71036";
100
101 data->result = grn_table_create(data->context, NULL, 0, NULL,
102 GRN_OBJ_TABLE_HASH_KEY | GRN_OBJ_WITH_SUBREC,
103 data->table, NULL);
104
105 set_geo_point(data->context, &(data->top_left_point),
106 ikebukuro_station);
107 set_geo_point(data->context, &(data->bottom_right_point),
108 tokyo_station);
109 }
110
111 static void
112 bench_setup_query_partial(gpointer user_data)
113 {
114 BenchmarkData *data = user_data;
115 const gchar *tokyo_station = "35.68136,139.76609";
116 const gchar *ikebukuro_station = "35.72890,139.71036";
117
118 set_geo_point(data->context, &(data->top_left_point),
119 ikebukuro_station);
120 set_geo_point(data->context, &(data->bottom_right_point),
121 tokyo_station);
122 }
123
124 static void
125 bench_setup_query_all(gpointer user_data)
126 {
127 BenchmarkData *data = user_data;
128 const gchar *tokyo_station = "35.0,140.0";
129 const gchar *ikebukuro_station = "36.0,139.0";
130
131 set_geo_point(data->context, &(data->top_left_point),
132 ikebukuro_station);
133 set_geo_point(data->context, &(data->bottom_right_point),
134 tokyo_station);
135 }
136
137 static void
138 bench_setup_in_rectangle_partial(gpointer user_data)
139 {
140 bench_setup_common(user_data);
141 bench_setup_query_partial(user_data);
142 }
143
144 static void
145 bench_setup_in_rectangle_all(gpointer user_data)
146 {
147 bench_setup_common(user_data);
148 bench_setup_query_all(user_data);
149 }
150
151 static void
152 bench_geo_select_in_rectangle(gpointer user_data)
153 {
154 BenchmarkData *data = user_data;
155
156 grn_geo_select_in_rectangle(data->context,
157 data->index_column,
158 &(data->top_left_point),
159 &(data->bottom_right_point),
160 data->result,
161 GRN_OP_OR);
162 }
163
164 static void
165 bench_teardown(gpointer user_data)
166 {
167 BenchmarkData *data = user_data;
168
169 if (data->report_result) {
170 g_print("result: %d\n", grn_table_size(data->context, data->result));
171 }
172
173 grn_obj_unlink(data->context, data->result);
174 }
175
176 static gchar *
177 get_tmp_dir(void)
178 {
179 gchar *current_dir;
180 gchar *tmp_dir;
181
182 current_dir = g_get_current_dir();
183 tmp_dir = g_build_filename(current_dir, "tmp", NULL);
184 g_free(current_dir);
185
186 return tmp_dir;
187 }
188
189 static void
190 setup_database(BenchmarkData *data)
191 {
192 gchar *tmp_dir;
193 gchar *database_path;
194
195 tmp_dir = get_tmp_dir();
196 database_path = g_build_filename(tmp_dir, "geo-select", "db", NULL);
197 data->database = grn_db_open(data->context, database_path);
198
199 data->table = GET(data->context, "Addresses");
200 data->index_column = GET(data->context, "Locations.address");
201
202 g_free(database_path);
203 }
204
205 static void
206 teardown_database(BenchmarkData *data)
207 {
208 grn_obj_unlink(data->context, data->index_column);
209 grn_obj_unlink(data->context, data->table);
210 grn_obj_unlink(data->context, data->database);
211 }
212
213 int
214 main(int argc, gchar **argv)
215 {
216 grn_rc rc;
217 BenchmarkData data;
218 BenchReporter *reporter;
219 gint n = 100;
220
221 rc = grn_init();
222 if (rc != GRN_SUCCESS) {
223 g_print("failed to initialize Groonga: <%d>: %s\n",
224 rc, grn_get_global_error_message());
225 return EXIT_FAILURE;
226 }
227 bench_init(&argc, &argv);
228
229 data.report_result = g_getenv("GROONGA_BENCH_REPORT_RESULT") != NULL;
230
231 data.context = g_new(grn_ctx, 1);
232 grn_ctx_init(data.context, 0);
233
234 setup_database(&data);
235 GRN_WGS84_GEO_POINT_INIT(&(data.top_left_point), 0);
236 GRN_WGS84_GEO_POINT_INIT(&(data.bottom_right_point), 0);
237
238 {
239 const gchar *groonga_bench_n;
240 groonga_bench_n = g_getenv("GROONGA_BENCH_N");
241 if (groonga_bench_n) {
242 n = atoi(groonga_bench_n);
243 }
244 }
245
246 reporter = bench_reporter_new();
247
248 #define REGISTER(label, type, area) \
249 bench_reporter_register(reporter, \
250 label, \
251 n, \
252 bench_setup_ ## type ## _ ## area, \
253 bench_geo_select_ ## type, \
254 bench_teardown, \
255 &data)
256 REGISTER("1st: select_in_rectangle (partial)", in_rectangle, partial);
257 REGISTER("2nd: select_in_rectangle (partial)", in_rectangle, partial);
258 REGISTER("1st: select_in_rectangle (all)", in_rectangle, all);
259 REGISTER("2nd: select_in_rectangle (all)", in_rectangle, all);
260 #undef REGISTER
261
262 bench_reporter_run(reporter);
263 g_object_unref(reporter);
264
265 grn_obj_unlink(data.context, &(data.top_left_point));
266 grn_obj_unlink(data.context, &(data.bottom_right_point));
267 teardown_database(&data);
268
269 grn_ctx_fin(data.context);
270 g_free(data.context);
271
272 bench_quit();
273 grn_fin();
274
275 return EXIT_SUCCESS;
276 }