Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-read-write.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright (C) 2008-2009 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 #include <groonga.h>
20 #include <gcutter.h>
21 #include "../lib/grn-assertions.h"
22 #include <db.h>
23 
24 void data_read_write(void);
25 void test_read_write(gconstpointer *data);
26 
27 #define VALUE_SIZE 1024
28 #define N_THREADS 100
29 
30 static grn_ctx contexts[N_THREADS];
31 static grn_obj *tables[N_THREADS];
32 
33 void
35 {
36  int i;
37  grn_init();
38  for (i = 0; i < N_THREADS; i++) {
40 
41  context = &contexts[i];
42  grn_ctx_init(context, 0);
43  // contexts[i] = NULL;
44  // grn_ctx_fin(context);
45  tables[i] = NULL;
46  }
47 }
48 
49 void
51 {
52  int i;
53 
54  for (i = 0; i < N_THREADS; i++) {
56 
57  context = &contexts[i];
58  if (context) {
59  grn_obj *table;
60 
61  table = tables[i];
62 #if 0
63  if (table)
64  grn_table_close(context, table);
65 #endif
66  grn_ctx_fin(context);
67  }
68  }
69  grn_fin();
70 }
71 
72 
73 void
74 data_read_write(void)
75 {
76  gint i;
77  const gchar *table_type, *n_processes, *process_number, *thread_type;
78 
79  table_type = g_getenv(GRN_TEST_ENV_TABLE_TYPE);
80  if (!table_type)
81  table_type = "?";
82 
83  n_processes = g_getenv(GRN_TEST_ENV_N_PROCESSES);
84  if (!n_processes)
85  n_processes = "?";
86 
87  process_number = g_getenv(GRN_TEST_ENV_PROCESS_NUMBER);
88  if (!process_number)
89  process_number = "?";
90 
91  if (g_getenv(GRN_TEST_ENV_MULTI_THREAD))
92  thread_type = "multi thread";
93  else
94  thread_type = "single thread";
95 
96  for (i = 0; i < N_THREADS; i++) {
97  cut_add_data(cut_take_printf("%s - %s process(es)(%s) - %s - %d",
98  table_type,
99  n_processes, process_number, thread_type, i),
100  GINT_TO_POINTER(i), NULL);
101  }
102 }
103 
104 void
105 test_read_write(gconstpointer *data)
106 {
107  gint i;
108  int added;
109  grn_ctx *context;
110  grn_obj *table;
111  const gchar *path;
112  const gchar *value_string;
113  gint process_number = 0;
114  const gchar *process_number_string;
115  const gchar table_name[] = "performance-read-write";
116  grn_obj value;
117  grn_obj *retrieved_value;
118  grn_id id;
119  grn_rc rc;
120 
121  i = GPOINTER_TO_INT(data);
122  process_number_string = g_getenv(GRN_TEST_ENV_PROCESS_NUMBER);
123  if (process_number_string)
124  process_number = atoi(process_number_string);
125 
126  rc = grn_ctx_init(&contexts[i], GRN_CTX_USE_QL);
127  grn_test_assert(rc, cut_set_message("context: %d (%d)", i, process_number));
128  context = &contexts[i];
129 
130  path = g_getenv(GRN_TEST_ENV_TABLE_PATH);
131  cut_assert_not_null(path);
132  tables[i] = grn_table_open(context, table_name, strlen(table_name),
133  path);
134  cut_assert_not_null(tables[i],
135  cut_message("table: %d (%d)", i, process_number));
136  table = tables[i];
137 
138  grn_test_assert_nil(grn_table_get(context, table, &i, sizeof(grn_id)),
139  cut_message("lookup - fail: (%d:%d)", i, process_number));
140 
141  value_string = cut_take_printf("value: (%d:%d)", i, process_number);
142  id = grn_table_add(context, table, &i, sizeof(grn_id), &added);
144  cut_assert_equal_int(1, added);
145 
147  GRN_TEXT_SET_REF(&value, value_string, strlen(value_string));
148  grn_obj_set_value(context, table, id, &value, GRN_OBJ_SET);
149 
150  retrieved_value = grn_obj_get_value(context, table, id, NULL);
152  id,
153  cut_message("lookup - success: (%d:%d)", i, process_number));
154  GRN_TEXT_PUTC(context, retrieved_value, '\0');
155  cut_assert_equal_string(value_string, GRN_BULK_HEAD(retrieved_value));
156 
157  tables[i] = NULL;
158  grn_test_assert(grn_obj_close(context, table));
159 
160  // contexts[i] = NULL;
161  grn_test_assert(grn_ctx_fin(context));
162 }