Merge
[m6w6/libmemcached] / tests / atomsmasher.c
index d3074a7f1eb61da9aebb5a6b86d8af68b703016d..b7e5924e0d2a364041e061c124bc9930c22b3710 100644 (file)
@@ -14,7 +14,6 @@
 */
 #include "libmemcached/common.h"
 
-#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -57,11 +56,10 @@ static test_return_t cleanup_pairs(memcached_st *memc __attribute__((unused)))
 
 static test_return_t generate_pairs(memcached_st *memc __attribute__((unused)))
 {
-  unsigned long long x;
   global_pairs= pairs_generate(GLOBAL_COUNT, 400);
   global_count= GLOBAL_COUNT;
 
-  for (x= 0; x < global_count; x++)
+  for (size_t x= 0; x < global_count; x++)
   {
     global_keys[x]= global_pairs[x].key; 
     global_keys_length[x]=  global_pairs[x].key_length;
@@ -72,14 +70,13 @@ static test_return_t generate_pairs(memcached_st *memc __attribute__((unused)))
 
 static test_return_t drizzle(memcached_st *memc)
 {
-  unsigned int x;
   memcached_return_t rc;
   char *return_value;
   size_t return_value_length;
   uint32_t flags;
 
 infinite:
-  for (x= 0; x < TEST_COUNTER; x++)
+  for (size_t x= 0; x < TEST_COUNTER; x++)
   {
     uint32_t test_bit;
     uint8_t which;
@@ -92,9 +89,13 @@ infinite:
       return_value= memcached_get(memc, global_keys[test_bit], global_keys_length[test_bit],
                                   &return_value_length, &flags, &rc);
       if (rc == MEMCACHED_SUCCESS && return_value)
+      {
         free(return_value);
+      }
       else if (rc == MEMCACHED_NOTFOUND)
+      {
         continue;
+      }
       else
       {
         WATCHPOINT_ERROR(rc);
@@ -119,45 +120,14 @@ infinite:
   if (getenv("MEMCACHED_ATOM_BURIN_IN"))
     goto infinite;
 
-  return 0;
+  return TEST_SUCCESS;
 }
 
-static memcached_return_t pre_nonblock(memcached_st *memc)
+static test_return_t pre_nonblock(memcached_st *memc)
 {
   memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
 
-  return MEMCACHED_SUCCESS;
-}
-
-static memcached_return_t pre_md5(memcached_st *memc)
-{
-  memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MD5);
-
-  return MEMCACHED_SUCCESS;
-}
-
-static memcached_return_t pre_hsieh(memcached_st *memc)
-{
-  memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_HSIEH);
-
-  return MEMCACHED_SUCCESS;
-}
-
-static memcached_return_t enable_consistent(memcached_st *memc)
-{
-  memcached_server_distribution_t value= MEMCACHED_DISTRIBUTION_CONSISTENT;
-  memcached_hash_t hash;
-  memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, value);
-  pre_hsieh(memc);
-
-  value= (memcached_server_distribution_t)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION);
-  assert(value == MEMCACHED_DISTRIBUTION_CONSISTENT);
-
-  hash= (memcached_hash_t)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_HASH);
-  assert(hash == MEMCACHED_HASH_HSIEH);
-
-
-  return MEMCACHED_SUCCESS;
+  return TEST_SUCCESS;
 }
 
 /* 
@@ -176,7 +146,7 @@ static test_return_t add_test(memcached_st *memc)
   rc= memcached_set(memc, key, strlen(key), 
                     value, strlen(value),
                     (time_t)0, (uint32_t)0);
-  test_truth(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
+  test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
   memcached_quit(memc);
   rc= memcached_add(memc, key, strlen(key), 
                     value, strlen(value),
@@ -185,11 +155,11 @@ static test_return_t add_test(memcached_st *memc)
   /* Too many broken OS'es have broken loopback in async, so we can't be sure of the result */
   if (setting_value)
   {
-    test_truth(rc == MEMCACHED_NOTSTORED || rc == MEMCACHED_STORED);
+    test_true(rc == MEMCACHED_NOTSTORED || rc == MEMCACHED_STORED);
   }
   else
   {
-    test_truth(rc == MEMCACHED_NOTSTORED);
+    test_true(rc == MEMCACHED_NOTSTORED);
   }
 
   return 0;
