Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
bench-ctx-create.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright (C) 2013 Kouhei Sutou <kou@clear-code.com>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License version 2.1 as published by the Free Software Foundation.
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: 4851bf7c20fd37e7c40f350dade3716d3e210805
21  CFLAGS: -O0 -g3
22  % (cd benchmark/ && make --quiet run-bench-ctx-create)
23  run-bench-ctx-create:
24  ./bench-ctx-create
25  (time)
26  with mruby: 404KB (0.1260ms)
27  without mruby: 36KB (0.0610ms)
28 */
29 
30 #include <stdlib.h>
31 
32 #include <glib.h>
33 
34 #include <groonga.h>
35 
36 #include "lib/benchmark.h"
37 
38 typedef struct _BenchmarkData {
43 
44 static guint
45 get_memory_usage(void)
46 {
47  GRegex *vm_rss_pattern;
48  gchar *status;
49  GMatchInfo *match_info;
50  gchar *vm_rss_string;
51  guint vm_rss;
52 
53  g_file_get_contents("/proc/self/status", &status, NULL, NULL);
54 
55  vm_rss_pattern = g_regex_new("VmRSS:\\s*(\\d*)\\s+kB", 0, 0, NULL);
56  if (!g_regex_match(vm_rss_pattern, status, 0, &match_info)) {
57  g_print("not match...: %s\n", status);
58  return 0;
59  }
60  vm_rss_string = g_match_info_fetch(match_info, 1);
61  vm_rss = atoi(vm_rss_string);
62  g_free(vm_rss_string);
63  g_match_info_free(match_info);
64  g_regex_unref(vm_rss_pattern);
65  g_free(status);
66 
67  return vm_rss;
68 }
69 
70 static void
71 bench_with_mruby(gpointer user_data)
72 {
73  BenchmarkData *data = user_data;
74 
75  g_setenv("GRN_MRUBY_ENABLED", "yes", TRUE);
76  grn_ctx_init(&(data->context), 0);
77  grn_ctx_use(&(data->context), data->database);
78 }
79 
80 static void
81 bench_without_mruby(gpointer user_data)
82 {
83  BenchmarkData *data = user_data;
84 
85  g_setenv("GRN_MRUBY_ENABLED", "no", TRUE);
86  grn_ctx_init(&(data->context), 0);
87  grn_ctx_use(&(data->context), data->database);
88 }
89 
90 static void
91 bench_setup(gpointer user_data)
92 {
93  BenchmarkData *data = user_data;
94 
95  data->memory_usage_before = get_memory_usage();
96 }
97 
98 static void
99 bench_teardown(gpointer user_data)
100 {
101  BenchmarkData *data = user_data;
102 
103  grn_ctx_fin(&(data->context));
104  g_print("%3dKB ", get_memory_usage() - data->memory_usage_before);
105 }
106 
107 static gchar *
108 get_tmp_dir(void)
109 {
110  gchar *current_dir;
111  gchar *tmp_dir;
112 
113  current_dir = g_get_current_dir();
114  tmp_dir = g_build_filename(current_dir, "tmp", NULL);
115  g_free(current_dir);
116 
117  return tmp_dir;
118 }
119 
120 static grn_obj *
121 setup_database(grn_ctx *context)
122 {
123  gchar *tmp_dir;
124  gchar *database_path;
125  grn_obj *database;
126 
127  tmp_dir = get_tmp_dir();
128  database_path = g_build_filename(tmp_dir, "ctx-create", "db", NULL);
129  database = grn_db_open(context, database_path);
130 
131  g_free(database_path);
132 
133  return database;
134 }
135 
136 static void
137 teardown_database(grn_ctx *context, grn_obj *database)
138 {
139  grn_obj_close(context, database);
140 }
141 
142 int
143 main(int argc, gchar **argv)
144 {
146  BenchmarkData data;
147  BenchReporter *reporter;
148  gint n = 1;
149 
150  grn_init();
151  bench_init(&argc, &argv);
152 
153  grn_ctx_init(&context, 0);
154 
155  data.database = setup_database(&context);
156 
157  reporter = bench_reporter_new();
158 
159 #define REGISTER(label, bench_function) \
160  bench_reporter_register(reporter, label, n, \
161  bench_setup, \
162  bench_function, \
163  bench_teardown, \
164  &data)
165  REGISTER("with mruby", bench_with_mruby);
166  REGISTER("without mruby", bench_without_mruby);
167 #undef REGISTER
168 
169  bench_reporter_run(reporter);
170  g_object_unref(reporter);
171 
172  teardown_database(&context, data.database);
173 
174  grn_ctx_fin(&context);
175 
176  grn_fin();
177 
178  return 0;
179 }