Debian Codegraph @

benchmark/bench-ctx-create.c

raw symbols SCIP
1 /*
2 Copyright (C) 2013-2016 Kouhei Sutou <[email protected]>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 /*
20 Groonga: 6b128f318d682e50648f3b82c5f7956f3bcb3fe8
21 CFLAGS: -O0 -g3
22 % (cd benchmark/ && make --quiet run-bench-ctx-create)
23 run-bench-ctx-create:
24 (time)
25 with mruby1: 288KB (0.0091s)
26 without mruby1: 28KB (0.0240ms)
27 with mruby2: 0KB (0.0097s)
28 without mruby2: 0KB (0.0220ms)
29
30 Groonga: c4379140c02699e3c74b94cd9e7b88d372202aa5
31 CFLAGS: -O2 -g
32 % make --quiet -C benchmark run-bench-ctx-create
33 run-bench-ctx-create:
34 (time)
35 with mruby1: 524KB (0.0041s)
36 without mruby1: 32KB (0.0220ms)
37 with mruby2: 0KB (0.0040s)
38 without mruby2: 0KB (0.0200ms)
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 }