Simplify down the benchmark caller.
[m6w6/libmemcached] / tests / libmemcached-1.0 / generate.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached Client and Server
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38 #include <config.h>
39 #include <libtest/test.hpp>
40
41 #include <libmemcached/util.h>
42 #include <libmemcached/is.h>
43
44 #include <tests/libmemcached-1.0/generate.h>
45 #include <tests/libmemcached-1.0/fetch_all_results.h>
46 #include "tests/libmemcached-1.0/callback_counter.h"
47
48 #include "clients/generator.h"
49 #include "clients/execute.h"
50
51 #define GLOBAL_COUNT 10000
52 #define GLOBAL2_COUNT 100
53
54 using namespace libtest;
55
56 static pairs_st *global_pairs;
57 static const char *global_keys[GLOBAL_COUNT];
58 static size_t global_keys_length[GLOBAL_COUNT];
59 static size_t global_count= 0;
60
61 test_return_t cleanup_pairs(memcached_st *memc)
62 {
63 (void)memc;
64 pairs_free(global_pairs);
65
66 return TEST_SUCCESS;
67 }
68
69 static test_return_t generate_pairs(memcached_st *)
70 {
71 global_pairs= pairs_generate(GLOBAL_COUNT, 400);
72 global_count= GLOBAL_COUNT;
73
74 for (size_t x= 0; x < global_count; x++)
75 {
76 global_keys[x]= global_pairs[x].key;
77 global_keys_length[x]= global_pairs[x].key_length;
78 }
79
80 return TEST_SUCCESS;
81 }
82
83 test_return_t generate_large_pairs(memcached_st *)
84 {
85 global_pairs= pairs_generate(GLOBAL2_COUNT, MEMCACHED_MAX_BUFFER+10);
86 global_count= GLOBAL2_COUNT;
87
88 for (size_t x= 0; x < global_count; x++)
89 {
90 global_keys[x]= global_pairs[x].key;
91 global_keys_length[x]= global_pairs[x].key_length;
92 }
93
94 return TEST_SUCCESS;
95 }
96
97 test_return_t generate_data(memcached_st *memc)
98 {
99 test_compare(TEST_SUCCESS, generate_pairs(memc));
100
101 unsigned int check_execute= execute_set(memc, global_pairs, global_count);
102
103 test_compare_warn_hint(global_count, check_execute, "Possible false, positive, memcached may have ejected key/value based on memory needs");
104
105 return TEST_SUCCESS;
106 }
107
108 test_return_t generate_data_with_stats(memcached_st *memc)
109 {
110 test_compare(TEST_SUCCESS, generate_pairs(memc));
111
112 unsigned int check_execute= execute_set(memc, global_pairs, global_count);
113
114 test_compare(check_execute, global_count);
115
116 // @todo hosts used size stats
117 memcached_return_t rc;
118 memcached_stat_st *stat_p= memcached_stat(memc, NULL, &rc);
119 test_true(stat_p);
120
121 for (uint32_t host_index= 0; host_index < memcached_server_count(memc); host_index++)
122 {
123 /* This test was changes so that "make test" would work properlly */
124 if (DEBUG)
125 {
126 memcached_server_instance_st instance=
127 memcached_server_instance_by_position(memc, host_index);
128
129 printf("\nserver %u|%s|%u bytes: %llu\n", host_index, instance->hostname, instance->port, (unsigned long long)(stat_p + host_index)->bytes);
130 }
131 test_true((unsigned long long)(stat_p + host_index)->bytes);
132 }
133
134 memcached_stat_free(NULL, stat_p);
135
136 return TEST_SUCCESS;
137 }
138
139 test_return_t generate_buffer_data(memcached_st *memc)
140 {
141 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, true);
142 generate_data(memc);
143
144 return TEST_SUCCESS;
145 }
146
147 test_return_t get_read_count(memcached_st *memc)
148 {
149 memcached_st *memc_clone= memcached_clone(NULL, memc);
150 test_true(memc_clone);
151
152 memcached_server_add_with_weight(memc_clone, "localhost", 6666, 0);
153
154 {
155 char *return_value;
156 size_t return_value_length;
157 uint32_t flags;
158 uint32_t count;
159
160 for (size_t x= count= 0; x < global_count; x++)
161 {
162 memcached_return_t rc;
163 return_value= memcached_get(memc_clone, global_keys[x], global_keys_length[x],
164 &return_value_length, &flags, &rc);
165 if (rc == MEMCACHED_SUCCESS)
166 {
167 count++;
168 if (return_value)
169 {
170 free(return_value);
171 }
172 }
173 }
174 }
175
176 memcached_free(memc_clone);
177
178 return TEST_SUCCESS;
179 }
180
181 test_return_t get_read(memcached_st *memc)
182 {
183 size_t keys_returned= 0;
184 for (size_t x= 0; x < global_count; x++)
185 {
186 size_t return_value_length;
187 uint32_t flags;
188 memcached_return_t rc;
189 char *return_value= memcached_get(memc, global_keys[x], global_keys_length[x],
190 &return_value_length, &flags, &rc);
191 /*
192 test_true(return_value);
193 test_compare(MEMCACHED_SUCCESS, rc);
194 */
195 if (rc == MEMCACHED_SUCCESS && return_value)
196 {
197 keys_returned++;
198 free(return_value);
199 }
200 }
201 test_compare_warn_hint(global_count, keys_returned, "Possible false, positive, memcached may have ejected key/value based on memory needs");
202
203 return TEST_SUCCESS;
204 }
205
206 test_return_t mget_read(memcached_st *memc)
207 {
208
209 test_skip(true, bool(libmemcached_util_version_check(memc, 1, 4, 4)));
210
211 test_compare(MEMCACHED_SUCCESS,
212 memcached_mget(memc, global_keys, global_keys_length, global_count));
213
214 // Go fetch the keys and test to see if all of them were returned
215 {
216 unsigned int keys_returned;
217 test_compare(TEST_SUCCESS, fetch_all_results(memc, keys_returned));
218 test_true(keys_returned > 0);
219 test_compare_warn_hint(global_count, keys_returned, "Possible false, positive, memcached may have ejected key/value based on memory needs");
220 }
221
222 return TEST_SUCCESS;
223 }
224
225 test_return_t mget_read_result(memcached_st *memc)
226 {
227
228 test_skip(true, bool(libmemcached_util_version_check(memc, 1, 4, 4)));
229
230 test_compare(MEMCACHED_SUCCESS,
231 memcached_mget(memc, global_keys, global_keys_length, global_count));
232
233 /* Turn this into a help function */
234 {
235 memcached_result_st results_obj;
236 memcached_result_st *results= memcached_result_create(memc, &results_obj);
237 test_true(results);
238
239 memcached_return_t rc;
240 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
241 {
242 if (rc == MEMCACHED_IN_PROGRESS)
243 {
244 continue;
245 }
246
247 test_true(results);
248 test_compare(MEMCACHED_SUCCESS, rc);
249 }
250 test_compare(MEMCACHED_END, rc);
251
252 memcached_result_free(&results_obj);
253 }
254
255 return TEST_SUCCESS;
256 }
257
258 test_return_t mget_read_partial_result(memcached_st *memc)
259 {
260
261 test_skip(true, bool(libmemcached_util_version_check(memc, 1, 4, 4)));
262
263 test_compare(MEMCACHED_SUCCESS,
264 memcached_mget(memc, global_keys, global_keys_length, global_count));
265
266 // We will scan for just one key
267 {
268 memcached_result_st results_obj;
269 memcached_result_st *results= memcached_result_create(memc, &results_obj);
270
271 memcached_return_t rc;
272 results= memcached_fetch_result(memc, results, &rc);
273 test_true(results);
274 test_compare(MEMCACHED_SUCCESS, rc);
275
276 memcached_result_free(&results_obj);
277 }
278
279 // We already have a read happening, lets start up another one.
280 test_compare(MEMCACHED_SUCCESS,
281 memcached_mget(memc, global_keys, global_keys_length, global_count));
282 {
283 memcached_result_st results_obj;
284 memcached_result_st *results= memcached_result_create(memc, &results_obj);
285 test_true(results);
286 test_false(memcached_is_allocated(results));
287
288 memcached_return_t rc;
289 while ((results= memcached_fetch_result(memc, &results_obj, &rc)))
290 {
291 test_true(results);
292 test_compare(MEMCACHED_SUCCESS, rc);
293 }
294 test_compare(MEMCACHED_END, rc);
295
296 memcached_result_free(&results_obj);
297 }
298
299 return TEST_SUCCESS;
300 }
301
302 test_return_t mget_read_function(memcached_st *memc)
303 {
304 test_skip(true, bool(libmemcached_util_version_check(memc, 1, 4, 4)));
305
306 test_compare(MEMCACHED_SUCCESS,
307 memcached_mget(memc, global_keys, global_keys_length, global_count));
308
309 memcached_execute_fn callbacks[]= { &callback_counter };
310 size_t counter= 0;
311 test_compare(MEMCACHED_SUCCESS,
312 memcached_fetch_execute(memc, callbacks, (void *)&counter, 1));
313
314 return TEST_SUCCESS;
315 }
316
317 test_return_t delete_generate(memcached_st *memc)
318 {
319 size_t total= 0;
320 for (size_t x= 0; x < global_count; x++)
321 {
322 if (memcached_success(memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0)))
323 {
324 total++;
325 }
326 }
327 test_compare_warn_hint(global_count, total, "Possible false, positive, memcached may have ejected key/value based on memory needs");
328
329 return TEST_SUCCESS;
330 }
331
332 test_return_t delete_buffer_generate(memcached_st *memc)
333 {
334 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, true);
335
336 size_t total= 0;
337 for (size_t x= 0; x < global_count; x++)
338 {
339 if (memcached_success(memcached_delete(memc, global_keys[x], global_keys_length[x], (time_t)0)))
340 {
341 total++;
342 }
343 }
344 test_compare_warn_hint(global_count, total, "Possible false, positive, memcached may have ejected key/value based on memory needs");
345
346 return TEST_SUCCESS;
347 }
348
349 test_return_t mget_read_internal_result(memcached_st *memc)
350 {
351
352 test_skip(true, bool(libmemcached_util_version_check(memc, 1, 4, 4)));
353
354 test_compare(MEMCACHED_SUCCESS,
355 memcached_mget(memc, global_keys, global_keys_length, global_count));
356 {
357 memcached_result_st *results= NULL;
358 memcached_return_t rc;
359 while ((results= memcached_fetch_result(memc, results, &rc)))
360 {
361 test_true(results);
362 test_compare(MEMCACHED_SUCCESS, rc);
363 }
364 test_compare(MEMCACHED_END, rc);
365
366 memcached_result_free(results);
367 }
368
369 return TEST_SUCCESS;
370 }
371