| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <string.h> |
| 20 |
#include <stdlib.h> |
| 21 |
|
| 22 |
#include <groonga.h> |
| 23 |
|
| 24 |
#include "lib/benchmark.h" |
| 25 |
|
| 26 |
#define DEFAULT_FLAGS (GRN_OBJ_PERSISTENT | GRN_OBJ_TABLE_PAT_KEY) |
| 27 |
#define DEFAULT_VALUE_SIZE (1024) |
| 28 |
#define VALUE_TYPE_NAME "<value_type>" |
| 29 |
|
| 30 |
typedef struct _BenchmarkData |
| 31 |
{ |
| 32 |
gchar *base_dir; |
| 33 |
|
| 34 |
grn_ctx *context; |
| 35 |
const char *name; |
| 36 |
unsigned name_size; |
| 37 |
char *path; |
| 38 |
grn_obj_flags flags; |
| 39 |
grn_obj *key_type; |
| 40 |
unsigned value_size; |
| 41 |
grn_encoding encoding; |
| 42 |
} BenchmarkData; |
| 43 |
|
| 44 |
static void |
| 45 |
bench_normal(gpointer user_data) |
| 46 |
{ |
| 47 |
BenchmarkData *data = user_data; |
| 48 |
grn_obj *table; |
| 49 |
grn_obj *value_type = grn_ctx_get(data->context, VALUE_TYPE_NAME, strlen(VALUE_TYPE_NAME)); |
| 50 |
if (!value_type) { |
| 51 |
value_type = grn_type_create(data->context, VALUE_TYPE_NAME, strlen(VALUE_TYPE_NAME), |
| 52 |
0, data->value_size); |
| 53 |
} |
| 54 |
table = grn_table_create(data->context, |
| 55 |
data->name, data->name_size, |
| 56 |
data->path, data->flags, |
| 57 |
data->key_type, value_type); |
| 58 |
grn_obj_close(data->context, table); |
| 59 |
} |
| 60 |
|
| 61 |
static void |
| 62 |
bench_normal_temporary(gpointer user_data) |
| 63 |
{ |
| 64 |
BenchmarkData *data = user_data; |
| 65 |
grn_obj *table; |
| 66 |
grn_obj *value_type = grn_ctx_get(data->context, VALUE_TYPE_NAME, strlen(VALUE_TYPE_NAME)); |
| 67 |
if (!value_type) { |
| 68 |
value_type = grn_type_create(data->context, VALUE_TYPE_NAME, strlen(VALUE_TYPE_NAME), |
| 69 |
0, data->value_size); |
| 70 |
} |
| 71 |
GRN_CTX_SET_ENCODING(data->context, data->encoding); |
| 72 |
table = grn_table_create(data->context, |
| 73 |
data->name, data->name_size, |
| 74 |
NULL, data->flags & ~GRN_OBJ_PERSISTENT, |
| 75 |
data->key_type, value_type); |
| 76 |
grn_obj_close(data->context, table); |
| 77 |
} |
| 78 |
|
| 79 |
typedef struct _grn_table_factory |
| 80 |
{ |
| 81 |
grn_ctx *context; |
| 82 |
char *name; |
| 83 |
unsigned name_size; |
| 84 |
char *path; |
| 85 |
grn_obj_flags flags; |
| 86 |
grn_obj *key_type; |
| 87 |
unsigned value_size; |
| 88 |
grn_encoding encoding; |
| 89 |
} grn_table_factory; |
| 90 |
|
| 91 |
static grn_table_factory * |
| 92 |
grn_table_factory_create(void) |
| 93 |
{ |
| 94 |
grn_table_factory *factory; |
| 95 |
|
| 96 |
factory = g_new0(grn_table_factory, 1); |
| 97 |
|
| 98 |
factory->context = NULL; |
| 99 |
factory->name = NULL; |
| 100 |
factory->name_size = 0; |
| 101 |
factory->path = NULL; |
| 102 |
factory->flags = DEFAULT_FLAGS; |
| 103 |
factory->key_type = NULL; |
| 104 |
factory->value_size = DEFAULT_VALUE_SIZE; |
| 105 |
factory->encoding = GRN_ENC_DEFAULT; |
| 106 |
|
| 107 |
return factory; |
| 108 |
} |
| 109 |
|
| 110 |
static void |
| 111 |
grn_table_factory_set_context(grn_table_factory *factory, grn_ctx *context) |
| 112 |
{ |
| 113 |
factory->context = context; |
| 114 |
} |
| 115 |
|
| 116 |
static void |
| 117 |
grn_table_factory_set_name(grn_table_factory *factory, const char *name) |
| 118 |
{ |
| 119 |
factory->name = g_strdup(name); |
| 120 |
factory->name_size = strlen(name); |
| 121 |
} |
| 122 |
|
| 123 |
static void |
| 124 |
grn_table_factory_set_path(grn_table_factory *factory, const char *path) |
| 125 |
{ |
| 126 |
factory->path = g_strdup(path); |
| 127 |
if (path) |
| 128 |
factory->flags |= GRN_OBJ_PERSISTENT; |
| 129 |
else |
| 130 |
factory->flags &= ~GRN_OBJ_PERSISTENT; |
| 131 |
} |
| 132 |
|
| 133 |
static void |
| 134 |
grn_table_factory_set_key_type(grn_table_factory *factory, grn_obj *key_type) |
| 135 |
{ |
| 136 |
factory->key_type = key_type; |
| 137 |
} |
| 138 |
|
| 139 |
static grn_obj * |
| 140 |
grn_table_factory_make(grn_table_factory *factory) |
| 141 |
{ |
| 142 |
grn_obj *value_type = grn_ctx_get(factory->context, VALUE_TYPE_NAME, strlen(VALUE_TYPE_NAME)); |
| 143 |
if (!value_type) { |
| 144 |
value_type = grn_type_create(factory->context, VALUE_TYPE_NAME, strlen(VALUE_TYPE_NAME), |
| 145 |
0, factory->value_size); |
| 146 |
} |
| 147 |
GRN_CTX_SET_ENCODING(factory->context, factory->encoding); |
| 148 |
return grn_table_create(factory->context, |
| 149 |
factory->name, factory->name_size, |
| 150 |
factory->path, factory->flags, |
| 151 |
factory->key_type, value_type); |
| 152 |
} |
| 153 |
|
| 154 |
static void |
| 155 |
grn_table_factory_close(grn_table_factory *factory) |
| 156 |
{ |
| 157 |
g_free(factory->name); |
| 158 |
g_free(factory->path); |
| 159 |
g_free(factory); |
| 160 |
} |
| 161 |
|
| 162 |
static void |
| 163 |
bench_factory(gpointer user_data) |
| 164 |
{ |
| 165 |
BenchmarkData *data = user_data; |
| 166 |
grn_table_factory *factory; |
| 167 |
grn_obj *table; |
| 168 |
|
| 169 |
factory = grn_table_factory_create(); |
| 170 |
grn_table_factory_set_context(factory, data->context); |
| 171 |
grn_table_factory_set_name(factory, data->name); |
| 172 |
grn_table_factory_set_path(factory, data->path); |
| 173 |
grn_table_factory_set_key_type(factory, data->key_type); |
| 174 |
|
| 175 |
table = grn_table_factory_make(factory); |
| 176 |
grn_obj_close(data->context, table); |
| 177 |
|
| 178 |
grn_table_factory_close(factory); |
| 179 |
} |
| 180 |
|
| 181 |
static void |
| 182 |
bench_factory_temporary(gpointer user_data) |
| 183 |
{ |
| 184 |
BenchmarkData *data = user_data; |
| 185 |
grn_table_factory *factory; |
| 186 |
grn_obj *table; |
| 187 |
|
| 188 |
factory = grn_table_factory_create(); |
| 189 |
grn_table_factory_set_context(factory, data->context); |
| 190 |
grn_table_factory_set_name(factory, data->name); |
| 191 |
grn_table_factory_set_key_type(factory, data->key_type); |
| 192 |
|
| 193 |
table = grn_table_factory_make(factory); |
| 194 |
grn_obj_close(data->context, table); |
| 195 |
|
| 196 |
grn_table_factory_close(factory); |
| 197 |
} |
| 198 |
|
| 199 |
static void |
| 200 |
bench_setup(gpointer user_data) |
| 201 |
{ |
| 202 |
BenchmarkData *data = user_data; |
| 203 |
const gchar *type_name; |
| 204 |
|
| 205 |
bench_utils_remove_path_recursive_force(data->base_dir); |
| 206 |
g_mkdir_with_parents(data->base_dir, 0755); |
| 207 |
|
| 208 |
grn_ctx_init(data->context, GRN_CTX_USE_QL); |
| 209 |
|
| 210 |
type_name = "name"; |
| 211 |
data->key_type = grn_type_create(data->context, |
| 212 |
type_name, strlen(type_name), |
| 213 |
GRN_OBJ_KEY_UINT, sizeof(grn_id)); |
| 214 |
|
| 215 |
} |
| 216 |
|
| 217 |
static void |
| 218 |
bench_teardown(gpointer user_data) |
| 219 |
{ |
| 220 |
BenchmarkData *data = user_data; |
| 221 |
|
| 222 |
grn_obj_close(data->context, data->key_type); |
| 223 |
grn_ctx_fin(data->context); |
| 224 |
bench_utils_remove_path_recursive_force(data->base_dir); |
| 225 |
} |
| 226 |
|
| 227 |
int |
| 228 |
main(int argc, gchar **argv) |
| 229 |
{ |
| 230 |
grn_rc rc; |
| 231 |
BenchmarkData data; |
| 232 |
BenchReporter *reporter; |
| 233 |
gint n = 100; |
| 234 |
|
| 235 |
rc = grn_init(); |
| 236 |
if (rc != GRN_SUCCESS) { |
| 237 |
g_print("failed to initialize Groonga: <%d>: %s\n", |
| 238 |
rc, grn_get_global_error_message()); |
| 239 |
return EXIT_FAILURE; |
| 240 |
} |
| 241 |
bench_init(&argc, &argv); |
| 242 |
|
| 243 |
data.context = g_new(grn_ctx, 1); |
| 244 |
data.base_dir = g_build_filename(g_get_tmp_dir(), "groonga-bench", NULL); |
| 245 |
data.name = "table"; |
| 246 |
data.name_size = strlen(data.name); |
| 247 |
data.path = g_build_filename(data.base_dir, "table", NULL); |
| 248 |
data.flags = DEFAULT_FLAGS; |
| 249 |
data.key_type = NULL; |
| 250 |
data.value_size = DEFAULT_VALUE_SIZE; |
| 251 |
data.encoding = GRN_ENC_DEFAULT; |
| 252 |
|
| 253 |
reporter = bench_reporter_new(); |
| 254 |
bench_reporter_register(reporter, "normal (persistent)", n, |
| 255 |
bench_setup, bench_normal, bench_teardown, &data); |
| 256 |
bench_reporter_register(reporter, "factory (persistent)", n, |
| 257 |
bench_setup, bench_factory, bench_teardown, &data); |
| 258 |
bench_reporter_register(reporter, "normal (temporary)", n, |
| 259 |
bench_setup, bench_normal_temporary, bench_teardown, |
| 260 |
&data); |
| 261 |
bench_reporter_register(reporter, "factory (temporary)", n, |
| 262 |
bench_setup, bench_factory_temporary, bench_teardown, |
| 263 |
&data); |
| 264 |
bench_reporter_run(reporter); |
| 265 |
g_object_unref(reporter); |
| 266 |
|
| 267 |
bench_utils_remove_path_recursive_force(data.base_dir); |
| 268 |
|
| 269 |
g_free(data.path); |
| 270 |
g_free(data.base_dir); |
| 271 |
g_free(data.context); |
| 272 |
|
| 273 |
bench_quit(); |
| 274 |
grn_fin(); |
| 275 |
|
| 276 |
return EXIT_SUCCESS; |
| 277 |
} |