| 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 |
#include <string.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#include <stdio.h> |
| 39 |
|
| 40 |
#include <grn_cache.h> |
| 41 |
|
| 42 |
#include "lib/benchmark.h" |
| 43 |
|
| 44 |
typedef struct _BenchmarkData |
| 45 |
{ |
| 46 |
grn_ctx *context; |
| 47 |
grn_cache *cache; |
| 48 |
grn_obj value; |
| 49 |
} BenchmarkData; |
| 50 |
|
| 51 |
static void |
| 52 |
bench_n(BenchmarkData *data, gint64 n) |
| 53 |
{ |
| 54 |
gint64 i; |
| 55 |
grn_ctx *ctx; |
| 56 |
grn_cache *cache; |
| 57 |
grn_obj *value; |
| 58 |
grn_obj fetch_buffer; |
| 59 |
|
| 60 |
ctx = data->context; |
| 61 |
cache = data->cache; |
| 62 |
value = &(data->value); |
| 63 |
GRN_TEXT_INIT(&fetch_buffer, 0); |
| 64 |
for (i = 0; i < n; i++) { |
| 65 |
char key[GRN_TABLE_MAX_KEY_SIZE]; |
| 66 |
grn_snprintf(key, |
| 67 |
GRN_TABLE_MAX_KEY_SIZE, |
| 68 |
GRN_TABLE_MAX_KEY_SIZE, |
| 69 |
"key:%" GRN_FMT_INT64D, |
| 70 |
i); |
| 71 |
GRN_BULK_REWIND(&fetch_buffer); |
| 72 |
grn_cache_fetch(ctx, cache, key, strlen(key), &fetch_buffer); |
| 73 |
grn_cache_update(ctx, cache, key, strlen(key), value); |
| 74 |
} |
| 75 |
GRN_OBJ_FIN(ctx, &fetch_buffer); |
| 76 |
} |
| 77 |
|
| 78 |
static void |
| 79 |
bench_1000(gpointer user_data) |
| 80 |
{ |
| 81 |
BenchmarkData *data = user_data; |
| 82 |
bench_n(data, 1000); |
| 83 |
} |
| 84 |
|
| 85 |
static void |
| 86 |
bench_10000(gpointer user_data) |
| 87 |
{ |
| 88 |
BenchmarkData *data = user_data; |
| 89 |
bench_n(data, 10000); |
| 90 |
} |
| 91 |
|
| 92 |
static void |
| 93 |
bench_setup(gpointer user_data) |
| 94 |
{ |
| 95 |
BenchmarkData *data = user_data; |
| 96 |
|
| 97 |
data->cache = grn_cache_open(data->context); |
| 98 |
GRN_TEXT_INIT(&(data->value), 0); |
| 99 |
while (GRN_TEXT_LEN(&(data->value)) < 1024) { |
| 100 |
GRN_TEXT_PUTS(data->context, &(data->value), "XXXXXXXXXXX"); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
static void |
| 105 |
bench_teardown(gpointer user_data) |
| 106 |
{ |
| 107 |
BenchmarkData *data = user_data; |
| 108 |
|
| 109 |
grn_obj_close(data->context, &(data->value)); |
| 110 |
grn_cache_close(data->context, data->cache); |
| 111 |
} |
| 112 |
|
| 113 |
int |
| 114 |
main(int argc, gchar **argv) |
| 115 |
{ |
| 116 |
grn_rc rc; |
| 117 |
BenchmarkData data; |
| 118 |
BenchReporter *reporter; |
| 119 |
gchar *base_dir; |
| 120 |
grn_ctx ctx; |
| 121 |
gint n = 100; |
| 122 |
|
| 123 |
rc = grn_init(); |
| 124 |
if (rc != GRN_SUCCESS) { |
| 125 |
g_print("failed to initialize Groonga: <%d>: %s\n", |
| 126 |
rc, grn_get_global_error_message()); |
| 127 |
return EXIT_FAILURE; |
| 128 |
} |
| 129 |
bench_init(&argc, &argv); |
| 130 |
|
| 131 |
grn_ctx_init(&ctx, 0); |
| 132 |
|
| 133 |
data.context = &ctx; |
| 134 |
|
| 135 |
base_dir = g_build_filename(g_get_tmp_dir(), "groonga-bench", NULL); |
| 136 |
bench_utils_remove_path_recursive_force(base_dir); |
| 137 |
g_mkdir_with_parents(base_dir, 0755); |
| 138 |
|
| 139 |
reporter = bench_reporter_new(); |
| 140 |
bench_reporter_register(reporter, "1000", n, |
| 141 |
bench_setup, bench_1000, bench_teardown, &data); |
| 142 |
bench_reporter_register(reporter, "10000", n, |
| 143 |
bench_setup, bench_10000, bench_teardown, &data); |
| 144 |
bench_reporter_run(reporter); |
| 145 |
g_object_unref(reporter); |
| 146 |
|
| 147 |
grn_ctx_fin(&ctx); |
| 148 |
|
| 149 |
bench_utils_remove_path_recursive_force(base_dir); |
| 150 |
|
| 151 |
bench_quit(); |
| 152 |
grn_fin(); |
| 153 |
|
| 154 |
return EXIT_SUCCESS; |
| 155 |
} |