travis: install python-sphinx
[awesomized/libmemcached] / clients / memslap.cc
index 095cd08817b74e59804347e159227f1604f98e32..9bc4f2385683583960aa12348c11ecf1633a224f 100644 (file)
@@ -2,7 +2,7 @@
  * 
  *  Libmemcached library
  *
- *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *  Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
  *  Copyright (C) 2006-2009 Brian Aker All rights reserved.
  *
  *  Redistribution and use in source and binary forms, with or without
  */
 
 
-#include <config.h>
+#include <mem_config.h>
 
 #include <cassert>
+#include <cerrno>
 #include <cstdio>
 #include <cstdlib>
 #include <cstring>
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/types.h>
-#include <sys/types.h>
+#include <unistd.h>
+
+#include <iostream>
 
-#include <libmemcached/memcached.h>
+#include <libmemcached-1.0/memcached.h>
 
 #include "client_options.h"
 #include "utilities.h"
@@ -63,6 +66,8 @@
 #define DEFAULT_EXECUTE_NUMBER 10000
 #define DEFAULT_CONCURRENCY 1
 
+#define VALUE_BYTES 4096
+
 #define PROGRAM_NAME "memslap"
 #define PROGRAM_DESCRIPTION "Generates a load against a memcached custer of servers."
 
@@ -141,7 +146,7 @@ pairs_st *load_create_data(memcached_st *memc, unsigned int number_of,
                            unsigned int *actual_loaded);
 void flush_all(memcached_st *memc);
 
-static int opt_binary= 0;
+static bool opt_binary= 0;
 static int opt_verbose= 0;
 static int opt_flush= 0;
 static int opt_non_blocking_io= 0;
@@ -151,12 +156,12 @@ static unsigned int opt_createial_load= 0;
 static unsigned int opt_concurrency= 0;
 static int opt_displayflag= 0;
 static char *opt_servers= NULL;
-static int opt_udp_io= 0;
+static bool opt_udp_io= false;
 test_t opt_test= SET_TEST;
 
 extern "C" {
 
-static void *run_task(void *p)
+static __attribute__((noreturn)) void *run_task(void *p)
 {
   thread_context_st *context= (thread_context_st *)p;
 
@@ -209,19 +214,33 @@ int main(int argc, char *argv[])
     {
       opt_servers= strdup(temp);
     }
-    else
+    
+    if (opt_servers == NULL)
     {
-      fprintf(stderr, "No Servers provided\n");
-      return EXIT_FAILURE;
+      std::cerr << "No Servers provided" << std::endl;
+      exit(EXIT_FAILURE);
     }
   }
 
   memcached_server_st *servers= memcached_servers_parse(opt_servers);
+  if (servers == NULL or memcached_server_list_count(servers) == 0)
+  {
+    std::cerr << "Invalid server list provided:" << opt_servers << std::endl;
+    return EXIT_FAILURE;
+  }
 
   pthread_mutex_init(&sleeper_mutex, NULL);
   pthread_cond_init(&sleep_threshhold, NULL);
 
-  scheduler(servers, &conclusion);
+  int error_code= EXIT_SUCCESS;
+  try {
+    scheduler(servers, &conclusion);
+  }
+  catch(std::exception& e)
+  {
+    std::cerr << "Died with exception: " << e.what() << std::endl;
+    error_code= EXIT_FAILURE;
+  }
 
   free(opt_servers);
 
@@ -230,7 +249,7 @@ int main(int argc, char *argv[])
   conclusions_print(&conclusion);
   memcached_server_list_free(servers);
 
-  return EXIT_SUCCESS;
+  return error_code;
 }
 
 void scheduler(memcached_server_st *servers, conclusions_st *conclusion)
@@ -242,26 +261,31 @@ void scheduler(memcached_server_st *servers, conclusions_st *conclusion)
 
   memcached_st *memc= memcached_create(NULL);
 
+  memcached_server_push(memc, servers);
+
   /* We need to set udp behavior before adding servers to the client */
   if (opt_udp_io)
   {
-    memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP,
-                           (uint64_t)opt_udp_io);
-    for (uint32_t x= 0; x < memcached_server_list_count(servers); x++ )
+    if (memcached_failed(memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, opt_udp_io)))
     {
-      servers[x].type= MEMCACHED_CONNECTION_UDP;
+      std::cerr << "Failed to enable UDP." << std::endl;
+      memcached_free(memc);
+      exit(EXIT_FAILURE);
     }
   }
-  memcached_server_push(memc, servers);
 
   memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
                          (uint64_t)opt_binary);
 
   if (opt_flush)
+  {
     flush_all(memc);
+  }
 
   if (opt_createial_load)
+  {
     pairs= load_create_data(memc, opt_createial_load, &actual_loaded);
+  }
 
   char **keys= static_cast<char **>(calloc(actual_loaded, sizeof(char*)));
   size_t *key_lengths= static_cast<size_t *>(calloc(actual_loaded, sizeof(size_t)));
@@ -297,7 +321,7 @@ void scheduler(memcached_server_st *servers, conclusions_st *conclusion)
 
   pthread_t *threads= new  (std::nothrow) pthread_t[opt_concurrency];
 
-  if (not threads)
+  if (threads == NULL)
   {
     exit(EXIT_FAILURE);
   }
@@ -314,7 +338,7 @@ void scheduler(memcached_server_st *servers, conclusions_st *conclusion)
 
     if (opt_test == SET_TEST)
     {
-      context->execute_pairs= pairs_generate(opt_execute_number, 400);
+      context->execute_pairs= pairs_generate(opt_execute_number, VALUE_BYTES);
       context->execute_number= opt_execute_number;
     }
 
