Reworked the generator code into its own files.
[m6w6/libmemcached] / src / memslap.c
index 0ea9cca34d581cdcf0e05b18ff07859ade99fa76..216506d88b26625bdc38521a34a64f77a7d0e63e 100644 (file)
@@ -5,38 +5,32 @@
 #include <sys/types.h>
 #include <sys/mman.h>
 #include <fcntl.h>
+#include <sys/time.h>
 #include <getopt.h>
 
 #include <memcached.h>
 
 #include "client_options.h"
 #include "utilities.h"
+#include "generator.h"
 
 
-/* Use this for string generation */
-static const char ALPHANUMERICS[]=
-  "0123456789ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstuvwxyz";
-
-#define ALPHANUMERICS_SIZE (sizeof(ALPHANUMERICS)-1)
-
 /* Types */
-typedef struct pairs_st pairs_st;
+typedef struct conclusions_st conclusions_st;
 
-struct pairs_st {
-  char *key;
-  size_t key_length;
-  char *value;
-  size_t value_length;
+struct conclusions_st {
+  long int load_time;
+  long int read_time;
+  unsigned int rows_loaded;
+  unsigned int rows_read;
 };
 
 /* Prototypes */
 void options_parse(int argc, char *argv[]);
-static pairs_st *pairs_generate(void);
-static void pairs_free(pairs_st *pairs);
-static void get_random_string(char *buffer, size_t size);
+void conclusions_print(conclusions_st *conclusion);
 
 static int opt_verbose= 0;
-static int opt_default_pairs= 100;
+static unsigned int opt_default_pairs= 100;
 static int opt_displayflag= 0;
 static char *opt_servers= NULL;
 
@@ -45,7 +39,11 @@ int main(int argc, char *argv[])
   unsigned int x;
   memcached_return rc;
   memcached_st *memc;
+  struct timeval start_time, end_time;
   pairs_st *pairs;
+  conclusions_st conclusion;
+
+  memset(&conclusion, 0, sizeof(conclusions_st));
 
   srandom(time(NULL));
   memc= memcached_init(NULL);
@@ -56,19 +54,42 @@ int main(int argc, char *argv[])
 
   parse_opt_servers(memc, opt_servers);
 
-  pairs= pairs_generate();
+  pairs= pairs_generate(opt_default_pairs);
 
 
+  gettimeofday(&start_time, NULL);
   for (x= 0; x < opt_default_pairs; x++)
   {
-    printf("Key(%u) %.10s \t%.10s\n", x, pairs[x].key, pairs[x].value);
     rc= memcached_set(memc, pairs[x].key, pairs[x].key_length,
                       pairs[x].value, pairs[x].value_length,
                       0, 0);
     if (rc != MEMCACHED_SUCCESS)
       fprintf(stderr, "Failured on insert of %.*s\n", 
               (unsigned int)pairs[x].key_length, pairs[x].key);
+    conclusion.rows_loaded++;
+  }
+  gettimeofday(&end_time, NULL);
+  conclusion.load_time= timedif(end_time, start_time);
+
+  gettimeofday(&start_time, NULL);
+  for (x= 0; x < opt_default_pairs; x++)
+  {
+    char *value;
+    size_t value_length;
+    uint16_t flags;
+
+    value= memcached_get(memc, pairs[x].key, pairs[x].key_length,
+                         &value_length,
+                         &flags, &rc);
+
+    if (rc != MEMCACHED_SUCCESS)
+      fprintf(stderr, "Failured on read of %.*s\n", 
+              (unsigned int)pairs[x].key_length, pairs[x].key);
+    conclusion.rows_read++;
+    free(value);
   }
+  gettimeofday(&end_time, NULL);
+  conclusion.read_time= timedif(end_time, start_time);
 
   pairs_free(pairs);
 
@@ -76,6 +97,8 @@ int main(int argc, char *argv[])
 
   memcached_deinit(memc);
 
+  conclusions_print(&conclusion);
+
   return 0;
 }
 
@@ -89,6 +112,7 @@ void options_parse(int argc, char *argv[])
       {"debug", no_argument, &opt_verbose, OPT_DEBUG},
       {"servers", required_argument, NULL, OPT_SERVERS},
       {"flag", no_argument, &opt_displayflag, OPT_FLAG},
+      {"default-pairs", required_argument, NULL, OPT_SLAP_DEFAULT_PAIRS},
       {0, 0, 0, 0},
     };
 
@@ -120,6 +144,9 @@ void options_parse(int argc, char *argv[])
     case OPT_SERVERS: /* --servers or -s */
       opt_servers= strdup(optarg);
       break;
+    case OPT_SLAP_DEFAULT_PAIRS:
+      opt_default_pairs= strtol(optarg, (char **)NULL, 10);
+      break;
     case '?':
       /* getopt_long already printed an error message. */
       exit(1);
@@ -129,55 +156,12 @@ void options_parse(int argc, char *argv[])
   }
 }
 
-static void pairs_free(pairs_st *pairs)
-{
-  unsigned int x;
-
-  for (x= 0; x < opt_default_pairs; x++)
-  {
-    free(pairs[x].key);
-    free(pairs[x].value);
-  }
-
-  free(pairs);
-}
-
-static pairs_st *pairs_generate(void)
-{
-  unsigned int x;
-  pairs_st *pairs;
-
-  pairs= (pairs_st*)malloc(sizeof(pairs_st) * opt_default_pairs);
-
-  if (!pairs)
-    goto error;
-
-  for (x= 0; x < opt_default_pairs; x++)
-  {
-    pairs[x].key= (char *)malloc(sizeof(char) * 100);
-    if (!pairs[x].key)
-      goto error;
-    get_random_string(pairs[x].key, 100);
-    pairs[x].key_length= 100;
-
-    pairs[x].value= (char *)malloc(sizeof(char) * 400);
-    if (!pairs[x].value)
-      goto error;
-    get_random_string(pairs[x].value, 400);
-    pairs[x].value_length= 400;
-  }
-
-  return pairs;
-error:
-    fprintf(stderr, "Memory Allocation failure in pairs_generate.\n");
-    exit(0);
-}
-
-static void get_random_string(char *buffer, size_t size)
+void conclusions_print(conclusions_st *conclusion)
 {
-  char *buffer_ptr= buffer;
-
-  while (--size)
-    *buffer_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE];
-  *buffer_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE];
+  printf("\tLoaded %u rows\n", conclusion->rows_loaded);
+  printf("\tRead %u rows\n", conclusion->rows_read);
+  printf("\tTook %ld.%03ld seconds to load data\n", conclusion->load_time / 1000, 
+         conclusion->load_time % 1000);
+  printf("\tTook %ld.%03ld seconds to read data\n", conclusion->read_time / 1000, 
+         conclusion->read_time % 1000);
 }