hg merge binary, resolving one conflict by choosing left (new 0.17 version)
[awesomized/libmemcached] / tests / atomsmasher.c
1 /*
2 Sample test application.
3 */
4 #include <assert.h>
5 #include <memcached.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/time.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13 #include <time.h>
14 #include "server.h"
15 #include "../lib/common.h"
16 #include "../src/generator.h"
17 #include "../src/execute.h"
18
19 #ifndef INT64_MAX
20 #define INT64_MAX LONG_MAX
21 #endif
22 #ifndef INT32_MAX
23 #define INT32_MAX INT_MAX
24 #endif
25
26
27 #include "test.h"
28
29 /* Number of items generated for tests */
30 #define GLOBAL_COUNT 100000
31
32 /* Number of times to run the test loop */
33 #define TEST_COUNTER 500000
34 static uint32_t global_count;
35
36 static pairs_st *global_pairs;
37 static char *global_keys[GLOBAL_COUNT];
38 static size_t global_keys_length[GLOBAL_COUNT];
39
40 uint8_t cleanup_pairs(memcached_st *memc)
41 {
42 pairs_free(global_pairs);
43
44 return 0;
45 }
46
47 uint8_t generate_pairs(memcached_st *memc)
48 {
49 unsigned long long x;
50 global_pairs= pairs_generate(GLOBAL_COUNT, 400);
51 global_count= GLOBAL_COUNT;
52
53 for (x= 0; x < global_count; x++)
54 {
55 global_keys[x]= global_pairs[x].key;
56 global_keys_length[x]= global_pairs[x].key_length;
57 }
58
59 return 0;
60 }
61
62 uint8_t drizzle(memcached_st *memc)
63 {
64 unsigned int x;
65 memcached_return rc;
66 char *return_value;
67 size_t return_value_length;
68 uint32_t flags;
69
70 infinite:
71 for (x= 0; x < TEST_COUNTER; x++)
72 {
73 uint32_t test_bit;
74 uint8_t which;
75
76 test_bit= random() % GLOBAL_COUNT;
77 which= random() % 2;
78
79 if (which == 0)
80 {
81 return_value= memcached_get(memc, global_keys[test_bit], global_keys_length[test_bit],
82 &return_value_length, &flags, &rc);
83 if (rc == MEMCACHED_SUCCESS && return_value)
84 free(return_value);
85 else if (rc == MEMCACHED_NOTFOUND)
86 continue;
87 else
88 {
89 WATCHPOINT_ERROR(rc);
90 WATCHPOINT_ASSERT(rc);
91 }
92 }
93 else
94 {
95 rc= memcached_set(memc, global_pairs[test_bit].key,
96 global_pairs[test_bit].key_length,
97 global_pairs[test_bit].value,
98 global_pairs[test_bit].value_length,
99 0, 0);
100 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_BUFFERED)
101 {
102 WATCHPOINT_ERROR(rc);
103 WATCHPOINT_ASSERT(0);
104 }
105 }
106 }
107
108 if (getenv("MEMCACHED_ATOM_BURIN_IN"))
109 goto infinite;
110
111 return 0;
112 }
113
114 memcached_return pre_nonblock(memcached_st *memc)
115 {
116 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
117
118 return MEMCACHED_SUCCESS;
119 }
120
121 memcached_return pre_md5(memcached_st *memc)
122 {
123 memcached_hash value= MEMCACHED_HASH_MD5;
124 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, value);
125
126 return MEMCACHED_SUCCESS;
127 }
128
129 memcached_return pre_hsieh(memcached_st *memc)
130 {
131 memcached_hash value= MEMCACHED_HASH_HSIEH;
132 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, value);
133
134 return MEMCACHED_SUCCESS;
135 }
136
137 memcached_return enable_consistent(memcached_st *memc)
138 {
139 memcached_server_distribution value= MEMCACHED_DISTRIBUTION_CONSISTENT;
140 memcached_hash hash;
141 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, value);
142 pre_hsieh(memc);
143
144 value= (memcached_server_distribution)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION);
145 assert(value == MEMCACHED_DISTRIBUTION_CONSISTENT);
146
147 hash= (memcached_hash)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
148 assert(hash == MEMCACHED_HASH_HSIEH);
149
150
151 return MEMCACHED_SUCCESS;
152 }
153
154 /*
155 Set the value, then quit to make sure it is flushed.
156 Come back in and test that add fails.
157 */
158 uint8_t add_test(memcached_st *memc)
159 {
160 memcached_return rc;
161 char *key= "foo";
162 char *value= "when we sanitize";
163 unsigned long long setting_value;
164
165 setting_value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NO_BLOCK);
166
167 rc= memcached_set(memc, key, strlen(key),
168 value, strlen(value),
169 (time_t)0, (uint32_t)0);
170 assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
171 memcached_quit(memc);
172 rc= memcached_add(memc, key, strlen(key),
173 value, strlen(value),
174 (time_t)0, (uint32_t)0);
175
176 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
177 if (setting_value)
178 assert(rc == MEMCACHED_NOTSTORED || MEMCACHED_STORED);
179 else
180 assert(rc == MEMCACHED_NOTSTORED);
181
182 return 0;
183 }
184
185 /*
186 * repeating add_tests many times
187 * may show a problem in timing
188 */
189 uint8_t many_adds(memcached_st *memc)
190 {
191 unsigned int i;
192 for (i = 0; i < TEST_COUNTER; i++){
193 add_test(memc);
194 }
195 return 0;
196 }
197
198 test_st smash_tests[] ={
199 {"generate_pairs", 1, generate_pairs },
200 {"drizzle", 1, drizzle },
201 {"cleanup", 1, cleanup_pairs },
202 {"many_adds", 1, many_adds },
203 {0, 0, 0}
204 };
205
206
207 collection_st collection[] ={
208 {"smash", 0, 0, smash_tests},
209 {"smash_hsieh", pre_hsieh, 0, smash_tests},
210 {"smash_hsieh_consistent", enable_consistent, 0, smash_tests},
211 {"smash_md5", pre_md5, 0, smash_tests},
212 {"smash_nonblock", pre_nonblock, 0, smash_tests},
213 {0, 0, 0, 0}
214 };
215
216 #define SERVERS_TO_CREATE 5
217
218 void *world_create(void)
219 {
220 server_startup_st *construct;
221
222 construct= (server_startup_st *)malloc(sizeof(server_startup_st));
223 memset(construct, 0, sizeof(server_startup_st));
224 construct->count= SERVERS_TO_CREATE;
225 construct->udp= 0;
226 server_startup(construct);
227
228 return construct;
229 }
230
231 void world_destroy(void *p)
232 {
233 server_startup_st *construct= (server_startup_st *)p;
234 memcached_server_st *servers= (memcached_server_st *)construct->servers;
235 memcached_server_list_free(servers);
236
237 server_shutdown(construct);
238 free(construct);
239 }
240
241 void get_world(world_st *world)
242 {
243 world->collections= collection;
244 world->create= world_create;
245 world->destroy= world_destroy;
246 }