Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
bench-geo-select.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright (C) 2011 Kouhei Sutou <kou@clear-code.com>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License version 2.1 as published by the Free Software Foundation.
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 <string.h>
60 
61 #include <db.h>
62 #include <groonga.h>
63 
64 #include "lib/benchmark.h"
65 
66 #define GET(context, name) (grn_ctx_get(context, name, strlen(name)))
67 
68 typedef struct _BenchmarkData
69 {
70  gboolean report_result;
71 
77 
81 
82 static void
83 set_geo_point(grn_ctx *context, grn_obj *geo_point, const gchar *geo_point_text)
84 {
85  grn_obj point_text;
86 
87  GRN_TEXT_INIT(&point_text, 0);
88  GRN_TEXT_PUTS(context, &point_text, geo_point_text);
89  grn_obj_cast(context, &point_text, geo_point, GRN_FALSE);
90  grn_obj_unlink(context, &point_text);
91 }
92 
93 static void
94 bench_setup_common(gpointer user_data)
95 {
96  BenchmarkData *data = user_data;
97  const gchar *tokyo_station = "35.68136,139.76609";
98  const gchar *ikebukuro_station = "35.72890,139.71036";
99 
100  data->result = grn_table_create(data->context, NULL, 0, NULL,
102  data->table, NULL);
103 
104  set_geo_point(data->context, &(data->top_left_point),
105  ikebukuro_station);
106  set_geo_point(data->context, &(data->bottom_right_point),
107  tokyo_station);
108 }
109 
110 static void
111 bench_setup_query_partial(gpointer user_data)
112 {
113  BenchmarkData *data = user_data;
114  const gchar *tokyo_station = "35.68136,139.76609";
115  const gchar *ikebukuro_station = "35.72890,139.71036";
116 
117  set_geo_point(data->context, &(data->top_left_point),
118  ikebukuro_station);
119  set_geo_point(data->context, &(data->bottom_right_point),
120  tokyo_station);
121 }
122 
123 static void
124 bench_setup_query_all(gpointer user_data)
125 {
126  BenchmarkData *data = user_data;
127  const gchar *tokyo_station = "35.0,140.0";
128  const gchar *ikebukuro_station = "36.0,139.0";
129 
130  set_geo_point(data->context, &(data->top_left_point),
131  ikebukuro_station);
132  set_geo_point(data->context, &(data->bottom_right_point),
133  tokyo_station);
134 }
135 
136 static void
137 bench_setup_in_rectangle_partial(gpointer user_data)
138 {
139  bench_setup_common(user_data);
140  bench_setup_query_partial(user_data);
141 }
142 
143 static void
144 bench_setup_in_rectangle_all(gpointer user_data)
145 {
146  bench_setup_common(user_data);
147  bench_setup_query_all(user_data);
148 }
149 
150 static void
151 bench_geo_select_in_rectangle(gpointer user_data)
152 {
153  BenchmarkData *data = user_data;
154 
156  data->index_column,
157  &(data->top_left_point),
158  &(data->bottom_right_point),
159  data->result,
160  GRN_OP_OR);
161 }
162 
163 static void
164 bench_teardown(gpointer user_data)
165 {
166  BenchmarkData *data = user_data;
167 
168  if (data->report_result) {
169  g_print("result: %d\n", grn_table_size(data->context, data->result));
170  }
171 
172  grn_obj_unlink(data->context, data->result);
173 }
174 
175 static gchar *
176 get_tmp_dir(void)
177 {
178  gchar *current_dir;
179  gchar *tmp_dir;
180 
181  current_dir = g_get_current_dir();
182  tmp_dir = g_build_filename(current_dir, "tmp", NULL);
183  g_free(current_dir);
184 
185  return tmp_dir;
186 }
187 
188 static void
189 setup_database(BenchmarkData *data)
190 {
191  gchar *tmp_dir;
192  gchar *database_path;
193 
194  tmp_dir = get_tmp_dir();
195  database_path = g_build_filename(tmp_dir, "geo-select", "db", NULL);
196  data->database = grn_db_open(data->context, database_path);
197 
198  data->table = GET(data->context, "Addresses");
199  data->index_column = GET(data->context, "Locations.address");
200 
201  g_free(database_path);
202 }
203 
204 static void
205 teardown_database(BenchmarkData *data)
206 {
207  grn_obj_unlink(data->context, data->index_column);
208  grn_obj_unlink(data->context, data->table);
209  grn_obj_unlink(data->context, data->database);
210 }
211 
212 int
213 main(int argc, gchar **argv)
214 {
215  BenchmarkData data;
216  BenchReporter *reporter;
217  gint n = 100;
218 
219  grn_init();
220  bench_init(&argc, &argv);
221 
222  data.report_result = g_getenv("GROONGA_BENCH_REPORT_RESULT") != NULL;
223 
224  data.context = g_new(grn_ctx, 1);
225  grn_ctx_init(data.context, 0);
226 
227  setup_database(&data);
230 
231  {
232  const gchar *groonga_bench_n;
233  groonga_bench_n = g_getenv("GROONGA_BENCH_N");
234  if (groonga_bench_n) {
235  n = atoi(groonga_bench_n);
236  }
237  }
238 
239  reporter = bench_reporter_new();
240 
241 #define REGISTER(label, type, area) \
242  bench_reporter_register(reporter, \
243  label, \
244  n, \
245  bench_setup_ ## type ## _ ## area, \
246  bench_geo_select_ ## type, \
247  bench_teardown, \
248  &data)
249  REGISTER("1st: select_in_rectangle (partial)", in_rectangle, partial);
250  REGISTER("2nd: select_in_rectangle (partial)", in_rectangle, partial);
251  REGISTER("1st: select_in_rectangle (all)", in_rectangle, all);
252  REGISTER("2nd: select_in_rectangle (all)", in_rectangle, all);
253 #undef REGISTER
254 
255  bench_reporter_run(reporter);
256  g_object_unref(reporter);
257 
258  grn_obj_unlink(data.context, &(data.top_left_point));
260  teardown_database(&data);
261 
262  grn_ctx_fin(data.context);
263  g_free(data.context);
264 
265  bench_quit();
266  grn_fin();
267 
268  return 0;
269 }