@@ -201,8 +171,7 @@ static test_return_t add_test(memcached_st *memc)
  */
 static test_return_t many_adds(memcached_st *memc)
 {
-  unsigned int i;
-  for (i = 0; i < TEST_COUNTER; i++)
+  for (size_t x= 0; x < TEST_COUNTER; x++)
   {
     add_test(memc);
   }
@@ -217,13 +186,87 @@ test_st smash_tests[] ={
   {0, 0, 0}
 };
 
+#define BENCHMARK_TEST_LOOP 20000
+
+struct benchmark_state_st
+{
+  bool create_init;
+  bool clone_init;
+  memcached_st *create;
+  memcached_st *clone;
+} benchmark_state;
+
+static test_return_t memcached_create_benchmark(memcached_st *memc __attribute__((unused)))
+{
+  benchmark_state.create_init= true;
+
+  for (size_t x= 0; x < BENCHMARK_TEST_LOOP; x++)
+  {
+    memcached_st *ptr;
+    ptr= memcached_create(&benchmark_state.create[x]);
+
+    test_true(ptr);
+  }
+
+  return TEST_SUCCESS;
+}
+
+static test_return_t memcached_clone_benchmark(memcached_st *memc)
+{
+  benchmark_state.clone_init= true;
+
+  for (size_t x= 0; x < BENCHMARK_TEST_LOOP; x++)
+  {
+    memcached_st *ptr;
+    ptr= memcached_clone(&benchmark_state.clone[x], memc);
+
+    test_true(ptr);
+  }
+
+  return TEST_SUCCESS;
+}
+
+static test_return_t pre_allocate(memcached_st *memc __attribute__((unused)))
+{
+  memset(&benchmark_state, 0, sizeof(benchmark_state));
+
+  benchmark_state.create= (memcached_st *)calloc(BENCHMARK_TEST_LOOP, sizeof(memcached_st));
+  test_true(benchmark_state.create);
+  benchmark_state.clone= (memcached_st *)calloc(BENCHMARK_TEST_LOOP, sizeof(memcached_st));
+  test_true(benchmark_state.clone);
+
+  return TEST_SUCCESS;
+}
+
+static test_return_t post_allocate(memcached_st *memc __attribute__((unused)))
+{
+  for (size_t x= 0; x < BENCHMARK_TEST_LOOP; x++)
+  {
+    if (benchmark_state.create_init)
+      memcached_free(&benchmark_state.create[x]);
+
+    if (benchmark_state.clone_init)
+      memcached_free(&benchmark_state.clone[x]);
+  }
+
+  free(benchmark_state.create);
+  free(benchmark_state.clone);
+
+  return TEST_SUCCESS;
+}
+
+
+test_st micro_tests[] ={
+  {"memcached_create", 1, (test_callback_fn)memcached_create_benchmark },
+  {"memcached_clone", 1, (test_callback_fn)memcached_clone_benchmark },
+  {0, 0, 0}
+};
+
 
 collection_st collection[] ={
   {"smash", 0, 0, smash_tests},
-  {"smash_hsieh", (test_callback_fn)pre_hsieh, 0, smash_tests},
-  {"smash_hsieh_consistent", (test_callback_fn)enable_consistent, 0, smash_tests},
-  {"smash_md5", (test_callback_fn)pre_md5, 0, smash_tests},
   {"smash_nonblock", (test_callback_fn)pre_nonblock, 0, smash_tests},
+  {"micro-benchmark", (test_callback_fn)pre_allocate, (test_callback_fn)post_allocate, micro_tests},
   {0, 0, 0, 0}
 };
 
@@ -235,12 +278,18 @@ collection_st collection[] ={
 void get_world(world_st *world)
 {
   world->collections= collection;
-  world->collection_startup= (test_callback_fn)world_collection_startup;
-  world->flush= (test_callback_fn)world_flush;
-  world->pre_run= (test_callback_fn)world_pre_run;
+
   world->create= (test_callback_create_fn)world_create;
-  world->post_run= (test_callback_fn)world_post_run;
-  world->on_error= (test_callback_error_fn)world_on_error;
   world->destroy= (test_callback_fn)world_destroy;
+
+  world->test.startup= (test_callback_fn)world_test_startup;
+  world->test.flush= (test_callback_fn)world_flush;
+  world->test.pre_run= (test_callback_fn)world_pre_run;
+  world->test.post_run= (test_callback_fn)world_post_run;
+  world->test.on_error= (test_callback_error_fn)world_on_error;
+
+  world->collection.startup= (test_callback_fn)world_container_startup;
+  world->collection.shutdown= (test_callback_fn)world_container_shutdown;
+
   world->runner= &defualt_libmemcached_runner;
 }