| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 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 |
} |