c267fcee546b24e3dc45b2467493b4c8a57eef55
[m6w6/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 #define GLOBAL_COUNT 100000
30 #define TEST_COUNTER 100000
31 static uint32_t global_count;
32
33 static pairs_st *global_pairs;
34 static char *global_keys[GLOBAL_COUNT];
35 static size_t global_keys_length[GLOBAL_COUNT];
36
37 uint8_t cleanup_pairs(memcached_st *memc)
38 {
39 pairs_free(global_pairs);
40
41 return 0;
42 }
43
44 uint8_t generate_pairs(memcached_st *memc)
45 {
46 unsigned long long x;
47 global_pairs= pairs_generate(GLOBAL_COUNT, 400);
48 global_count= GLOBAL_COUNT;
49
50 for (x= 0; x < global_count; x++)
51 {
52 global_keys[x]= global_pairs[x].key;
53 global_keys_length[x]= global_pairs[x].key_length;
54 }
55
56 return 0;
57 }
58
59 uint8_t drizzle(memcached_st *memc)
60 {
61 unsigned int x;
62 memcached_return rc;
63
64 {
65 char *return_value;
66 size_t return_value_length;
67 uint32_t flags;
68
69 for (x= 0; x < TEST_COUNTER; x++)
70 {
71 uint32_t test_bit;
72 uint8_t which;
73
74 test_bit= random() % GLOBAL_COUNT;
75 which= random() % 2;
76
77 if (which == 0)
78 {
79 return_value= memcached_get(memc, global_keys[test_bit], global_keys_length[test_bit],
80 &return_value_length, &flags, &rc);
81 if (rc == MEMCACHED_SUCCESS && return_value)
82 free(return_value);
83 else
84 WATCHPOINT_ERROR(rc);
85 }
86 else
87 {
88 rc= memcached_set(memc, global_pairs[test_bit].key,
89 global_pairs[test_bit].key_length,
90 global_pairs[test_bit].value,
91 global_pairs[test_bit].value_length,
92 0, 0);
93 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_BUFFERED)
94 {
95 WATCHPOINT_ERROR(rc);
96 WATCHPOINT_ASSERT(0);
97 }
98 }
99 }
100 }
101
102 return 0;
103 }
104
105 memcached_return pre_nonblock(memcached_st *memc)
106 {
107 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, NULL);
108
109 return MEMCACHED_SUCCESS;
110 }
111
112 memcached_return pre_md5(memcached_st *memc)
113 {
114 memcached_hash value= MEMCACHED_HASH_MD5;
115 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
116
117 return MEMCACHED_SUCCESS;
118 }
119
120 memcached_return pre_hsieh(memcached_st *memc)
121 {
122 memcached_hash value= MEMCACHED_HASH_HSIEH;
123 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, &value);
124
125 return MEMCACHED_SUCCESS;
126 }
127
128 memcached_return enable_consistent(memcached_st *memc)
129 {
130 memcached_server_distribution value= MEMCACHED_DISTRIBUTION_CONSISTENT;
131 memcached_hash hash;
132 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, &value);
133 pre_hsieh(memc);
134
135 value= (memcached_server_distribution)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION);
136 assert(value == MEMCACHED_DISTRIBUTION_CONSISTENT);
137
138 hash= (memcached_hash)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
139 assert(hash == MEMCACHED_HASH_HSIEH);
140
141
142 return MEMCACHED_SUCCESS;
143 }
144
145 test_st generate_tests[] ={
146 {"generate_pairs", 1, generate_pairs },
147 {"cleanup", 1, cleanup_pairs },
148 {0, 0, 0}
149 };
150
151
152 collection_st collection[] ={
153 {"generate", 0, 0, generate_tests},
154 {"generate_hsieh", pre_hsieh, 0, generate_tests},
155 {"generate_hsieh_consistent", enable_consistent, 0, generate_tests},
156 {"generate_md5", pre_md5, 0, generate_tests},
157 {"generate_nonblock", pre_nonblock, 0, generate_tests},
158 {0, 0, 0, 0}
159 };
160
161 #define SERVERS_TO_CREATE 5
162
163 void *world_create(void)
164 {
165 server_startup_st *construct;
166
167 construct= (server_startup_st *)malloc(sizeof(server_startup_st));
168 memset(construct, 0, sizeof(server_startup_st));
169 construct->count= SERVERS_TO_CREATE;
170 construct->udp= 0;
171 server_startup(construct);
172
173 return construct;
174 }
175
176 void world_destroy(void *p)
177 {
178 server_startup_st *construct= (server_startup_st *)p;
179 memcached_server_st *servers= (memcached_server_st *)construct->servers;
180 memcached_server_list_free(servers);
181
182 server_shutdown(construct);
183 free(construct);
184 }
185
186 void get_world(world_st *world)
187 {
188 world->collections= collection;
189 world->create= world_create;
190 world->destroy= world_destroy;
191 }