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 <libtest/test.hpp>
19 #include <libmemcached/memcached.h>
26 #include <sys/types.h>
30 #include <clients/generator.h>
31 #include <clients/execute.h>
33 #include <libtest/server.h>
35 #include <tests/debug.h>
37 using namespace libtest
;
39 /* Number of items generated for tests */
40 #define GLOBAL_COUNT 100000
42 /* Number of times to run the test loop */
43 #define TEST_COUNTER 500000
45 static pairs_st
*global_pairs
;
46 static char *global_keys
[GLOBAL_COUNT
];
47 static size_t global_keys_length
[GLOBAL_COUNT
];
49 static test_return_t
cleanup_pairs(memcached_st
*)
51 pairs_free(global_pairs
);
56 static test_return_t
generate_pairs(memcached_st
*)
58 global_pairs
= pairs_generate(GLOBAL_COUNT
, 400);
60 for (ptrdiff_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
)
72 for (ptrdiff_t x
= 0; x
< TEST_COUNTER
; x
++)
74 memcached_return_t rc
;
76 size_t return_value_length
;
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
)
99 test_compare(MEMCACHED_SUCCESS
, rc
);
104 rc
= memcached_set(memc
, global_pairs
[test_bit
].key
,
105 global_pairs
[test_bit
].key_length
,
106 global_pairs
[test_bit
].value
,
107 global_pairs
[test_bit
].value_length
,
109 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_BUFFERED
)
111 test_compare(MEMCACHED_SUCCESS
, rc
);
116 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 const char *key
= "foo";
138 const char *value
= "when we sanitize";
140 memcached_return_t rc
;
141 rc
= memcached_set(memc
, key
, strlen(key
),
142 value
, strlen(value
),
143 (time_t)0, (uint32_t)0);
144 test_true_got(rc
== MEMCACHED_SUCCESS
or rc
== MEMCACHED_BUFFERED
, memcached_strerror(NULL
, rc
));
145 memcached_quit(memc
);
146 rc
= memcached_add(memc
, key
, strlen(key
),
147 value
, strlen(value
),
148 (time_t)0, (uint32_t)0);
150 if (rc
== MEMCACHED_CONNECTION_FAILURE
)
155 /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
156 if (memcached_behavior_get(memc
, MEMCACHED_BEHAVIOR_NO_BLOCK
))
158 test_true(rc
== MEMCACHED_NOTSTORED
or rc
== MEMCACHED_STORED
);
162 test_compare_got(MEMCACHED_NOTSTORED
, rc
, memcached_strerror(NULL
, rc
));
169 * repeating add_tests many times
170 * may show a problem in timing
172 static test_return_t
many_adds(memcached_st
*memc
)
175 for (ptrdiff_t x
= 0; x
< TEST_COUNTER
; x
++)
177 test_compare_got(TEST_SUCCESS
, add_test(memc
), x
);
182 test_st smash_tests
[] ={
183 {"generate_pairs", true, (test_callback_fn
*)generate_pairs
},
184 {"drizzle", true, (test_callback_fn
*)drizzle
},
185 {"cleanup", true, (test_callback_fn
*)cleanup_pairs
},
186 {"many_adds", true, (test_callback_fn
*)many_adds
},
190 #define BENCHMARK_TEST_LOOP 20000
192 struct benchmark_state_st
196 memcached_st
*create
;
200 static test_return_t
memcached_create_benchmark(memcached_st
*)
202 benchmark_state
.create_init
= true;
204 for (ptrdiff_t x
= 0; x
< BENCHMARK_TEST_LOOP
; x
++)
207 ptr
= memcached_create(&benchmark_state
.create
[x
]);
215 static test_return_t
memcached_clone_benchmark(memcached_st
*memc
)
217 benchmark_state
.clone_init
= true;
219 for (ptrdiff_t x
= 0; x
< BENCHMARK_TEST_LOOP
; x
++)
221 memcached_st
*ptr
= memcached_clone(&benchmark_state
.clone
[x
], memc
);
229 static test_return_t
pre_allocate(memcached_st
*)
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
*)
243 for (ptrdiff_t x
= 0; x
< BENCHMARK_TEST_LOOP
; x
++)
245 if (benchmark_state
.create_init
)
247 memcached_free(&benchmark_state
.create
[x
]);
250 if (benchmark_state
.clone_init
)
252 memcached_free(&benchmark_state
.clone
[x
]);
256 free(benchmark_state
.create
);
257 free(benchmark_state
.clone
);
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
},
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
},
278 #include "tests/libmemcached_world.h"
280 void get_world(Framework
*world
)
282 world
->collections
= collection
;
284 world
->_create
= (test_callback_create_fn
*)world_create
;
285 world
->_destroy
= (test_callback_destroy_fn
*)world_destroy
;
287 world
->item
._startup
= (test_callback_fn
*)world_test_startup
;
288 world
->item
._flush
= (test_callback_fn
*)world_flush
;
289 world
->_on_error
= (test_callback_error_fn
*)world_on_error
;
291 world
->collection_startup
= (test_callback_fn
*)world_container_startup
;
292 world
->collection_shutdown
= (test_callback_fn
*)world_container_shutdown
;
294 world
->set_runner(new LibmemcachedRunner
);