| 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 |
#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 |
} |