From: Brian Aker Date: Sat, 29 Sep 2007 18:08:12 +0000 (-0700) Subject: Reworked the generator code into its own files. X-Git-Tag: 0.3~7 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=320638630f5a35ddfc4fe0d571b24016a7a80de7;p=awesomized%2Flibmemcached Reworked the generator code into its own files. --- diff --git a/src/Makefile.am b/src/Makefile.am index 47e91aad..4a119db3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -20,5 +20,5 @@ memrm_LDADD = $(LDADDS) memflush_SOURCES = memflush.c utilities.c memflush_LDADD = $(LDADDS) -memslap_SOURCES = memslap.c utilities.c +memslap_SOURCES = memslap.c utilities.c generator.c memslap_LDADD = $(LDADDS) diff --git a/src/generator.c b/src/generator.c new file mode 100644 index 00000000..df04d348 --- /dev/null +++ b/src/generator.c @@ -0,0 +1,67 @@ +#include +#include +#include + +#include "generator.h" + +/* Use this for string generation */ +static const char ALPHANUMERICS[]= + "0123456789ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstuvwxyz"; + +#define ALPHANUMERICS_SIZE (sizeof(ALPHANUMERICS)-1) + +static void get_random_string(char *buffer, size_t size) +{ + char *buffer_ptr= buffer; + + while (--size) + *buffer_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE]; + *buffer_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE]; +} + +void pairs_free(pairs_st *pairs) +{ + unsigned int x; + + /* We free until we hit the null pair we stores during creation */ + for (x= 0; pairs[x].key; x++) + { + free(pairs[x].key); + free(pairs[x].value); + } + + free(pairs); +} + +pairs_st *pairs_generate(unsigned long long number_of) +{ + unsigned int x; + pairs_st *pairs; + + pairs= (pairs_st*)malloc(sizeof(pairs_st) * (number_of+1)); + + if (!pairs) + goto error; + + memset(pairs, 0, sizeof(pairs_st) * (number_of+1)); + + for (x= 0; x < number_of; 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); +} diff --git a/src/generator.h b/src/generator.h new file mode 100644 index 00000000..c1c37f5b --- /dev/null +++ b/src/generator.h @@ -0,0 +1,16 @@ +/* + Code to generate data to be pushed into memcached +*/ + +typedef struct pairs_st pairs_st; + +struct pairs_st { + char *key; + size_t key_length; + char *value; + size_t value_length; +}; + +pairs_st *pairs_generate(unsigned long long number_of); +void pairs_free(pairs_st *pairs); +static void get_random_string(char *buffer, size_t size); diff --git a/src/memslap.c b/src/memslap.c index 661790ab..216506d8 100644 --- a/src/memslap.c +++ b/src/memslap.c @@ -12,16 +12,10 @@ #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 conclusions_st { @@ -31,18 +25,8 @@ struct conclusions_st { unsigned int rows_read; }; -struct pairs_st { - char *key; - size_t key_length; - char *value; - size_t value_length; -}; - /* 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; @@ -70,7 +54,7 @@ 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); @@ -172,50 +156,6 @@ 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); -} - void conclusions_print(conclusions_st *conclusion) { printf("\tLoaded %u rows\n", conclusion->rows_loaded); @@ -225,12 +165,3 @@ void conclusions_print(conclusions_st *conclusion) printf("\tTook %ld.%03ld seconds to read data\n", conclusion->read_time / 1000, conclusion->read_time % 1000); } - -static void get_random_string(char *buffer, size_t size) -{ - char *buffer_ptr= buffer; - - while (--size) - *buffer_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE]; - *buffer_ptr++= ALPHANUMERICS[random() % ALPHANUMERICS_SIZE]; -}