| 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 |
#include <stdlib.h> |
| 44 |
#include <string.h> |
| 45 |
|
| 46 |
#include <glib.h> |
| 47 |
|
| 48 |
#include <groonga.h> |
| 49 |
|
| 50 |
#include "lib/benchmark.h" |
| 51 |
|
| 52 |
typedef struct _BenchmarkData { |
| 53 |
grn_ctx context; |
| 54 |
grn_obj *database; |
| 55 |
bool use_mruby; |
| 56 |
GString *command; |
| 57 |
} BenchmarkData; |
| 58 |
|
| 59 |
static void |
| 60 |
bench(gpointer user_data) |
| 61 |
{ |
| 62 |
BenchmarkData *data = user_data; |
| 63 |
grn_ctx *context = &(data->context); |
| 64 |
char *response; |
| 65 |
unsigned int response_length; |
| 66 |
int flags; |
| 67 |
|
| 68 |
grn_ctx_send(context, data->command->str, data->command->len, 0); |
| 69 |
grn_ctx_recv(context, &response, &response_length, &flags); |
| 70 |
} |
| 71 |
|
| 72 |
static void |
| 73 |
bench_setup(gpointer user_data) |
| 74 |
{ |
| 75 |
BenchmarkData *data = user_data; |
| 76 |
|
| 77 |
if (data->use_mruby) { |
| 78 |
g_setenv("GRN_MRUBY_ENABLED", "yes", TRUE); |
| 79 |
} else { |
| 80 |
g_setenv("GRN_MRUBY_ENABLED", "no", TRUE); |
| 81 |
} |
| 82 |
grn_ctx_init(&(data->context), 0); |
| 83 |
grn_ctx_use(&(data->context), data->database); |
| 84 |
} |
| 85 |
|
| 86 |
static void |
| 87 |
bench_teardown(gpointer user_data) |
| 88 |
{ |
| 89 |
BenchmarkData *data = user_data; |
| 90 |
|
| 91 |
grn_ctx_fin(&(data->context)); |
| 92 |
} |
| 93 |
|
| 94 |
static gchar * |
| 95 |
get_tmp_dir(void) |
| 96 |
{ |
| 97 |
gchar *current_dir; |
| 98 |
gchar *tmp_dir; |
| 99 |
|
| 100 |
current_dir = g_get_current_dir(); |
| 101 |
tmp_dir = g_build_filename(current_dir, "tmp", NULL); |
| 102 |
g_free(current_dir); |
| 103 |
|
| 104 |
return tmp_dir; |
| 105 |
} |
| 106 |
|
| 107 |
static grn_obj * |
| 108 |
setup_database(grn_ctx *context) |
| 109 |
{ |
| 110 |
gchar *tmp_dir; |
| 111 |
gchar *database_path; |
| 112 |
grn_obj *database; |
| 113 |
const gchar *warmup_command = "dump"; |
| 114 |
gchar *response; |
| 115 |
unsigned int response_length; |
| 116 |
int flags; |
| 117 |
|
| 118 |
tmp_dir = get_tmp_dir(); |
| 119 |
database_path = g_build_filename(tmp_dir, "query-optimizer", "db", NULL); |
| 120 |
database = grn_db_open(context, database_path); |
| 121 |
grn_ctx_send(context, warmup_command, strlen(warmup_command), 0); |
| 122 |
grn_ctx_recv(context, &response, &response_length, &flags); |
| 123 |
|
| 124 |
g_free(database_path); |
| 125 |
|
| 126 |
return database; |
| 127 |
} |
| 128 |
|
| 129 |
static void |
| 130 |
teardown_database(grn_ctx *context, grn_obj *database) |
| 131 |
{ |
| 132 |
grn_obj_close(context, database); |
| 133 |
} |
| 134 |
|
| 135 |
int |
| 136 |
main(int argc, gchar **argv) |
| 137 |
{ |
| 138 |
grn_rc rc; |
| 139 |
grn_ctx context; |
| 140 |
grn_obj *database; |
| 141 |
BenchReporter *reporter; |
| 142 |
gint n = 100; |
| 143 |
|
| 144 |
rc = grn_init(); |
| 145 |
if (rc != GRN_SUCCESS) { |
| 146 |
g_print("failed to initialize Groonga: <%d>: %s\n", |
| 147 |
rc, grn_get_global_error_message()); |
| 148 |
return EXIT_FAILURE; |
| 149 |
} |
| 150 |
bench_init(&argc, &argv); |
| 151 |
|
| 152 |
grn_ctx_init(&context, 0); |
| 153 |
|
| 154 |
database = setup_database(&context); |
| 155 |
|
| 156 |
reporter = bench_reporter_new(); |
| 157 |
|
| 158 |
g_print("Process %d times in each pattern\n", n); |
| 159 |
{ |
| 160 |
BenchmarkData data_one_condition_with_mruby; |
| 161 |
BenchmarkData data_one_condition_without_mruby; |
| 162 |
BenchmarkData data_multiple_conditions_with_mruby; |
| 163 |
BenchmarkData data_multiple_conditions_without_mruby; |
| 164 |
|
| 165 |
#define REGISTER(label, data, use_mruby_, command_) \ |
| 166 |
data.database = database; \ |
| 167 |
data.use_mruby = use_mruby_; \ |
| 168 |
data.command = g_string_new(command_); \ |
| 169 |
bench_reporter_register(reporter, label, \ |
| 170 |
n, \ |
| 171 |
bench_setup, \ |
| 172 |
bench, \ |
| 173 |
bench_teardown, \ |
| 174 |
&data) |
| 175 |
|
| 176 |
REGISTER("1 condition: with mruby", data_one_condition_with_mruby, |
| 177 |
true, |
| 178 |
"select Entries --cache no --query 'name:@Groonga'"); |
| 179 |
REGISTER("1 condition: without mruby", data_one_condition_without_mruby, |
| 180 |
false, |
| 181 |
"select Entries --cache no --query 'name:@Groonga'"); |
| 182 |
REGISTER("4 conditions: with mruby", |
| 183 |
data_multiple_conditions_with_mruby, |
| 184 |
true, |
| 185 |
"select Entries --cache no --filter '" |
| 186 |
"name @ \"Groonga\" && " |
| 187 |
"description @ \"search\" && " |
| 188 |
"last_modified >= \"2014-2-9 00:00:00\" && " |
| 189 |
"last_modified <= \"2014-11-29 00:00:00\"" |
| 190 |
"'"); |
| 191 |
REGISTER("4 conditions: without mruby", |
| 192 |
data_multiple_conditions_without_mruby, |
| 193 |
false, |
| 194 |
"select Entries --cache no --filter '" |
| 195 |
"name @ \"Groonga\" && " |
| 196 |
"description @ \"search\" && " |
| 197 |
"last_modified >= \"2014-2-9 00:00:00\" && " |
| 198 |
"last_modified <= \"2014-11-29 00:00:00\"" |
| 199 |
"'"); |
| 200 |
|
| 201 |
#undef REGISTER |
| 202 |
|
| 203 |
bench_reporter_run(reporter); |
| 204 |
} |
| 205 |
g_object_unref(reporter); |
| 206 |
|
| 207 |
teardown_database(&context, database); |
| 208 |
|
| 209 |
grn_ctx_fin(&context); |
| 210 |
|
| 211 |
grn_fin(); |
| 212 |
|
| 213 |
return EXIT_SUCCESS; |
| 214 |
} |