| 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 |
#include <stdlib.h> |
| 42 |
|
| 43 |
#include <glib.h> |
| 44 |
|
| 45 |
#include <groonga.h> |
| 46 |
|
| 47 |
#include "lib/benchmark.h" |
| 48 |
|
| 49 |
typedef struct _BenchmarkData { |
| 50 |
grn_ctx context; |
| 51 |
grn_obj *database; |
| 52 |
guint memory_usage_before; |
| 53 |
} BenchmarkData; |
| 54 |
|
| 55 |
static guint |
| 56 |
get_memory_usage(void) |
| 57 |
{ |
| 58 |
GRegex *vm_rss_pattern; |
| 59 |
gchar *status; |
| 60 |
GMatchInfo *match_info; |
| 61 |
gchar *vm_rss_string; |
| 62 |
guint vm_rss; |
| 63 |
|
| 64 |
g_file_get_contents("/proc/self/status", &status, NULL, NULL); |
| 65 |
|
| 66 |
vm_rss_pattern = g_regex_new("VmRSS:\\s*(\\d*)\\s+kB", 0, 0, NULL); |
| 67 |
if (!g_regex_match(vm_rss_pattern, status, 0, &match_info)) { |
| 68 |
g_print("not match...: %s\n", status); |
| 69 |
return 0; |
| 70 |
} |
| 71 |
vm_rss_string = g_match_info_fetch(match_info, 1); |
| 72 |
vm_rss = atoi(vm_rss_string); |
| 73 |
g_free(vm_rss_string); |
| 74 |
g_match_info_free(match_info); |
| 75 |
g_regex_unref(vm_rss_pattern); |
| 76 |
g_free(status); |
| 77 |
|
| 78 |
return vm_rss; |
| 79 |
} |
| 80 |
|
| 81 |
static void |
| 82 |
bench_with_mruby(gpointer user_data) |
| 83 |
{ |
| 84 |
BenchmarkData *data = user_data; |
| 85 |
|
| 86 |
g_setenv("GRN_MRUBY_ENABLED", "yes", TRUE); |
| 87 |
grn_ctx_init(&(data->context), 0); |
| 88 |
grn_ctx_use(&(data->context), data->database); |
| 89 |
} |
| 90 |
|
| 91 |
static void |
| 92 |
bench_without_mruby(gpointer user_data) |
| 93 |
{ |
| 94 |
BenchmarkData *data = user_data; |
| 95 |
|
| 96 |
g_setenv("GRN_MRUBY_ENABLED", "no", TRUE); |
| 97 |
grn_ctx_init(&(data->context), 0); |
| 98 |
grn_ctx_use(&(data->context), data->database); |
| 99 |
} |
| 100 |
|
| 101 |
static void |
| 102 |
bench_setup(gpointer user_data) |
| 103 |
{ |
| 104 |
BenchmarkData *data = user_data; |
| 105 |
|
| 106 |
data->memory_usage_before = get_memory_usage(); |
| 107 |
} |
| 108 |
|
| 109 |
static void |
| 110 |
bench_teardown(gpointer user_data) |
| 111 |
{ |
| 112 |
BenchmarkData *data = user_data; |
| 113 |
|
| 114 |
grn_ctx_fin(&(data->context)); |
| 115 |
g_print("%3dKB ", get_memory_usage() - data->memory_usage_before); |
| 116 |
} |
| 117 |
|
| 118 |
static gchar * |
| 119 |
get_tmp_dir(void) |
| 120 |
{ |
| 121 |
gchar *current_dir; |
| 122 |
gchar *tmp_dir; |
| 123 |
|
| 124 |
current_dir = g_get_current_dir(); |
| 125 |
tmp_dir = g_build_filename(current_dir, "tmp", NULL); |
| 126 |
g_free(current_dir); |
| 127 |
|
| 128 |
return tmp_dir; |
| 129 |
} |
| 130 |
|
| 131 |
static grn_obj * |
| 132 |
setup_database(grn_ctx *context) |
| 133 |
{ |
| 134 |
gchar *tmp_dir; |
| 135 |
gchar *database_path; |
| 136 |
grn_obj *database; |
| 137 |
|
| 138 |
tmp_dir = get_tmp_dir(); |
| 139 |
database_path = g_build_filename(tmp_dir, "ctx-create", "db", NULL); |
| 140 |
database = grn_db_open(context, database_path); |
| 141 |
|
| 142 |
g_free(database_path); |
| 143 |
|
| 144 |
return database; |
| 145 |
} |
| 146 |
|
| 147 |
static void |
| 148 |
teardown_database(grn_ctx *context, grn_obj *database) |
| 149 |
{ |
| 150 |
grn_obj_close(context, database); |
| 151 |
} |
| 152 |
|
| 153 |
int |
| 154 |
main(int argc, gchar **argv) |
| 155 |
{ |
| 156 |
grn_rc rc; |
| 157 |
grn_ctx context; |
| 158 |
BenchmarkData data; |
| 159 |
BenchReporter *reporter; |
| 160 |
gint n = 1; |
| 161 |
|
| 162 |
rc = grn_init(); |
| 163 |
if (rc != GRN_SUCCESS) { |
| 164 |
g_print("failed to initialize Groonga: <%d>: %s\n", |
| 165 |
rc, grn_get_global_error_message()); |
| 166 |
return EXIT_FAILURE; |
| 167 |
} |
| 168 |
bench_init(&argc, &argv); |
| 169 |
|
| 170 |
grn_ctx_init(&context, 0); |
| 171 |
|
| 172 |
data.database = setup_database(&context); |
| 173 |
|
| 174 |
reporter = bench_reporter_new(); |
| 175 |
|
| 176 |
#define REGISTER(label, bench_function) \ |
| 177 |
bench_reporter_register(reporter, label, n, \ |
| 178 |
bench_setup, \ |
| 179 |
bench_function, \ |
| 180 |
bench_teardown, \ |
| 181 |
&data) |
| 182 |
REGISTER("with mruby1", bench_with_mruby); |
| 183 |
REGISTER("without mruby1", bench_without_mruby); |
| 184 |
REGISTER("with mruby2", bench_with_mruby); |
| 185 |
REGISTER("without mruby2", bench_without_mruby); |
| 186 |
#undef REGISTER |
| 187 |
|
| 188 |
bench_reporter_run(reporter); |
| 189 |
g_object_unref(reporter); |
| 190 |
|
| 191 |
teardown_database(&context, data.database); |
| 192 |
|
| 193 |
grn_ctx_fin(&context); |
| 194 |
|
| 195 |
grn_fin(); |
| 196 |
|
| 197 |
return EXIT_SUCCESS; |
| 198 |
} |