2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
13 Sample test application.
17 #include "libmemcached/memcached.h"
18 #include "libmemcached/watchpoint.h"
25 #include <sys/types.h>
29 #include "../clients/generator.h"
30 #include "../clients/execute.h"
32 #include <libtest/server.h>
33 #include <libtest/test.h>
35 /* Number of items generated for tests */
36 #define GLOBAL_COUNT 100000
38 /* Number of times to run the test loop */
39 #define TEST_COUNTER 500000
40 static uint32_t global_count
;
42 static pairs_st
*global_pairs
;
43 static char *global_keys
[GLOBAL_COUNT
];
44 static size_t global_keys_length
[GLOBAL_COUNT
];
46 static test_return_t
cleanup_pairs(memcached_st
*memc
)
49 pairs_free(global_pairs
);
54 static test_return_t
generate_pairs(memcached_st
*memc
)
57 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
58 global_count
= GLOBAL_COUNT
;
60 for (size_t x
= 0; x
< global_count
; x
++)
62 global_keys
[x
]= global_pairs
[x
].key
;
63 global_keys_length
[x
]= global_pairs
[x
].key_length
;
69 static test_return_t
drizzle(memcached_st
*memc
)
71 memcached_return_t rc
;
73 size_t return_value_length
;
77 for (size_t x
= 0; x
< TEST_COUNTER
; x
++)
82 test_bit
= (uint32_t)(random() % GLOBAL_COUNT
);
83 which
= (uint8_t)(random() % 2);
87 return_value
= memcached_get(memc
, global_keys
[test_bit
], global_keys_length
[test_bit
],
88 &return_value_length
, &flags
, &rc
);
89 if (rc
== MEMCACHED_SUCCESS
&& return_value
)
93 else if (rc
== MEMCACHED_NOTFOUND
)
100 WATCHPOINT_ASSERT(rc
);
105 rc
= memcached_set(memc
, global_pairs
[test_bit
].key
,
106 global_pairs
[test_bit
].key_length
,
107 global_pairs
[test_bit
].value
,
108 global_pairs
[test_bit
].value_length
,
110 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
112 WATCHPOINT_ERROR(rc
);
113 WATCHPOINT_ASSERT(0);
118 if (getenv("MEMCACHED_ATOM_BURIN_IN"))
124 static test_return_t
pre_nonblock(memcached_st
*memc
)
126 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
, 0);
132 Set the value, then quit to make sure it is flushed.
133 Come back in and test that add fails.
135 static test_return_t
add_test(memcached_st
*memc
)
137 memcached_return_t rc
;
138 const char *key
= "foo";
139 const char *value
= "when we sanitize";
140 unsigned long long setting_value
;
142 setting_value
= memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
);
144 rc
= memcached_set(memc
, key
, strlen(key
),
145 value
, strlen(value
),
146 (time_t)0, (uint32_t)0);
147 test_true(rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
);
148 memcached_quit(memc
);
149 rc
= memcached_add(memc
, key
, strlen(key
),
150 value
, strlen(value
),
151 (time_t)0, (uint32_t)0);
153 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
156 test_true(rc
== MEMCACHED_NOTSTORED
|| rc
== MEMCACHED_STORED
);
160 test_true(rc
== MEMCACHED_NOTSTORED
);
167 * repeating add_tests many times
168 * may show a problem in timing
170 static test_return_t
many_adds(memcached_st
*memc
)
172 for (size_t x
= 0; x
< TEST_COUNTER
; x
++)
179 test_st smash_tests
[] ={
180 {"generate_pairs", 1, (test_callback_fn
)generate_pairs
},
181 {"drizzle", 1, (test_callback_fn
)drizzle
},
182 {"cleanup", 1, (test_callback_fn
)cleanup_pairs
},
183 {"many_adds", 1, (test_callback_fn
)many_adds
},
187 #define BENCHMARK_TEST_LOOP 20000
189 struct benchmark_state_st
193 memcached_st
*create
;
197 static test_return_t
memcached_create_benchmark(memcached_st
*memc
)
200 benchmark_state
.create_init
= true;
202 for (size_t x
= 0; x
< BENCHMARK_TEST_LOOP
; x
++)
205 ptr
= memcached_create(&benchmark_state
.create
[x
]);
213 static test_return_t
memcached_clone_benchmark(memcached_st
*memc
)
215 benchmark_state
.clone_init
= true;
217 for (size_t x
= 0; x
< BENCHMARK_TEST_LOOP
; x
++)
220 ptr
= memcached_clone(&benchmark_state
.clone
[x
], memc
);
228 static test_return_t
pre_allocate(memcached_st
*memc
)
231 memset(&benchmark_state
, 0, sizeof(benchmark_state
));
233 benchmark_state
.create
= (memcached_st
*)calloc(BENCHMARK_TEST_LOOP
, sizeof(memcached_st
));
234 test_true(benchmark_state
.create
);
235 benchmark_state
.clone
= (memcached_st
*)calloc(BENCHMARK_TEST_LOOP
, sizeof(memcached_st
));
236 test_true(benchmark_state
.clone
);
241 static test_return_t
post_allocate(memcached_st
*memc
)
244 for (size_t x
= 0; x
< BENCHMARK_TEST_LOOP
; x
++)
246 if (benchmark_state
.create_init
)
247 memcached_free(&benchmark_state
.create
[x
]);
249 if (benchmark_state
.clone_init
)
250 memcached_free(&benchmark_state
.clone
[x
]);
253 free(benchmark_state
.create
);
254 free(benchmark_state
.clone
);
260 test_st micro_tests
[] ={
261 {"memcached_create", 1, (test_callback_fn
)memcached_create_benchmark
},
262 {"memcached_clone", 1, (test_callback_fn
)memcached_clone_benchmark
},
267 collection_st collection
[] ={
268 {"smash", 0, 0, smash_tests
},
269 {"smash_nonblock", (test_callback_fn
)pre_nonblock
, 0, smash_tests
},
270 {"micro-benchmark", (test_callback_fn
)pre_allocate
, (test_callback_fn
)post_allocate
, micro_tests
},
275 #define SERVERS_TO_CREATE 5
277 #include "libmemcached_world.h"
279 void get_world(world_st
*world
)
281 world
->collections
= collection
;
283 world
->create
= (test_callback_create_fn
)world_create
;
284 world
->destroy
= (test_callback_fn
)world_destroy
;
286 world
->test
.startup
= (test_callback_fn
)world_test_startup
;
287 world
->test
.flush
= (test_callback_fn
)world_flush
;
288 world
->test
.pre_run
= (test_callback_fn
)world_pre_run
;
289 world
->test
.post_run
= (test_callback_fn
)world_post_run
;
290 world
->test
.on_error
= (test_callback_error_fn
)world_on_error
;
292 world
->collection
.startup
= (test_callback_fn
)world_container_startup
;
293 world
->collection
.shutdown
= (test_callback_fn
)world_container_shutdown
;
295 world
->runner
= &defualt_libmemcached_runner
;