3f4eb405ab288247a6e41553bdd7e54703fb447b
[awesomized/libmemcached] / tests / atomsmasher.cc
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11
12 /*
13 Sample test application.
14 */
15 #include <config.h>
16
17 #include <libtest/test.hpp>
18
19 #include <libmemcached/memcached.h>
20 #include <libmemcached/watchpoint.h>
21
22 #include <cstdio>
23 #include <cstdlib>
24 #include <stdint.h>
25 #include <cstring>
26 #include <sys/time.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <ctime>
31 #include <clients/generator.h>
32 #include <clients/execute.h>
33
34 #include <libtest/server.h>
35
36 using namespace libtest;
37
38 /* Number of items generated for tests */
39 #define GLOBAL_COUNT 100000
40
41 /* Number of times to run the test loop */
42 #define TEST_COUNTER 500000
43 static uint32_t global_count;
44
45 static pairs_st *global_pairs;
46 static char *global_keys[GLOBAL_COUNT];
47 static size_t global_keys_length[GLOBAL_COUNT];
48
49 static test_return_t cleanup_pairs(memcached_st *memc)
50 {
51 (void)memc;
52 pairs_free(global_pairs);
53
54 return TEST_SUCCESS;
55 }
56
57 static test_return_t generate_pairs(memcached_st *memc)
58 {
59 (void)memc;
60 global_pairs= pairs_generate(GLOBAL_COUNT, 400);
61 global_count= GLOBAL_COUNT;
62
63 for (size_t x= 0; x < global_count; x++)
64 {
65 global_keys[x]= global_pairs[x].key;
66 global_keys_length[x]= global_pairs[x].key_length;
67 }
68
69 return TEST_SUCCESS;
70 }
71
72 static test_return_t drizzle(memcached_st *memc)
73 {
74 memcached_return_t rc;
75 char *return_value;
76 size_t return_value_length;
77 uint32_t flags;
78
79 infinite:
80 for (size_t x= 0; x < TEST_COUNTER; x++)
81 {
82 uint32_t test_bit;
83 uint8_t which;
84
85 test_bit= (uint32_t)(random() % GLOBAL_COUNT);
86 which= (uint8_t)(random() % 2);
87
88 if (which == 0)
89 {
90 return_value= memcached_get(memc, global_keys[test_bit], global_keys_length[test_bit],
91 &return_value_length, &flags, &rc);
92 if (rc == MEMCACHED_SUCCESS && return_value)
93 {
94 free(return_value);
95 }
96 else if (rc == MEMCACHED_NOTFOUND)
97 {
98 continue;
99 }
100 else
101 {
102 WATCHPOINT_ERROR(rc);
103 WATCHPOINT_ASSERT(rc);
104 }
105 }
106 else
107 {
108 rc= memcached_set(memc, global_pairs[test_bit].key,
109 global_pairs[test_bit].key_length,
110 global_pairs[test_bit].value,
111 global_pairs[test_bit].value_length,
112 0, 0);
113 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_BUFFERED)
114 {
115 WATCHPOINT_ERROR(rc);
116 WATCHPOINT_ASSERT(0);
117 }
118 }
119 }
120
121 if (getenv("MEMCACHED_ATOM_BURIN_IN"))
122 goto infinite;
123
124 return TEST_SUCCESS;
125 }
126
127 static test_return_t pre_nonblock(memcached_st *memc)
128 {
129 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
130
131 return TEST_SUCCESS;
132 }
133
134 /*
135 Set the value, then quit to make sure it is flushed.
136 Come back in and test that add fails.
137 */
138 static test_return_t add_test(memcached_st *memc)
139 {
140 memcached_return_t rc;
141 const char *key= "foo";
142 const char *value= "when we sanitize";
143 unsigned long long setting_value;
144
145 setting_value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
146
147 rc= memcached_set(memc, key, strlen(key),
148 value, strlen(value),
149 (time_t)0, (uint32_t)0);
150 test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
151 memcached_quit(memc);
152 rc= memcached_add(memc, key, strlen(key),
153 value, strlen(value),
154 (time_t)0, (uint32_t)0);
155
156 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
157 if (setting_value)
158 {
159 test_true(rc == MEMCACHED_NOTSTORED || rc == MEMCACHED_STORED);
160 }
161 else
162 {
163 test_true(rc == MEMCACHED_NOTSTORED);
164 }
165
166 return TEST_SUCCESS;
167 }
168
169 /*
170 * repeating add_tests many times
171 * may show a problem in timing
172 */
173 static test_return_t many_adds(memcached_st *memc)
174 {
175 for (size_t x= 0; x < TEST_COUNTER; x++)
176 {
177 add_test(memc);
178 }
179 return TEST_SUCCESS;
180 }
181
182 test_st smash_tests[] ={
183 {"generate_pairs", 1, (test_callback_fn*)generate_pairs },
184 {"drizzle", 1, (test_callback_fn*)drizzle },
185 {"cleanup", 1, (test_callback_fn*)cleanup_pairs },
186 {"many_adds", 1, (test_callback_fn*)many_adds },
187 {0, 0, 0}
188 };
189
190 #define BENCHMARK_TEST_LOOP 20000
191
192 struct benchmark_state_st
193 {
194 bool create_init;
195 bool clone_init;
196 memcached_st *create;
197 memcached_st *clone;
198 } benchmark_state;
199
200 static test_return_t memcached_create_benchmark(memcached_st *memc)
201 {
202 (void)memc;
203 benchmark_state.create_init= true;
204
205 for (size_t x= 0; x < BENCHMARK_TEST_LOOP; x++)
206 {
207 memcached_st *ptr;
208 ptr= memcached_create(&benchmark_state.create[x]);
209
210 test_true(ptr);
211 }
212
213 return TEST_SUCCESS;
214 }
215
216 static test_return_t memcached_clone_benchmark(memcached_st *memc)
217 {
218 benchmark_state.clone_init= true;
219
220 for (size_t x= 0; x < BENCHMARK_TEST_LOOP; x++)
221 {
222 memcached_st *ptr;
223 ptr= memcached_clone(&benchmark_state.clone[x], memc);
224
225 test_true(ptr);
226 }
227
228 return TEST_SUCCESS;
229 }
230
231 static test_return_t pre_allocate(memcached_st *memc)
232 {
233 (void)memc;
234 memset(&benchmark_state, 0, sizeof(benchmark_state));
235
236 benchmark_state.create= (memcached_st *)calloc(BENCHMARK_TEST_LOOP, sizeof(memcached_st));
237 test_true(benchmark_state.create);
238 benchmark_state.clone= (memcached_st *)calloc(BENCHMARK_TEST_LOOP, sizeof(memcached_st));
239 test_true(benchmark_state.clone);
240
241 return TEST_SUCCESS;
242 }
243
244 static test_return_t post_allocate(memcached_st *memc)
245 {
246 (void)memc;
247 for (size_t x= 0; x < BENCHMARK_TEST_LOOP; x++)
248 {
249 if (benchmark_state.create_init)
250 memcached_free(&benchmark_state.create[x]);
251
252 if (benchmark_state.clone_init)
253 memcached_free(&benchmark_state.clone[x]);
254 }
255
256 free(benchmark_state.create);
257 free(benchmark_state.clone);
258
259 return TEST_SUCCESS;
260 }
261
262
263 test_st micro_tests[] ={
264 {"memcached_create", 1, (test_callback_fn*)memcached_create_benchmark },
265 {"memcached_clone", 1, (test_callback_fn*)memcached_clone_benchmark },
266 {0, 0, 0}
267 };
268
269
270 collection_st collection[] ={
271 {"smash", 0, 0, smash_tests},
272 {"smash_nonblock", (test_callback_fn*)pre_nonblock, 0, smash_tests},
273 {"micro-benchmark", (test_callback_fn*)pre_allocate, (test_callback_fn*)post_allocate, micro_tests},
274 {0, 0, 0, 0}
275 };
276
277
278 #define SERVERS_TO_CREATE 5
279
280 #include "libmemcached_world.h"
281
282 void get_world(Framework *world)
283 {
284 world->collections= collection;
285
286 world->_create= (test_callback_create_fn*)world_create;
287 world->_destroy= (test_callback_destroy_fn*)world_destroy;
288
289 world->item._startup= (test_callback_fn*)world_test_startup;
290 world->item._flush= (test_callback_fn*)world_flush;
291 world->item.set_pre((test_callback_fn*)world_pre_run);
292 world->item.set_post((test_callback_fn*)world_post_run);
293 world->_on_error= (test_callback_error_fn*)world_on_error;
294
295 world->collection_startup= (test_callback_fn*)world_container_startup;
296 world->collection_shutdown= (test_callback_fn*)world_container_shutdown;
297
298 world->set_runner(&defualt_libmemcached_runner);
299 }