Debian Codegraph @

benchmark/bench-result-set.c

raw symbols SCIP
1 /*
2 Copyright (C) 2015-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 #include <string.h>
20 #include <stdlib.h>
21
22 #include <groonga.h>
23
24 #include "lib/benchmark.h"
25
26 typedef struct _BenchmarkData
27 {
28 gchar *base_dir;
29 grn_ctx *context;
30 grn_obj *source_table;
31 grn_obj *result_set;
32 } BenchmarkData;
33
34 static void
35 bench_n(BenchmarkData *data, gint64 n)
36 {
37 gint64 i;
38 grn_ctx *ctx;
39 grn_hash *result_set;
40
41 ctx = data->context;
42 result_set = (grn_hash *)data->result_set;
43 for (i = 0; i < n; i++) {
44 grn_id id = i;
45 grn_hash_add(ctx, result_set, &id, sizeof(grn_id), NULL, NULL);
46 }
47 }
48
49 static void
50 bench_1000(gpointer user_data)
51 {
52 BenchmarkData *data = user_data;
53 bench_n(data, 1000);
54 }
55
56 static void
57 bench_10000(gpointer user_data)
58 {
59 BenchmarkData *data = user_data;
60 bench_n(data, 10000);
61 }
62
63 static void
64 bench_100000(gpointer user_data)
65 {
66 BenchmarkData *data = user_data;
67 bench_n(data, 100000);
68 }
69
70 static void
71 bench_setup(gpointer user_data)
72 {
73 BenchmarkData *data = user_data;
74
75 data->result_set = grn_table_create(data->context,
76 NULL, 0, NULL,
77 GRN_TABLE_HASH_KEY | GRN_OBJ_WITH_SUBREC,
78 data->source_table,
79 NULL);
80
81 }
82
83 static void
84 bench_teardown(gpointer user_data)
85 {
86 BenchmarkData *data = user_data;
87
88 grn_obj_close(data->context, data->result_set);
89 }
90
91 int
92 main(int argc, gchar **argv)
93 {
94 grn_rc rc;
95 BenchmarkData data;
96 BenchReporter *reporter;
97 gchar *base_dir;
98 grn_ctx ctx;
99 gint n = 100;
100
101 rc = grn_init();
102 if (rc != GRN_SUCCESS) {
103 g_print("failed to initialize Groonga: <%d>: %s\n",
104 rc, grn_get_global_error_message());
105 return EXIT_FAILURE;
106 }
107 bench_init(&argc, &argv);
108
109 data.context = &ctx;
110
111 base_dir = g_build_filename(g_get_tmp_dir(), "groonga-bench", NULL);
112 bench_utils_remove_path_recursive_force(base_dir);
113 g_mkdir_with_parents(base_dir, 0755);
114
115 {
116 gchar *database_path;
117 const gchar *source_table_name = "Sources";
118
119 grn_ctx_init(&ctx, 0);
120 database_path = g_build_filename(base_dir, "db", NULL);
121 grn_db_create(&ctx, database_path, NULL);
122 g_free(database_path);
123
124 data.source_table = grn_table_create(&ctx,
125 source_table_name,
126 strlen(source_table_name),
127 NULL,
128 GRN_TABLE_PAT_KEY | GRN_OBJ_PERSISTENT,
129 grn_ctx_at(&ctx, GRN_DB_SHORT_TEXT),
130 NULL);
131 }
132
133 reporter = bench_reporter_new();
134 bench_reporter_register(reporter, "1000", n,
135 bench_setup, bench_1000, bench_teardown, &data);
136 bench_reporter_register(reporter, "10000", n,
137 bench_setup, bench_10000, bench_teardown, &data);
138 bench_reporter_register(reporter, "100000", n,
139 bench_setup, bench_100000, bench_teardown, &data);
140 bench_reporter_run(reporter);
141 g_object_unref(reporter);
142
143 grn_ctx_fin(&ctx);
144
145 bench_utils_remove_path_recursive_force(data.base_dir);
146
147 bench_quit();
148 grn_fin();
149
150 return EXIT_SUCCESS;
151 }