@@ -360,6 +384,7 @@ void options_parse(int argc, char *argv[])
     {
       {(OPTIONSTRING)"concurrency", required_argument, NULL, OPT_SLAP_CONCURRENCY},
       {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
+      {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
       {(OPTIONSTRING)"execute-number", required_argument, NULL, OPT_SLAP_EXECUTE_NUMBER},
       {(OPTIONSTRING)"flag", no_argument, &opt_displayflag, OPT_FLAG},
       {(OPTIONSTRING)"flush", no_argument, &opt_flush, OPT_FLUSH},
@@ -376,13 +401,15 @@ void options_parse(int argc, char *argv[])
       {0, 0, 0, 0},
     };
 
+  bool opt_help= false;
+  bool opt_version= false;
   int option_index= 0;
-  int option_rv;
-
   while (1)
   {
-    option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
+    int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
+
     if (option_rv == -1) break;
+
     switch (option_rv)
     {
     case 0:
@@ -395,27 +422,27 @@ void options_parse(int argc, char *argv[])
                   "does not currently support get ops.\n");
         exit(1);
       }
-      opt_udp_io= 1;
+      opt_udp_io= true;
       break;
 
     case OPT_BINARY:
-      opt_binary = 1;
+      opt_binary= true;
       break;
 
     case OPT_VERBOSE: /* --verbose or -v */
-      opt_verbose = OPT_VERBOSE;
+      opt_verbose= OPT_VERBOSE;
       break;
 
     case OPT_DEBUG: /* --debug or -d */
-      opt_verbose = OPT_DEBUG;
+      opt_verbose= OPT_DEBUG;
       break;
 
     case OPT_VERSION: /* --version or -V */
-      version_command(PROGRAM_NAME);
+      opt_version= true;
       break;
 
     case OPT_HELP: /* --help or -h */
-      help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
+      opt_help= true;
       break;
 
     case OPT_SERVERS: /* --servers or -s */
@@ -423,53 +450,88 @@ void options_parse(int argc, char *argv[])
       break;
 
     case OPT_SLAP_TEST:
-      if (!strcmp(optarg, "get"))
+      if (strcmp(optarg, "get") == 0)
       {
         if (opt_udp_io == 1)
         {
           fprintf(stderr, "You can not run a get test in UDP mode. UDP mode "
                   "does not currently support get ops.\n");
-          exit(1);
+          exit(EXIT_FAILURE);
         }
         opt_test= GET_TEST ;
       }
-      else if (!strcmp(optarg, "set"))
+      else if (strcmp(optarg, "set") == 0)
       {
         opt_test= SET_TEST;
       }
-      else if (!strcmp(optarg, "mget"))
+      else if (strcmp(optarg, "mget") == 0)
       {
         opt_test= MGET_TEST;
       }
       else
       {
         fprintf(stderr, "Your test, %s, is not a known test\n", optarg);
-        exit(1);
+        exit(EXIT_FAILURE);
       }
       break;
 
     case OPT_SLAP_CONCURRENCY:
+      errno= 0;
       opt_concurrency= (unsigned int)strtoul(optarg, (char **)NULL, 10);
+      if (errno != 0)
+      {
+        fprintf(stderr, "Invalid value for concurrency: %s\n", optarg);
+        exit(EXIT_FAILURE);
+      }
       break;
 
     case OPT_SLAP_EXECUTE_NUMBER:
+      errno= 0;
       opt_execute_number= (unsigned int)strtoul(optarg, (char **)NULL, 10);
+      if (errno != 0)
+      {
+        fprintf(stderr, "Invalid value for execute: %s\n", optarg);
+        exit(EXIT_FAILURE);
+      }
       break;
 
     case OPT_SLAP_INITIAL_LOAD:
+      errno= 0;
       opt_createial_load= (unsigned int)strtoul(optarg, (char **)NULL, 10);
+      if (errno != 0)
+      {
+        fprintf(stderr, "Invalid value for initial load: %s\n", optarg);
+        exit(EXIT_FAILURE);
+      }
+      break;
+
+    case OPT_QUIET:
+      close_stdio();
       break;
 
+
     case '?':
       /* getopt_long already printed an error message. */
-      exit(1);
+      exit(EXIT_FAILURE);
 
     default:
       abort();
     }
   }
 
-  if ((opt_test == GET_TEST or opt_test == MGET_TEST) && opt_createial_load == 0)
+  if (opt_version)
+  {
+    version_command(PROGRAM_NAME);
+    exit(EXIT_SUCCESS);
+  }
+
+  if (opt_help)
+  {
+    help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
+    exit(EXIT_SUCCESS);
+  }
+
+  if ((opt_test == GET_TEST or opt_test == MGET_TEST) and opt_createial_load == 0)
     opt_createial_load= DEFAULT_INITIAL_LOAD;
 
   if (opt_execute_number == 0)
@@ -506,7 +568,7 @@ pairs_st *load_create_data(memcached_st *memc, unsigned int number_of,
   /* We always used non-blocking IO for load since it is faster */
   memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_NO_BLOCK, 0);
 
-  pairs_st *pairs= pairs_generate(number_of, 400);
+  pairs_st *pairs= pairs_generate(number_of, VALUE_BYTES);
   *actual_loaded= execute_set(memc_clone, pairs, number_of);
 
   memcached_free(memc_clone);